What is the best way of storing level states

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By peterhoglund

I am doing a game where you can go back and forth between rooms. In the rooms, there are items that you can pick up/consume/open etc. What is the best way of storing the states of these items when I exit and then enter that room again?

I have my levels set up like this:
• GameScene
└ • CurrentLevel
└ • Level1-Room1
└ • FixedObjects
└ • Player
└ • Camera

Then when I walk through a door I fade to black, swap the scene under CurrentLevel and move the Player to the right position. However, unloading and loading the scene also reset all items.

How should I go about to save the states of the Level scene? I’ve tried to find a good solution for this online, but I almost only come across how to save the whole game to file and then load it again, which is not really what I want. But perhaps this is the same thing? Do I have to do a proper save file and save the game, it feels a bit overkill for only saving states of items. I rather save the states somewhere, and then when it is time to save the game, save that list of states. But I’m not sure this is a good idea or not.

:bust_in_silhouette: Reply From: dustin

there are a couple of ways i thought of from the top of my head that could fix your problem.

  1. saving item states in nested dictionaries

you could basically make a dictionary of every room and in the room dictionary slot make a nested dictionary for the items, and finally add an array for every item (for the position and extra values etc)

then, everytime you change rooms, call some function deletes all previous items and instances items depending on which room it is, what item it is, etc. (i cant make a mockup i got no time sorry)

2 . changing your whole room-changing system overall

you could make it so that instead of the rooms being seperate scenes that changes every room switch, you could just instance all of the rooms in one node and showing the current room and hiding every other room.

and in every room node, make the items a child of the room node so you dont have to worry about the positioning and the visibility.

this is a simpler approach, but may impact performace.

Thank you for your answer! I’ve been considering your option 1, to build dictionaries of all items in a room that needs to be saved. However, it seems there will be a lot of manual tweaking of what I want to have stored. Is it possible to store the Node as an object in a dictionary and then add it back to a scene, without having to instance it? The idea then is I store the already instanced Node in the dictionary and all parameters for the node will come for free. I’m trying this but can’t get it to work so I’m guessing there is something in this I don’t understand.

peterhoglund | 2020-07-07 15:57

i dont think its really possible to save a nodes data and its childrens data automatically. also, you’re thinking of a node as a data type that can be stored, but it isnt. the only way to do that (atleast that i know of), is to do the option 1.

dustin | 2020-07-07 16:02