I’m coding ios game with cocos2d-x. i want to save data game on file plist . how to do that?
Thank you
I’m coding ios game with cocos2d-x. i want to save data game on file plist . how to do that?
Thank you
Wind Runner wrote:
I’m coding ios game with cocos2d-x. i want to save data game on file plist . how to do that?
>
Thank you
If you want to read/write custom plists only on iOS, you can use NSPropertyListSerialization and perform convertion between cocos2dx and NS objects. You can use CCDictionary::createWithContentsOfFile() to read plist from resources; but this method works incorrectly with <, >, & and other escaped symbols.
you can write detailed for your idea :(. If you can, please post code to this post. Thank you
I mean that you can write function that recursively visits nested NSObjects and constructs CCDictionary/CCArray by copying
// string() constructs new CCString
// dictionary() iterates NSDictionary keys and calls objc2cocos for each object,
// resulting CCObject * will be added to CCDictionary
// array() iterates NSArray, converts each objects with objc2cocos and adds
resulting CCObject * to CCArray *
CCObject *objc2cocos(id value)
{
if ([id isKindOfClass:[NSString class]])
return string((NSString *)value);
if ([id isKindOfClass:[NSArray class]])
return array((NSArray *)value);
if ([id isKindOfClass:[NSDictionary class]])
return dictionary((NSDictionary *)value);
return NULL;
}
The same idea can be used to convert CCObject subclasses to NSObject subclasses, type recognition possible with typeid
const std::string name = typeid(*ccObject).name();
if (name.find("CCDictionary") != std::string::npos) {
return convert((const CCDictionary *)ccObject);
} else if (name.find("CCArray") != std::string::npos) {
return convert((const CCArray *)ccObject);
} else if (name.find("CCString") != std::string::npos) {
return convert((const CCString *)ccObject);
}
return nil;
You can try my branch here:
I use NSDictionary to do the job for iOS & Mac, and using tinyxml2 for other platforms.
I put the implementation in CCFileUtils because CCDictionary::createWithContentsOfFile() is already in it.
It is not merged yet, because @Minggo though it may be not a good idea to implement this stuff in CCFileUtils.
What’s your opinion ?
Sergey Shambir wrote:
I mean that you can write function that recursively visits nested NSObjects and constructs CCDictionary/CCArray by copying
[…]
>
The same idea can be used to convert CCObject subclasses to NSObject subclasses, type recognition possible with typeid
[…]
Nice improvement! I’ve also implemented alike thing two weeks ago, but your solution is more general-purpose (for example, we used binary format on iOS for backward compatibility).
I want also mention (again) that CCDictionary::createWithContentsOfFile() works incorrectly with escaped text nodes. Just put “keytest” as dictionary key and similar text as dictionary value, than save it with CCDictionary::writeToFile(). Parser will read garbage on all platforms except iOS/OSX. Tinyxml-based reader works OK.
Dai tianqi wrote:
You can try my branch here:
https://github.com/cocos2d/cocos2d-x/pull/2361
>
I use NSDictionary to do the job for iOS & Mac, and using tinyxml2 for other platforms.
I put the implementation in CCFileUtils because CCDictionary::createWithContentsOfFile() is already in it.
It is not merged yet, because @Minggo though it may be not a good idea to implement this stuff in CCFileUtils.
>
What’s your opinion ?
>
>
Sergey Shambir wrote:
> I mean that you can write function that recursively visits nested NSObjects and constructs CCDictionary/CCArray by copying
> […]
>
> The same idea can be used to convert CCObject subclasses to NSObject subclasses, type recognition possible with typeid
> […]
so can u give instruction for your idea.thank u very much. so if can i use ccdictionary to save and load data from plist ??
i find way to write data to plist
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
//here add elements to data file and write data to file
int value = 5;
[data setObject:[NSNumber numberWithInt:value] forKey:@”value”];
[data writeToFile: path atomically:YES];
[data release]
anyone can convert it to cocos2dx