Create, Compete & Win at Redbrick Connect 2024! 🎉
Text codingGet StartedEvent Functions

Event Functions

Provides guidance on event functions.

Event Function is a predefined function to run in a particular event situation. A means to allow a user to execute a script that is defined when a new user comes in, or when a keyboard or mouse input is executed.

Start()

const box = WORLD.getObject("BOX");
 
function Start() {
  PLAYER.onCollide(box, () => {
    box.kill();
  });
}

Update(dt)

It runs repeatedly every frame.

  • dt : deltaTime. time interval between frames, in seconds.
function Update(dt) {
  this.position.y += dt * 2;
  if (this.position.y >= 10) {
    this.position.y = 0;
  }
}

OnKeyDown(event)

Runs when the keyboard key is pressed.

  • event : Keyboard event. You can tell which key it is by event.code or event.key.

reference

function OnKeyDown(event) {
    switch (event.code) {
        case "KeyQ":
            changeWeapon();
            break;
        case "KeyR":
            reload();
            break;
        default:
            break;
    }
}
 
const changeWeapon = () => {...}
 
const reload = () => {...}

OnKeyUp(event)

Runs when the keyboard key is (pressed and then) released.

  • event : Keyboard event. You can tell which key it is by event.code or event.key.
function OnKeyUp(event) {
    switch (event.code) {
        case "KeyQ":
            doSomething();
            break;
        default:
            break;
    }
}
 
const doSomething = () = {...}

OnPointerDown(event)

Runs when the pointer is pressed.

  • event : Pointer event. Left-click/right-click via event.buton, and the coordinates via event.clientX and event.clientY.

MouseEvent: button property

💡

Pointers include mouse, touch, touch pen, etc. The event.pointerType tells you the type of pointer.

PonterEvent: pointerType property

function OnPointerDown(event) {
    if (event.button === 0) {
        fire(event.clientX, event.clientY);
    }
}
 
const fire = (x, y) => {...};

OnPointerMove(event)

Runs when the pointer is moved.

  • event : Pointer event. The coordinates via event.clientX and event.clientY.
function OnPointerMove(event) {
  // Similar to OnPointerDown
}

OnPointerUp(event)

Runs when the pointer is (pressed and then) released.

  • event : Pointer event. Left-click/right-click via event.buton, and the coordinates via event.clientX and event.clientY.
function OnPointerUp(event) {
  // Similar to OnPointerDown
}

OnJoin(clientId, avatar)

Runs when a new player joins. When used in Client Script, existing players run the OnJoin function on new players when new players join, and new players run the OnJoin function on all existing players, including themselves.

  • clientId : the clientId of the newly joined player.
  • avatar : Avatar of a newly joined player.
ℹī¸

clientId: a unique id used to identify the client (player).

const playerAvatars = {};
function OnJoin(clientId, avatar) {
playerAvatars[clientId] = avatar;
}
 

OnLeave(clientId)

Runs when the player has left.

  • clientId : The clientId of the player who left.
const playerAvatars = {};
 
function OnLeave(clientId) {
  delete playerAvatars[clientId];
}

✅

The following Event Functions are available in Server Script and Client Script.

TextServer ScriptClient Script
Start✅✅
Update✅✅
OnJoin✅✅
OnLeave✅✅
OnKeyDown❌✅
OnKeyUp❌✅
OnPointerDown❌✅
OnPointerMove❌✅
OnPointerUp❌âœ