Advertisement
Guest User

Variable Scope

a guest
Jul 6th, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. There are only three scopes for variables in GM:
  2.  
  3. Constant - A constant holds a value determined before you compile the game, listed in the IDE itself. You cannot create or destroy a constant in game. Constants are **read-only!** Constants can be accessed by any object or script in the game with the following syntax:
  4.  
  5. if (someLocalVariable == tau) // for this example, 'tau' is a constant set to 6.283185307179586476925286766559
  6. { show_message("someLocalVariable is equivalent to 2 x 3.1415926535897932384626433832795!"); }
  7.  
  8. Global - A global variable holds a value determined whenever you create it for the rest of the program's run time. You can only create global variables. Global variables are fully accessible in game. Global variables can be accessed by any object or script in the game with the following syntax:
  9.  
  10. global.thisIsAGlobalVariable = 4; // for this example, 'thisIsAGlobalVariable' and 'points' are global variables
  11. global.points = 0;
  12. global.points += 7;
  13.  
  14. Local - A local variable holds a value determined by an objects code for a single instance of that object. You can only create local variables. Local variables are fully accessible in game. Local variables can be accessed by the calling instance with the following syntax:
  15.  
  16. thisIsALocalVariable = 0; // 'thisIsALocalVariable', 'index', 'someOtherLocalVariable', and 'anotherLocalVariable' are all local variables
  17. index = 42;
  18. someOtherLocalVariable = thisIsALocalVariable+index;
  19. anotherLocalVariable = "Hello World!";
  20.  
  21. Local variables of another currently existing instance can be accessed by prefixing the instance's unique ID with the following syntax:
  22.  
  23. ball = instance_create(room_width*0.5,room_height*0.5,obj_ball);
  24. ball.size = 32;
  25. ball.image_speed = 0.2;
  26. with(ball) // Here, nested in a with statement, we can again access to our own variables using the 'other' keyword. If we didn't prefix doneMakingBall with 'other.' the instance we created earlier, 'ball', would instead set doneMakingBall to 1!
  27. { other.doneMakingBall = 1; }
  28.  
  29. Well, I hope that clears up any issues with scope! Good luck!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement