• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Variables not working?

A

AnotherDoor

Guest
Okay, I have one object with only Create and step event
(it has also speed declared in create after the "testvar = 2;")

Create event: testvar = 2;
Step event: image_angle += testvar;
"Variable not set before reading it"

I redo step event: show_message(testvar);
"2" after 5-7 times of pressing OK on the pop up it says the same error
"Variable not set before reading it"

I redo the whole thing using only global variables
Create event: global.testvar = 2;
Step event: image_angle += global.testvar;
Yayy, it works! (Also it works with the show_message when set to global)

But why only after I changed them to global ? Both of the events and their scripts are inside of the same object ? I need multiple objects with their own testvars so I can't use global. So why does not the variable work as intended?
Any help is appreciated!
 

FrostyCat

Redemption Seeker
Is there anything else like these in your code?
Code:
var testvar;
Code:
var testvar = 2;
var declares temporary variables that disappear at the end of the piece of code. It will cause "variable not set" errors if those disappeared variables are referenced again later. Remove the var from any variable declaration for instance-scope or global-scope variables.

See: Variable Scope
 
A

AnotherDoor

Guest


@FrostyCat
See for yourself. Not a single var prefix there. Also don't mind the child object cause 1. they are disabled 2. the error came from this specific object
Still can't figure out what causes the error.. I hope you see something and help me.
 
W

whale_cancer

Guest
Are the errors occurring in your child objects or in the parent? Please post the text from the error message completely.
 
A

AnotherDoor

Guest
@FrostyCat @whale_cancer Okay, whale you are right. First it was the parent object causing the crash but now that I look it has changed from it to the child objects and now I know how to fix it : )

Thank you both for your time.
 
Top