How to make plist and read data from plist in cocos2d-x
Depending on the conditions,if it is required to store small piece of data(mostly when tabular) and to retrieve that data in your project,then it is not wise to use sqlite,plist is best to use conditionally.
For making and add data to plist,just go to ios folder in your project,right click and select new file option,window will appear,then select resource option and click on property list,window then looks to you as following:
Click Next and name the plist file and click on create,your plist is created.Now open your plist and add the data to the file
For the Root folder,select Array from type(you can select any option) and click on + sign,1 data is created,you can select any type of the data,here i am selecting dictionary,open the data and add the items to a dictionary and add the corresponding values to them.Similarly you can add other data to the root folder,and it will look as:
Now your plist is ready to retrieve.
For retrieving the data from the plist,use the following code:
int Utils::getting_data_from_plist(int l,string s)
std::string levelsFile = CCFileUtils::sharedFileUtils()->fullPathForFilename("plist_name");
CCArray*arr = CCArray::createWithContentsOfFileThreadSafe(levelsFile.c_str());
CCDictionary *dict= (CCDictionary*) arr->objectAtIndex(l);
int characterSelected = dict->valueForKey(s.c_str())->intValue();
CCLOG("arr value %d",characterSelected);
this above function will return one value at a time for the array index and for the corresponding item.For calling this function,we use the following code line.Example we want to retrieve the value for 3_star for the 1st index,then we will call as:
int x=Utils::getting_data_from_plist(1, "3_star");//it will return value 4 as i saved 4 there(check in screenshot above)
Here we are getting all the values in the array “arr” in the above function,we can modify the array as per our requirement.
2,880 total views, 1 views today