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:
[lua]_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}
}[/lua]

Then in the mainGame module. I reload those like this:

[lua]spriteA = {}

for i = 1,3 do
spriteA[i] = display.newImageRect(“spriteA”…i … “.png”,75,75)
spriteA[i].x = gameSettings.spriteA [i].x
spriteA[i].y = gameSettings.spriteA [i].y
spriteA[i].health = gameSettings.spriteA [i].health
end [/lua]

BUT what to do with with spriteB when I do not know in advance how many I will need to save before end. SpriteB will also be a table with each element having an x,y,vx and vy (no health)

I tried for i = 1, #spriteB do… but it does not seems to work or I may not have save the spriteB table correctly.

I tried this to save (JSON) the spriteB’s location and speed:

[lua]-- in main game screen in the exit to main menu button method
– (spriteB [i]x,y,vx,vy are the current –
– location/speed of each spriteB on the screen just before the user --exit the game.

for i = 1,#spriteB do gameSettings.spriteB[i].x = spriteB [i].x
gameSettings.spriteB[i].y = spriteB [i].y
gameSettings.spriteB[i].vx = spriteB [i].vx
gameSettings.spriteB[i].vy = spriteB [i].vy
end

data.saveSettings()
director:changeScene(“menuScreen”,“downFlip”)
–…[/lua]

In the main.lua I have (update from the _G setting above)

[lua]_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}
},

[“spriteB”] = {} – I also tried {x=0,y=0,vx=0,vy=0}

}[/lua]

I am stuck at this time.Most of the time I get a “encoding of userdate: 01E5D218 unsupported” runtime error message. To complete the description, I should say I am using the data.lua module which is used to save and load settings. I am pretty sure that I have problem with saving the spriteB table into gamesSettings.

I have no idea if I made any sense! But if I do, I would really appreciate some pointers on how to add my sprites into spriteB table.

THANK YOU!

Mo

[import]uid: 49236 topic_id: 15517 reply_id: 315517[/import]

@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,

?:slight_smile: [import]uid: 3826 topic_id: 15517 reply_id: 57330[/import]

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:

[lua]_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}
}[/lua]

So in this case I can access each of the element as
gameSettings.spriteA[1] – or [2], [3]

Because I knew that the table will take 3 elements (each having their x,y,vx,vy and health. But if I do not know many elements will be at first, how can I setup the spriteB table to take an “unlimited” number of elements each having their own x, y,vx,vy…?

Can I just start with: spriteB = {} and then add element to it? How? What happen to FOR…END if some elements are no longer available (niled during the game because the enemy is destroyed)?

Sorry to ask you (and other) even more questions! I will look at your suggestion and see what I can come up with.

I really appreciate you taking the time to answer. Thank you.

Mo [import]uid: 49236 topic_id: 15517 reply_id: 57335[/import]

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,

?:slight_smile: [import]uid: 3826 topic_id: 15517 reply_id: 57337[/import]

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,

?:slight_smile: [import]uid: 3826 topic_id: 15517 reply_id: 57342[/import]

Hi

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

[lua]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()[/lua]

So I basically I have a global vaariable which I used to access game settings (like sound_on, score,… and sprites location/speed) Then I used a data.lua (which i found on the forum) that uses JSON to save the _G.gamesettings table. Finally I reload the file at the re-start of the game and do:

[lua]for i = 1,3 do
spriteA[i] = display.newImageRect(“spriteA”…i … “.png”,75,75)
spriteA[i].x = gameSettings.spriteA [i].x
spriteA[i].y = gameSettings.spriteA [i].y
spriteA[i].health = gameSettings.spriteA [i].health
end[/lua]

Hope this makes sense.

WOW, thank you for the links. I was aware of Jon (blog) tables great post about table but I can see that you also wrote some great posts too! I will look at those links and get back here.

THANKS!

Mo

EDIT: WOW, I am reading your tables tutorial and I learned quite of things I did not know. Thanks;)
[import]uid: 49236 topic_id: 15517 reply_id: 57341[/import]

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 [import]uid: 49236 topic_id: 15517 reply_id: 57343[/import]