Have any questions?

+44 (20) 3540 6226

hello@appshed.com

Reference Variables available in JavaScript

Within the JavaScript code editor you have access to the following variables:

  • object
  • item
  • data
  • 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.

  • image1
  • sprite-abc
  • my_own_function

(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".

myGameConfigObject

object

"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:

alert sprite1 created

 


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

"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

"data" is a variable that contains any data properties for the current item.

When you edit the properties of an object, you might see a collection of data properties. These will be available in JavaScript using the "data" object, e.g.:

alert(data.rows)

data variables

If there is no data for a particular object it will look like this:

data variables none

 

gameConfig

"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");

 

<other_game_objects>

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.

 

  • Thursday, 23 May 2019

GET IN TOUCH

 

   Get In Touch   

appshed name white text 250

Copyright © 2022 AppShed Limited

AppShed Support
Customer Support

We are online and available to help teachers/educators using AppShed. How can we help you today?

Chat, call or email support@appshed.com