Script

Entityceramic.Script (Class)
Implements: Component

Dynamic scripting component for runtime code execution.

Allows entities to have behavior defined through HScript code that can be modified at runtime. Scripts have access to the full Ceramic API and can define lifecycle methods (init, update, destroy).

Features:

  • JavaScript/TypeScript-like syntax (arrow functions, for-of loops)
  • Automatic entity lifecycle management
  • Error handling and sandboxing
  • Module system for inter-script communication
  • Infinite loop protection
// Create a script that moves an entity
var scriptCode = '
var speed = 100;

function update(delta) {
    entity.x += speed * delta;
    if (entity.x > screen.width) {
        entity.x = 0;
    }
}
';

var entity = new Quad();
entity.size(50, 50);
entity.script = new Script(scriptCode);

Static Members

script
errorHandlers: Array<Function>

Global error handlers called when scripts encounter errors. Add handlers here to be notified of all script errors.


script
traceHandlers: Array<Function>

Global trace handlers called when scripts use trace(). Add handlers here to intercept script trace output.


script
log: Logger

Logger instance for script-related messages

Instance Members

script
content: String

The source code content of this script. Preprocessed to convert JS/TS idioms to HScript.


script
program: hscript.Expr

The parsed HScript AST (Abstract Syntax Tree).


script
interp: Interp

The HScript interpreter executing this script. Provides access to variables and execution context.


script
module: ScriptModule

Module interface for inter-script communication. Other scripts can access this script's exports through this module.


script
entity: Entity

script
initializerName: String

script
destroy(): Void

script
run(): Void

Executes the script and sets up lifecycle callbacks.

Looks for and binds these optional functions:

  • init(): Called once after script execution
  • update(delta): Called every frame with time delta
  • destroy(): Called when script is destroyed

script
getEntityById(itemId: String): Entity

Gets an entity by its ID from the script's context.

Searches for entities in:

  1. Parent Fragment if the script's entity is a Visual
  2. Fragment variable in the script's scope
Name Type Description
itemId String Entity ID to look up
Returns Description
Entity Found entity or null

script
getModule(itemId: String): ScriptModule

Gets a script module by entity ID.

Allows scripts to access other scripts' exported functions and variables through their module interface.

Name Type Description
itemId String ID of the entity whose script module to retrieve
Returns Description
ScriptModule Script module or null if entity has no script

script
get(name: String): Dynamic

Gets a variable value from the script's scope.

Name Type Description
name String Variable name
Returns Description
Dynamic Variable value or null if not found

script
call(name: String, ?args: Array<Dynamic>): Dynamic

Calls a function defined in the script.

Name Type Default Description
name String Function name
args Array<Dynamic> (optional) Optional arguments to pass
Returns Description
Dynamic Function return value

script
callScriptMethod(name: String, numArgs: Int, ?arg1: Dynamic, ?arg2: Dynamic, ?arg3: Dynamic): Dynamic

Internal method to call script functions with optimized argument passing.

Name Type Default Description
name String Function name
numArgs Int Number of arguments
arg1 Dynamic (optional) First argument (or array of all arguments if numArgs > 3)
arg2 Dynamic (optional) Second argument
arg3 Dynamic (optional) Third argument
Returns Description
Dynamic Function return value or null if function not found

script
new(content: String): Void

Creates a new script with the given source code.

The script is parsed immediately. If parsing fails, the script is marked as broken and scheduled for destruction.

Name Type Description
content String Source code in JavaScript/TypeScript-like syntax

Private Members

script
MAX_LOOP_ITERATIONS: Int

Maximum iterations allowed in loops to prevent infinite loops


script
parser: hscript.Parser

Shared HScript parser instance


script
ready: Bool

Whether the script has been successfully parsed and is ready to run


script
running: Bool

Whether the script is currently executing


script
broken: Bool

Whether the script has encountered a fatal error


script
loopStates: IntIntMap

Tracks loop iterations for infinite loop detection


script
bindAsComponent(): Void

Called when script is attached to an entity as a component. Sets up entity-specific variables and schedules execution.


script
checkLoop(index: Int): Bool

Checks for infinite loops by tracking iteration counts. Called automatically in while loops.

Name Type Description
index Int Unique index for each loop in the script
Returns Description
Bool Always true unless max iterations exceeded

script
resetCheckLoop(_: Float): Void

Resets loop iteration counters each frame. Prevents false positives from accumulated iterations.

Name Type
_ Float

script
setEntity(entity: Entity): Void
Name Type
entity Entity

script
getEntity(): Entity
Returns
Entity

Metadata

Name Parameters
:build ceramic.macros.ComponentMacro.build()
:autoBuild ceramic.macros.ComponentMacro.build()
:build ceramic.macros.EntityMacro.buildForCompletion()
:autoBuild ceramic.macros.EntityMacro.buildForCompletion()
:build tracker.macros.EventsMacro.build()
:autoBuild tracker.macros.EventsMacro.build()