How to save\load objects without using ids?

Example Scenario:
I have 5 crates in a room. When the player destroys a crate, it's id is saved to an array. When you reenter that room, the array ids are matched against the crates and if the id's match, then the object is destroyed.

This works fine, however, I noticed that as I add or delete crates in the room, the save file breaks because the id changes. I've seen suggestions of saving the room + object_index + x + y, but that would also break if I changed the placement of the crates. Is there another way to identify an object without hand coding a specific variable for each object that needs to be saved?
 

TheouAegis

Member
You need your own identifier. Will there be 5 crates tops? Then make a global variable and save the statuses of all 5 crates in that variable as either a bitmask or an array. Have another global variable counter to keep track of crates spawned in the room. Make sure you set this variable back to zero every time you start the room, or every time you leave the room. When it is time to add a new crate into the room, check if the status variable or array has not been marked as destroyed yet for the current counter value, and then spawn in the new crate, saving the current counter value inside that crate so it can know where inside the global status variable to save its own status when it gets destroyed. Then increase the counter variable.
 

Yal

šŸ§ *penguin noises*
GMC Elder
One approach is to order crates based on something you normally wouldn't change (e.g. sort them left-to-right, top-to-bottom in the room and have that number be their ID, now it stays the same even if you move them around a bit), you still need to save the room somehow though since the room index might change between compilations.
 
Top