How do I save/add sprites into a table ( save game settings)?

Hi guys,

I decided to use JSON to save my game settings between play. I loved PropertyBag but I had to switch because of the complexity of my game settings table. Anyway it works fine for fixed sprites(x,y,health) and things like score and hi-score . My issue is that I discovered that I need to also to save moving sprites (it is space shooter game) The enemies also need to be saved. The problems is that those sprites a spawn randomly so i never know how many of them will be present (alive) just before I exit the game (those sprites get shot at and go away) So I need to save whatever sprites are left on the screen just before the player exit to the main menu. Of course the game needs to read the file back to place those sprites back on the screen..

In any event, I have fixed amount of fixed sprites (3) so it was easy to keep track and save/load those. Something like:

In Main:

1
2
3
4
5
6
_G.gameSettings.spriteA = 
{
{x=0, y=0,vx=0,vy =0,health = 100},
{x=0, y=0,vx=0,vy =0,health = 100},
 {x=0, y=0,vx=0,vy =0,health = 100}
}

@lemsim,

you might want to save first, the number of enemies that are present, then read the data for those many times (if that makes sense) and try to name your data as enemy_1, enemy_2 and so on, so in the loop you can read back "enemy_" .. i

hope that makes sense,

cheers,

?:)

Hello JayvantV

Thanks a lot! I guess what I do not get is how to setup the spriteB table at the beginning. I was able to set the spriteA table as:

1
2
3
4
5
6
_G.gameSettings.spriteA = 
{
{x=0, y=0,vx=0,vy =0,health = 100},
{x=0, y=0,vx=0,vy =0,health = 100},
 {x=0, y=0,vx=0,vy =0,health = 100}
}

Mo,
How are you saving the enemies?

Yes, the enemies will get removed from the array, that's why you should use a table and not an array, as suggested.

To understand about tables, read up here and here

Try to have an ID for each sprite that you are saving rather than just having an index, which can change.

cheers,

?:)

Hi

I am using a global variable _G.gamesettings which I save/load using JSON. Something like this:

1
2
3
4
5
6
7
for i = 1,3 do   -- I have 3 spritesA                                                                                                                                                                                                                                          gameSettings.spriteA[i].x = spriteA [i].x
gameSettings.spriteA[i].y = spriteA [i].y
gameSettings.spriteA[i].vx = spriteA [i].vx
gameSettings.spriteA[i].vy = spriteA [i].vy
end                                                             
 
data.saveSettings()

Mo,
I am still not clear on what is spriteB, you are saving spriteA as spriteB in the example above.

so make it easier...
create a value in spriteB called numberOfItems and save the spriteB's as sprite_

so that when you load them back, they will all get ordered from 1..number

it seems that the issue is a bit off from the issue of loading/saving the JSON data, but on the way you are using the tables.

perhaps the project can help understand what you are after.

cheers,

?:)

Hi

Sorry for the confusion. I edited the code above. Basically spriteA is a table that keep track of sprites that are fixed on the screen (I have 3 of those on the screen). SpriteB is a table that keep track of sprites that move around the screen (that why they have a vx, vy speeds)

You are 100%. My problem is not saving/load JSON but how I am using tables. I learned already (thanks to your great tutorials) that i cannot treat array and table the same (like #spriteA..)

Let me think about you suggesting and get back to you.

Thanks again.

Mo

views:1505 update:2011/10/15 21:01:16
corona forums © 2003-2011