VaelisDocs
v1.0.0
Scripting · v1.0.0

State & messaging

Build lightweight state machines and decoupled entity communication.

View as Markdownllms.txt

#State machine

Every entity gets this.state without a dedicated component. current starts as "default"; previous is the state before the last transition. change(name) is a no-op when the name is already current.

#State callbacks

js
function onStateEnter() {
  if (this.state.current === "chasing") {
    this.animator.play("Run");
  }
}

function onStateUpdate(dt) {
  if (this.state.current === "chasing") {
    // chase logic
  }
}

#Messages

js
function onStart() {
  sendMessage("Enemy", "takeDamage", { amount: 10 });
}

function onMessage(message, data) {
  if (message === "takeDamage") {
    this.hp -= data.amount;
  }
}

#When to use messages

Messaging is useful when entities should react to an event without knowing another entity’s exact implementation. Use findById/sendMessage(entity,...) when you need one exact target; use a tag when you intentionally want a group.