- gameConfig *
You can use these variables to make more powerful programs. These variables are called "reference variables" because they point to (or "reference") other objects.
You also have variables for each of the objects that have been added to the game, e.g.
(These names will be based on the names you have given your objects)
* The "gameConfig" variable is the default name for the Game Config object.
If you changed the name, then use whatever name you chose, e.g. "myGameConfigObject".
"object" is a variable created for the current game object.
For example, if you are editing a Sprite that has the name "sprite1", then the variable "object" is just a reference to this Sprite.
Try it out
- Create a Sprite.
- Edit the Action and replace the code with the following snippet:
alert(object.name + ' has been created.');
- Save the item
As soon as you save the item, you will see the popup alert message:
Why does the alert message appear when you save the item, not when you click "Play Game"?
The reason is that in Game Maker, whenever you save an item, the game is relaunched in the background.
This means that the game is actually running before you click "Play Game". It's just paused in the background.
"item" is a variable created for the current AppShed Item. This is part of app.js.
For more info, search of "Item Object" in app.js documentation.
"data" is a variable that contains any data properties for the current item.
If there is no data for a particular object it will look like this:
"gameConfig" is a variable that contains all the properties defined in the Game Config object.
This can be very useful in more advanced games when you want to know what the configration of the game is.
For example, to create a rectangle that is half the width and half the height of the game, use:
var rect = this.add.rectangle(160,225, gameConfig.width/2, gameConfig.height/2 , "0x7A7A7A");
All other game objects will be available using their unique Object Name.
e.g. "sprite1" or "collider1" or "audio2" e.g.
For example, if you have a Sprite called "sprite1", then you can use the variable in your code like this:
alert(sprite1.name + ' has been created.');
Have you noticed that this code is very similar to the code for "object" above?
In fact, when you are editing sprite1, then the variables "sprite1" and "object" are identical.
It's probably better to use "object" so that if you decide to change the name of the object (e.g. "newSpriteName"), the code does not break. Any code using "sprite1" will not work after you rename it.