Skip to content

Events Scripting

Igor Budzhak edited this page Nov 28, 2023 · 1 revision

Events scripting module gives you ability to run code at different game events. Scripts you can write here is more complex and they are not as fool proof as in another scripting modules.

To run your code when event occurs use JavaScript-like syntax to pass a function as argument. Example for TickEnd event:

events.onTickEnd(() => {
    // your code here
});

I will call such function event handler.

When you save this script it runs once and attaches your event handlers to game events. In general there is no point to do anything else here besides attaching handlers by using events API. And it is a bad idea to call events API from event function code:

events.onTickEnd(() => {
    // don't do this
    events.onPlayerAdded(() => {
        // something
    });
});

If you want to attach to multiple events, you do this one by one. You can also attach multiple handlers to the same event. Example:

events.onHandleKeys(() => {
    if (input.isKeyDown("C")) {
        zoom.start(10.0, 0);
    } else {
        zoom.stop();
    }
});

events.onTickEnd(() => {
    // code 1
});

events.onTickEnd(() => {
    // code 2
});

events.onPlayerAdded(() => {
    main.systemMessage("#44A1FF", currentPlayer.getName(), "#FF6B86", "entered visual range");
});

events.onPlayerRemoved(() => {
    main.systemMessage("#44A1FF", currentPlayer.getName(), "#FFE659", "left visual range");
});
Clone this wiki locally