4
\$\begingroup\$

I've been making my way through several tutorials on GameMaker: Studio at this point, and I've been trying to keep track of various aspects of the code used in them. One thing, however, confused me.

I have noticed that it's possible to define new variables in GM:S either with or without using the var keyword. I'm aware that you can use the var keyword to define multiple variables in one line, for example var xx, yy; (and, of course, defining null variables), but other than that, is there a particular reason why some variables don't need to use var? Is it all to do with functionality, or what? When, in that case, should I use var other than the example provided?

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

Variables declared with the var keyword are local variables, whereas variables declared without var are instance variables. More information on scope: https://yoyogames.com/tech_blog/41

\$\endgroup\$
0
\$\begingroup\$

If you say var test = 0, it is a local variable, and will be deleted at the end of the event.

If you say test = 0, it will be stored in that object until the game ends, or the object is deleted.

If you say globalvar test = 0, it will be stored until the game ends, and will not be deleted with the object. It can also be accessed in another object without typing otherObject.test.

\$\endgroup\$
1
  • \$\begingroup\$ Please do not use offensive language in your answer. \$\endgroup\$
    – Gnemlock
    Oct 2, 2016 at 3:54

You must log in to answer this question.