Tutorial - Entity interactions – Ylands
This is the first one in the series of scripting tutorial scenes showing how you can make your game interactive through scripting. The first scene shows how easy it is to perform actions in your game by using interactible objects.
NOTE
Before you look at how the particular scenes work, run them first by pressing "Test game" button (under Test) or by pressing F9 key. This will run the currently open game in the test mode so that you can actually play it and interact with its objects. You can leave the running game any time by pressing ESC key and selecting "Return to the editor" (under "Leave game").
Scripting basics
Before we get to describing this particular scene, let's go over some basic aspects of scripting in Ylands.
Scripts are attached to objects in scene.
These objects are of two types:
- entities - solid objects you see in the game ( a tree, table, grass, wolf etc.)
- Game Logic objects (GL) - these are special objects that perform certain actions (emit particles, play sounds, detect player or other entity entering them and much more)
NOTE
Any entity can have a script attached unless it is a group of entities or a stackable entity. If you need to attach a script to a stackable entity, you must set it first as unique in its properties.
Object's script can be added or edited by pressing the green button at the bottom of its properties window (or by double clicking an object while holding Left Shift key). Scripting in the editor is actually a "visual scripting". You place and connect together various tiles representing actions and events which are then processed by the game.
There two main type of tiles:
events
Event is an "entry point" into a code. It is executed when a specific action happens with the parent object (the one to which the script is attached) or even when something more generic occurs. To these events are attached instructions which are executed from top to bottom after this event "happens".
instructions
Each instruction is tile that performs an action. Usually you need to provide it with various parameters to specify what exactly you want it to do or with what object.
In this scene there are three scripted interactions:
The Lever
When you switch on or off the lever, the door next to it will either open or close. That is done by this simple script attached to the lever, which contains two events:
On switch on
this event is executed when the lever is switched on. When that happens, it either opens or closes (based on its current state) the door we selected via eyedropper tool present within the open/close instruction tile.
On switch off
this event does the same action as the one above but it is triggered when this lever is switched off.
We created this very simple script by drag and dropping these tiles from their respective categories on the left side (Events and Entities/Actions) and attaching them to each other. Then we selected the door in the scene.
The Door
There are many more ways to interact with objects then just by using a lever. You can use any events to trigger any other action. In this case opening or closing the door lights or extinguishes the torch.
Look again at the events we set up in the door's script and you'll immediately see what it does.
If you open the Events section on the left, you will be presented will all the events this object (Wooden door) supports. The On Open and On Close events are shown in black because they are already used in the script. Note that every event can be used only once in each script.
The Golden Penguin
When you pick up the golden penguin a penguin statue will appear in front of you.
The script behind looks a bit more complicated, but there's no need to worry - it can be easily explained.
- The golden penguin has a script with an event that is executed when someone picks it up (when the pick action is finished, to be exact).
- At that point it spawns an entity we chose from a list (Penguin statue) at the specified position and with the specified rotation.
- When you put the Spawn entity tile in the script, you will notice that that it requires you to provide coordinates for its position.
- What we did here is that we use one instruction to provide the values another instruction requires.
- Because the Get position instruction gives us position coordinates, we place a dummy object (Game logic object called Reference point) where we want the statue to spawn, read its position by using Get position instruction and use the result as the position where the penguin statue will be placed. If this, at this point, is too difficult for you to understand, don't worry about it. You can always just type in the values manually.