Skip to content
This repository was archived by the owner on Feb 19, 2024. It is now read-only.
itevie edited this page Sep 16, 2023 · 4 revisions

Events work similarly to C#, you can create an event, other places in code can subscribe to it, and you can execute all the listeners with on call.

Try live example here https://zephyr.itevie.repl.co/?preset=events.zr&autorun

To create an event:

event [type] [name];

For example, in this page we will use

event int numberChange;

Adding listeners

To add listeners, you can use the following:

// Method 1, using +=
numberChange += func(num) {
  console.writeLine("Got number " + (num -> string));
}

// Method 2, using the Event package
numberChange.subscribe(func (num) {
  console.writeLine("Got number " + (num -> string));
}));

// Directly the same as
Event.subscribe(numberChange, functionHere);

Note: When adding events, they are executed first-come-first-served. So the first function to listen to an event will be the first to be executed.

Removing listeners

Removing listeners is done the exact same as adding:

func b() {}
numberChange += b;

// Method 1
numberChange -= b;

// Method 2
numberChange.unsubscribe(b);

// Method 2.1
Event.unsubscribe(numberChange, b);

Alternatively, you can remove all listeners:

numberChange.removeAllListeners();

Calling an event

To call event, you can either call it like a function, or Event.call()

event int numberChange;
numberChange += func(num) {
  console.writeLine(num);
}

numberChange += func(num) {
  console.writeLine(num++);
}

// Method 1
numberChange(2);

// Method 2
numberChange.calll(2);

// Both will output:
// 2
// 3

Other methods:

Event.getListeners - Gets all active listeners as an array

Clone this wiki locally