This just logs the built-in runtime
variable to the browser console. Preview the project, click the button, and check the console. You should be able to expand it and see all sorts of properties and methods.
The built-in runtime
variable is a class named IRuntime
. Every property and method it has is documented in the Scripting section of the manual. You can find the IRuntime reference here, which covers everything you can see in the console. It's a good idea to refer to this documentation to check what properties and methods are available and how to use them. However looking at things in the console is also a quick way to explore what something is and what it can do.
Objects and instances
All the objects in your project - that is, everything under Object types in the Project Bar - are represented under runtime.objects
. For example, runtime.objects.Sprite
represents the Sprite object type in the Project Bar.
It's important to note that runtime.objects.Sprite
refers to an object type. This is different to an object instance. If you have 10 instances of a Sprite, then there is still only one Sprite object type, and there are 10 instances. The object type has properties such as the name of the object, and the instances have properties like a position and size. A common mistake is to mix them up and try to access the position of an object type. But object types don't have a position - only instances do.
So if we want to get or change the position of a sprite instance, we must first get the instance of the Sprite object type that is already in the layout. If there's only one, the easiest way to do that is to call getFirstInstance()
on the object type, i.e. runtime.objects.Sprite.getFirstInstance()
. That then returns an instance with a position, size etc.
Change your JavaScript code in the event sheet to the following:
let inst = runtime.objects.Sprite.getFirstInstance();
inst.x += 10;
This will get the first instance of the Sprite object type (the one already placed in the layout) and store it in a variable inst
. Then it adds 10 to the X co-ordinate of the instance, which will move it to the right. Try previewing the project and clicking the button - the sprite instance moves to the right!
Documentation
Once again it's useful to refer to the documentation and see what we've used.
runtime.objects.Sprite
is a class named IObjectClass (so named as it is also used for families, i.e. "object class" means "either an object type or a family"). In the linked documentation you can find that the method getFirstInstance()
is listed and documented, along with various other properties and methods.
The returned instance includes the properties and methods of IWorldInstance (so named as objects appearing in the layout are part of the game "world", whereas things like a Dictionary or an Array object are not). In the linked documentation you can find the properties x
and y
are listed and documented, along with various other properties and methods.
We're highlighting the documentation as it's an important reference to use when finding out what Construct features you can use from JavaScript. The editor will often try to autocomplete names as you type, but since JavaScript is a dynamic language it's not usually able to tell what properties and methods are actually available, and so it will tend to suggest everything. To know what is really available and what you can actually use, you'll need to refer to the documentation. After regular use you'll start to remember what the common properties and methods are and not need to keep looking them up.