Skip to main content

Examples

Standard GMCP Events

Player enters the room

The following is an example of a basic function tied to a GMCP event. This will wave to any player that enters the room. An example of the GMCP message from Nexus:

GMCP received from server
[GMCP]: Room.AddPlayer {"name":"Khaseem","fullname":"Khaseem"}

note: Room.AddPlayer is sent by Nexus but is not a core server GMCP message

With GMCP messages thse are raised automatically by eventStream as received. This event/listener will fire every time a player enters the room.

const greetPlayer = (args) => {
// Wave hello to the player who entered the room.
nexusclient.send_commands(`wave ${args.name}`);
};
eventStream.registerEvent("Room.AddPlayer", greetPlayer);

NPC enters the room

GMCP received from server
[GMCP]: Char.Items.Add {"location":"room","item":{"id":530669,"name":"a tiny blue quail","icon":"paw-claws","attrib":"m","parentid":"room"}}

Using the same style of code from the previous example we can make everyone else aware that we see the NPC. In this example the GMCP received is a bit more complex so we'll need to do some additional checks.

const pointNpc = (args) => {
// Char.Items.Add will fire for both the Room and your Inventory.
//First lets check if this GMCP message is for an item in the Room.
if (args.location === "room") {
nexusclient.send_commands(`point ${args.item.id}`);
}
};
eventStream.registerEvent("Char.Items.Add", pointNpc);

Custom event

Any number of events can be created. The only requirement is a unique event id.

Target Shielding

const shieldNotice = (args) => {
if (args.id === GMCP.Target.Text) {
nexusclient.display_notice("MY TARGET HAS SHIELDED!!!!");
}
};
eventStream.registerEvent("targetShield", shieldNotice);

Then in a Nexus trigger with a pattern like ^A nearly invisible magical shield forms around (.*)\.$ You could have snippet of code for

eventStream.raiseEvent("targetShield", { id: args[1] });

Now, someone may ask why not just place the code directly in the trigger? What purpose is the event serving here?

Both are viable paths. One benefit of an event handler in this scenario is any number of other packages could add on to the targetShield event. You may have a bashing package that cares when a target shields, but also a pvp package that cares as well. In a completely separate package you could add another listener:

const razeTargetPVP = (args) => {
nexusclient.send_commands(`queue addclear free raze ${myTargetVar}`);
};
eventStream.registerEvent("targetShield", razeTargetPVP);

Using this basic example when someone shields in your room (the trigger we previously noted that raises the event targetShield) both of the event listeners we have attached razeTargetPVP and shieldNotice will fire. Resulting in a display_notice and an attempt to raze the target.

Packaged events

Many packages, like nexMap, nexGui, nexSys, etc. create and handle a variety of custom events that users can hook into. nexSys for example will raise events for things like afflictions, defences, items, etc.

Check with the various packages for details.