Page 1 of 1

Removing missionVariables

Posted: Tue Jan 20, 2015 4:53 am
by phkb
I have an OXP that stores an array of items to missionVariables kind of like this:

Code: Select all

	missionVariables.MyOXP_TotalListEntries = this._myList.length;
	for (var i = 0; i < this._myList.length; i++) {
		missionVariables["MyOXP_ListItem" + (i + 1).toString()] = this._myList[i];
	}
This works fine. However, if at one point in time the length of _myList is truncated, say from 10 items to 5, when I look at the save file, all 10 items will still be in the file, even though my save routine only saves 5.

How do I remove the extraneous items from the missionVariables? Or should I combine all the items into a single text entry and split it out when loading the save file? Or am I doing this all wrong in the first place?

Re: Removing missionVariables

Posted: Tue Jan 20, 2015 7:56 am
by cim
Explicitly setting the value of a mission variable to null should remove it from the save file and the missionVariables object. Once set, a mission variable retains that value across save/load, for as long as the save-game keeps being played, until set to a new value. So once you've set ListItem8, that value remains until you unset it.

If you need to store information which is not a string - but you're only storing it to missionVariables to keep it safe over save/load - then one option is to use JSON.stringify and JSON.parse to convert a JS object or array to a string and back again.

If you do that, bear in mind that JSON.parse does not do type checking, so if you change the format of the saved object at any point, your OXP needs to be able to handle both old and new formats of the objects - and ideally, earlier versions of your OXP can safely handle later versions of the object. I would suggest putting a "version" number onto anything you save this way.