Loading sprites stored in table with pd.datastore.read()

Hello, I am learning how to save and load my game for playdate. I have my head mostly wrapped around it, but I am struggling with retrieving my sprite information when reading the pd.datastore. Developing on Windows in Lua.

Basically, I have one large table which represents a building in my game. Every slot of the table is a specific room that has been placed, which contains information such as the sprite and the type of room it is.

The problem appears to be that in my game, the "sprite" key's value is a gfx.sprite. When the game data is saved and read again, this value becomes a table. I can't access my sprite's information after reading the information.

Please see below the screenshots showing the tower table in game, follow by the tower table in the game_data. This shows how the sprite's information has turned from a gfx.sprite to a table. How can I fix this?

--tower table in game

--tower table in game_data for reloading

Thanks

Oh I wouldn't try to store the actual sprite! just store an ID or the name of the sprite if you have to. looks like you already have a value 'type' when you load, look at what the type is. if type == home then load the home sprite images.

Is there a reason why you wouldn’t store the actual sprite? Is it possible at all?

Unfortunately “type” is not specific enough to pull the right sprite on its own, as I have multiple sprites for each category (eg small apartment and large apartment are both “home” types). I suppose I could add another specific identifying variable for the sprite to use as well. But ideally I’d like to use the information that’s already stored and accessible in game, rather than having to re-create it on load.

It seems like a lot of duplicated data to store no? lots of images that you already have being stored in your save data file. I think regardless of how much you store you will need to create sprite:new() and the load any information/state back into that sprite. the images are harder, because it's not guaranteed that the same image data will be loaded at the exact same pointer location.

Hmmm that makes sense. I was thinking remaking every room’s sprite one-by-one and moving it to the correct location on load would be kind of a lot of work to do, but on the flip side I understand not wanting to junk up a playdate’s limited memory with unused copies of room sprite images (even if they are only 1-bit 16x16/32 sprites). I’m just kind of surprised it seems there is no way to read or convert all the existing information in the sprite hash?

Depending on how the rooms are put together. Storing only the changes from the game's starting point can be a good way of only needing to update a few things.
Also totally depends on how your game works.

I think I recall someone mentioning that some of the sprite data might be on the C side? I'm 95% sure that hex number you're looking at isn't a hash, but a memory address.

1 Like

That makes sense! I am basically saving the entire “tower” table as it exists in the game into the save file, which can include a ton of default information. It would save me a lot of looping on load if I only had a list of the table positions that have non-default information.

Ah, well, navigating between Lua and C for a memory address is beyond my knowledge at this time. I am still fairly new to programming/game dev in general. I think I’ll stick with saving only changed tower data and generating new sprites on load.