Events
A mod's GML and its JS talk to each other with events. Each event has a name and an optional payload.
// Create a GM object
mod.resource.object.create({
name: "main",
events: {
// Send messages to JS in anywhere in GML
step: ({ send }) => `
if (keyboard_check_pressed(ord("H"))) {
${ send("ping", "room_get_name(room)") }
}
`,
},
// Listen to JS events in GM
receive: {
pong: ({ payload }) => `
show_message("Pong received in room " + ${ payload });
`,
},
});
// Listen to GM events in JS
mod.on("ping", (room) => {
console.log(`Ping received from room ${ room }`);
// Send events to GM
mod.send("pong", room);
});
Pressing H sends "ping" from GML to JS, with the current room name as the payload. JS logs it and sends "pong" back, which the object's receive.pong reacts to.