4

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?

2 Answers2

5

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

KevLoughrey
  • 591
  • 3
  • 12
0

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.

Gnemlock
  • 5,263
  • 5
  • 28
  • 58
Tanner H.
  • 111
  • 1
  • 4