Reading global variable

W

Wolfarts

Guest
My save/load system was planned with a few functions extra to make the game interaction more interesting, one of this functions is to variate the main menu background when a global variable has reached a certain value.

I coded all the fuctions in the creation code of the main menu room, but when the game opens it says that the variable has not being set before reading it
 

TheouAegis

Member
Any references to that variable being read cannot occur until after your main menu room has properly loaded. Are you sure you are not trying to reference that variable prior? Typically, you should initialize any and all Global variables in a loading room at the very start of the game before the main menu is even access. That's just a safety precaution.

FYI room creation code is run after instance creation code.
 
W

Wolfarts

Guest
Any references to that variable being read cannot occur until after your main menu room has properly loaded. Are you sure you are not trying to reference that variable prior? Typically, you should initialize any and all Global variables in a loading room at the very start of the game before the main menu is even access. That's just a safety precaution.

FYI room creation code is run after instance creation code.
making a loading screen would help, but the "not set before reading it" message would still display
 

TheouAegis

Member
No, you would initialize the variable to its default values. You would still need to have a system in place that would read the save data in the main menu. It just could not be in the game start event. The preloader room serves to initialize all variables to the default value so that you never have an unset variable error.

Or you check if the save file exists and set the variables to the saved values if it does, otherwise you set all the variables to their default values if it doesn't. If your save file is just an ini file, set variables to the saved valueif the key exists in the file, otherwise set it to the default if the key does not exist.

Either way, your issue is you are trying to perform a read operation on a variable that is still waiting for its initial write operation. Make sure you aren't using += or -= in your save file loading.
 
Last edited:
W

Wolfarts

Guest
No, you would initialize the variable to its default values. You would still need to have a system in place that would read the save data in the main menu. It just could not be in the game start event. The preloader room serves to initialize all variables to the default value so that you never have an unset variable error.

Or you check if the save file exists and set the variables to the saved values if it does, otherwise you set all the variables to their default values if it doesn't. If your save file is just an ini file, set variables to the saved valueif the key exists in the file, otherwise set it to the default if the key does not exist.

Either way, your issue is you are trying to perform a read operation on a variable that is still waiting for its initial write operation. Make sure you aren't using += or -= in your save file loading.
and how do i do that?

Plus, i use game_save for saving
 
Last edited by a moderator:
Top