Scripting: Events

Skyline is an event driven system and as so provides many event messages for you to use to communicate with other objects or to process the controls your end user is activating. E.g. KeyDown event, Damage Event, Timer event, Game State event etc.
Please see the online Skyline Lua API Events section for a comprehensive list of available events.

How to Use Events

Events in lua are used as a functions in your script, some pass arguments such a value or ID others do not. These functions are triggered as a response to various engine features or user defined events from script or graph.

For example, when the player presses a key on the keyboard the system generates a “Key Down” event. This trigger response calls through to a Lua script by calling a function named onKeyDown(keyPressed). The “keyPressed” variable in the argument will be the key letter/number that the user pressed. To use this event just add the function to your script:

function onKeyDown( key )
    sky.lprint("Key Pressed: "..key);
    -- Do something here

Now any time a player presses a key, this function will be called in as many scripts that contain it. All of Skylines Events work in this way, to receive an event just add its function.

Some events only become available it an specific action is attach such as with the Damage Action. This action calls the function ds_onDamage( health, damage, attackingID ) when health is lost.

Why use postInit()?

A bit of advice for when setting up variables at the start of your game.

The onInit() Event is called when an object is initialized in the scene. There are times when you may want to access other objects data, such as the players ID. You would think that the onInit() would be a good place to do some variable setup.

For example if our object wanted to know what the players ID was.

playerID = entity.getIDFromTag("Player");

But imagine if a player was added to the scene after this object, our current object would not know that the player exists, but you will still be trying to get its ID. In this case we would get playerID = -1 not what we are looking for. As you can imagine this could cause a bug in your game! To get around this we use the function postInit()

function postInit()
  -- Set up Vars
  playerID = entity.getIDFromTag("Player");

This postInit() Event is called once all of the scenes objects have been initialized. So now if you call for the Players ID it will definitely exist?