Page 1 of 1

How do I save and load objects from an array?

PostPosted: Sat Jan 16, 2016 12:51 am
by visionarybeast
How do I save and load objects from an array?

Re: How do I save and load objects from an array?

PostPosted: Sat Jan 16, 2016 6:21 am
by R0J0hound
Maybe something like this to save if you just want to save positions.

Set array size to (Sprite.count, 2, 1)
For each Sprite
--- set array at (loopindex, 0) to Sprite.x
--- set array at (loopindex, 1) to Sprite.y

Then maybe this to load:

Array: for each X
--- create Sprite at array.at(array.curx,0), array.at(array.curx,1) )

Re: How do I save and load objects from an array?

PostPosted: Mon Jan 18, 2016 7:18 am
by R0J0hound
Another thing you can look at is the .asJSON expression and the "load from json" action. Every object has them and you can use that to save only objects that you want.

Here's an example:
https://dl.dropboxusercontent.com/u/542 ... load2.capx

The saving saves three values for each object:
"zindex" so the when we load they zorder will be the same.
"name" so we can know what object to load.
".asJSON" the actual object data.

after all that is put in the array it sorts the array by the zindexes, so then the array has each object that needs to be created in order of z.

Loading is simple enough, and we use the "name" to create the right object type, and then the json is loaded.

The capx has some comments of what you can do and what would require further implementation. Globals could be saved too but I'll leave that as an exercise for the user.


But actually the more you want to save the more you probably should just use the save/load actions included with C2. You can use the "no save" behavior on objects you don't want to save.
https://www.scirra.com/tutorials/526/ho ... -savegames