The engineβs script system allows extending the behavior of objects through JavaScript code. Each object can have a single script assigned, defining the functions that the engine automatically executes at different points in the lifecycle.
dt is the time in seconds since the previous frame.Update calls, useful for camera or UI logic.obj
Reference to the scene object the script is attached to.
Through obj you can access and modify its main properties:
function Start(obj) {
obj.hp = 100; // object health
obj.x = 200; // X position
obj.y = 300; // Y position
obj.color = "#f00"; // color if it's a simple object
}
Common obj properties:
obj.x, obj.y: world position.obj.w, obj.h: width and height (UI or manually defined).obj.scaleX, obj.scaleY: object scale.obj.name: object name in the scene.obj.active: whether the object is active (true) or not.obj.components: list of components (e.g., AudioSource).dt (Delta Time)The time in seconds elapsed since the last frame. It ensures movements and animations remain FPS-independent.
function Update(obj, dt) {
// Move 100 pixels per second to the right
obj.x += 100 * dt;
}
π Whether the game runs at 30 FPS or 120 FPS, the movement remains the same because it depends on time, not frames.
// Script PlayerController.js
function Start(obj) {
obj.hp = 100;
console.log(obj.name + " ready with " + obj.hp + " HP");
}
function Update(obj, dt) {
// Horizontal movement
if (isKeyPressed("ArrowRight")) obj.x += 200 * dt;
if (isKeyPressed("ArrowLeft")) obj.x -= 200 * dt;
// Simulate damage
if (isKeyPressed("d")) {
obj.hp -= 1;
console.log("HP: " + obj.hp);
}
}
function OnDestroy(obj) {
console.log(obj.name + " removed");
}
Even though each object can only have one script, objects can still communicate with each other:
CallFunction("Player", "Heal", 20) β directly invokes Heal in the Player script.obj to access the object and dt for time-based logic.PlayerController, EnemyAI, GameManager).