πŸ“Œ CallFunction

What is it?

CallFunction is an engine utility that allows you to execute a specific function inside the script attached to an object.

This method is required because, due to the engine’s script isolation system, functions defined inside scripts are not globally accessible and cannot be called directly from other objects.

By design, each object executes its script inside a protected environment, preventing other objects from directly accessing internal functions or altering behavior without control. CallFunction acts as the only authorized bridge that safely and consistently allows objects to invoke methods from one another.

πŸ“– Syntax

CallFunction(target, method, ...args);

🟩 Basic Example

function Heal(obj, amount) {
  obj.hp = (obj.hp ?? 100) + amount;
}

Call:

CallFunction(obj, "Heal", 25);

πŸ” Example with Return Value

function IsAlive(obj) {
  return obj.hp > 0;
}

let alive = CallFunction(obj, "IsAlive");

if (alive) {
  console.log("The player is still alive");
}
πŸ’‘ If several scripts contain a function with the same name, only the first one that returns something other than undefined will stop the search.

πŸ”‘ Real Differences Compared to sendMessage

Feature sendMessage CallFunction
Scope Calls the method in all scripts Stops at the first valid method
Parameters Supports parameters Supports parameters and return values
Return Value No return Returns the first non-undefined value

πŸ“Œ When to Use CallFunction