Load game crash when player do something like attacking

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

I just try to make a save system based on the save system of the doc but the thing is when

I load the save the music stop, the ennemies do not spawn , the player can not attack ,etc

I put the dictionnary in the player node but not all of the player variable are in the player

script . So I do not know how I am to do it in this case.

Thank you for helping me!

:bust_in_silhouette: Reply From: Sween123

There can be many possible problems when we do a save system, since it’s not just about saving basic stats like HP or something.
For a complex game world, a save system may get a little bit complicated. So, for saving basic informations, like HP, we use json. These information can be stored in a dictionary, and when we save the game, we simply save the dictionary into a file. When we load, we update the infos inside the dictionary according to what is saved inside the file. This is the simpler part. However, we can make the dictionaries be more useful for saving more complicated infos. Example: {"Info_Type": "Character Attribute", "Character_path": PATH, "attribute": ATTRIBUTE}
For saving other things like players position, enemies killed, current level of the scene, spawing points, etc. These kinds of infos can actually also be saved into a dictionary or array.
For example, for the enemies that are dead, when loading the game, we want them to be deleted so it will be just like the time before when you saved the game. We can use a array like: dead_enemies, whenever you killed an enemy, append something like this into the array: {"Info_Type": "Dead Enemy", "Node_path": PATH}. When save the game, save each of them into your file. When load the game, using for loop iterating every line, if Info_Type is “Dead Enemy”, use get_node(LINE.Node_path).free()
SImilar to other different situations. When save, we can use one more layer that contains extra infos that help to identify what is being saved instead of just a dictionary of attributes so it will be convenient when load.
For a character, use a dictionary to save all the attributes and status instead of tons of seperated single variables so it will be good when it comes for saving the game. Also, try to have a function save() for each objects you want to save (You may want to use “group” to keep in track of which objects are the ones you want to save) and a function save_game() that collects all the saved infos returned from the functions save() from all the objects in the group.
For things like attacking, just use variables to decide current status, and use variables of boolean or string to determine what characters can do. This will help to change status and update things, also it will be convinent for saving since they are variables and simple variables can be saved into a dictionary.

just one small question, what will happen if I put my dictionnary for save in evry scene I need but I just change the thing it will save ?

thoma | 2020-03-18 15:17

For the scenes you want to save, you need the kind of function like save() that packs up the infos you want to save as a dictionary, it’s not just the dictionary like attributes for a character. So, each time the game is saved, the dictionary returned from save() are new, keeping up with the newest infos. So definitly the change of values inside the dictionary is allowed, because that’s what save does.
If you mean the change of code inside the function save() where you want to change how infos are packed like changing Info_Type or other extra infos that help to identify the gaming infos, you can. What will happen depends on how you change it. If you are not changing it into something that your code for loading doesn’t understand, and as long as it’s correct for your game logic, it’s fine.

Sween123 | 2020-03-19 00:20