Scripting
Scripting Overview
How scripting works in Vibe Games.
How Scripts Work
Scripts bring your game to life. They run inside the game engine when you press Play and have access to the scene, input, physics, and more through the context object (ctx).
Lifecycle
- init(ctx) — Called once when the scene loads. Use it to set up initial state.
- update(ctx) — Called every frame. Use it for movement, input handling, and game logic.
- onMessage(ctx, message) — Called when another script sends a message to this object.
Context Object
The ctx parameter is your gateway to the engine. Key properties:
| Property | Description |
|---|---|
ctx.entity | The object this script is attached to |
ctx.time.delta | Time since last frame (seconds) |
ctx.time.elapsed | Total elapsed time (seconds) |
ctx.input.keys | Currently pressed keyboard keys |
ctx.input.mouseButtons | Currently pressed mouse buttons |
ctx.input.mousePosition | Current mouse position |
ctx.objects | Access and modify all objects in the scene |
ctx.variables | Read and write project-wide variables |
ctx.activeSceneId | Current scene ID |
ctx.activeCameraId | Active camera ID |
Communicating Between Scripts
Use ctx.send(type, payload) to send messages to other scripts. Any script with an onMessage handler will receive it.
// Sender
ctx.send("player:hit", { damage: 10 });
// Receiver
export function onMessage(ctx, message) {
if (message.type === "player:hit") {
health -= message.payload.damage;
}
}Watching for Changes
React to property changes with watchers:
export function init(ctx) {
ctx.watch(ctx.variables, (vars) => {
console.log("Variables changed:", vars);
});
ctx.watchKey(ctx.variables, "score", (value) => {
console.log("Score is now:", value);
});
}