i have a question about global variables

hello
im kinda new in this game maker thing

and i have a problem with either understanding the global variables or making it work
so if i set up a global i should be able to use that variable everywhere right?

like if i set a variable like this- and also is it possible to do this?
(im not very good with this progamming but i am learning)
im doing this in step btw

GML:
global.interact = keyboard_check_pressed(vk_up);
and then in another object in the step event set


Code:
if global.interact {
sprite_index = off
}
and when i do this i always get the error

''variables not set before reading it'' and now i have googled and fiddled with this all day, and i cant understand why.
anybody who has any idea why?
 

Nidoking

Member
The reason is that the instance running the second part is running the second part before the instance that runs the first part runs the first part. The first part, which should set the variable, is not setting the variable before the second part, which reads the variable, tries to read the variable. You'll need to ensure that something, somewhere, sets the variable to a value before you try to use that value. A Create event or a Game/Room Start event is more likely to run before a Step event than another Step event is, unless you do something to ensure that the instance running the second Step event doesn't exist until after the instance running the first Step event has run the first Step event.
 
The reason is that the instance running the second part is running the second part before the instance that runs the first part runs the first part. The first part, which should set the variable, is not setting the variable before the second part, which reads the variable, tries to read the variable. You'll need to ensure that something, somewhere, sets the variable to a value before you try to use that value. A Create event or a Game/Room Start event is more likely to run before a Step event than another Step event is, unless you do something to ensure that the instance running the second Step event doesn't exist until after the instance running the first Step event has run the first Step event.
thank you!!
 
Top