'Variable not set before reading it' but it is set

M

MMGames

Guest
Dear people,

I wonder if any of you can help me with the following. I have a game initiator object that will assign a value into a global variable. Then that same object will create a new instance of another object, which based on the previous value will be created in one or other way.
The strange part it says "the variable not set before reading it" but it's set right before creating the instance.

I am using object development not GML. Can any of you help? I am sending screenshots.

Initiator object:



Object which instance we create:



Full error message:

 

rIKmAN

Member
Forgot to say that in the second part, where I create the event, the variable gender it's making reference to the global variable set in the previous object: https://imgur.com/7f8ffaaa-ead0-4b7d-814a-35d53f1e245b
Does DND have a different block for accessing global variables?
If not try putting “global.gender” into the variable check.

You can see in the error message that it’s converted to using a with() statement for some reason which means it’s in the scope of obj_iniciadorJuego which doesn’t have a gender instance variable.

Also, I’m not sure of how DND converts into GML but although it’s failing due to the variable scope mixup, the actual line of generated code in the error looks wrong too: var l058E3ADD_0 = gender == 1;

Are you sure that’s a screenshot of the full Create Event?
I can’t see anything that would be converted into a
with(iniciadorJeugo) statement in that screenshot of obj_maincharacter.

 

TheouAegis

Member
You need to put global. in front of the variable when trying to read it. GMS2's only explicitly "global" actions are Set Global and Get Global, the latter of which requires you to create a temporary variable to hold the contents. Thus, the only way to explicitly include a global variable inside a conditional like "If Variable..." is to prefix the variable with the global. keyword.

What you could do is give the maincharacter his own gender variable and use Get Global to assign global.gender to gender; just make sure the focus of Get Global is set to self.
 
Top