Tutorial - Event listener – Ylands

From Bohemia Interactive Community
Jump to navigation Jump to search

We have seen that we can trigger actions when we interact with entities. We can make something happen when we, for example, toggle a switch or open a door. But there is yet another way to trigger actions - via Event listener.

Usually there are many things happening in the game that don’t have anything to do with you, the player. Entities are being created and destroyed. Time changes, fires go out, objects are hit and much more.

We can tell an Event Listener to listen for specific events like that. And when that event is detected, the Event Listener executes its own, specific event in its script. Sounds complicated? It really isn't, let's look at the example scene.


The Bananas

  • Here the Event Listener is set to detect, when any entity of banana type is created.
  • When that happens the On Create event is executed in its script and the Target entity (the entity that performed the action we detected - in this case the banana that has been created) is painted green.
  • Because the Execution limit is set to -1, meaning that this will happen as many times as this is detected, every banana that is created gets painted. If we set it to, say, 2, only the first two bananas created would be colored.


Note:

You may find it confusing that you see the bananas in the editor and yet we talk about them being “created” when the game starts. Note, that in the editor we just tell the game where objects should be and when your game starts it needs to create all those objects into the newly started scene. Some objects are created right at the game start, other may be created later - from the script (just like we spawned the Penguin statue in the Entity interaction example).


The Animated Arrow

Here we have an animated arrow (you don’t worry about how it’s animated at this point) moving between two Game Logic objects used to specify position and/or rotation, called Reference Points. The Event Listener here is set up so that it detects when the animation loop is finished and paints the arrow with one of two colors.


The Dummy And The Sword

Here the Event Listener detects if a specific object has been hit and if it has been killed (destroyed). Please note that we could have done this also by utilizing the dummy’s own events - the entity itself can detect if it is destroyed or damaged.


Note:

Many times something can be done in more way than just one. Consider this: You want something to happen when an entity is picked up. You can do this via the entity’s On Picked event. You can have an Event Listener detect this. You can have it inside a Trigger Zone and have it trigger when this specific objects leaves. All those are valid ways of achieving one goal.


The Jumping

The next listener is an interesting one. It shows that you can easily set it so that it doesn’t detect only interactions between two entities but even actions that players perform on their own. In this case we detect whether the players have jumped and if they do a specific event is executed.


However, this example shows yet another important feature. We don’t want the event listener to detect player jumping anywhere in this scene but only when they are in front of the signpost. Because of that the Event listener Game Logic object is disabled by default so it doesn’t actively “listen”. When the player enters the trigger zone in its script’s event On Trigger Entered we enable the Listener Game Logic object so that it starts "listening". When the player leaves the zone, we disable it. Enabling and disabling Game Logic objects in the runtime is a very powerful and important feature.


The Energy

Players can create energy links while holding the Energy linker. In this case we use the Event Listener to detect when a link has been made.


The Campfire

The last Event Listener is set to detect a specific interaction of player with a campfire. When the campfire is lit by the player, the event inside Event Listener is executed.