enableTouch() – API Reference

By |

ENABLETOUCH(SWALLOW, PRIORITY)

Enables touch events. Component will start receiving events for three functions
If swallow (optional) is set to true, then entities with lower priority will not receive their touch events.
priority (optional) can be negative or positive. Based on priority engine will device who receive touch event first

touchBegin(point)– player touches the screen
touchMove(point) – player moves a finger
touchEnd – player lifts a finger

Add this extra code to your component in order to handle these new events:

function touchBegan( point ){
  //player touches the screen
  /*Return true or false to tell if touch should be swallowed. If touch is swallowed, then other touch events with lower priority will not be triggered*/
  return true; 
}
component.touchBegan = touchBegan;

function touchMove( point ){
  //player moves a finger
}
component.touchMove = touchMove;

function touchEnded(){
  //player lifts a finger
}
component.touchEnded = touchEnded;

attribute() – API Reference

By |

ATTRIBUTE( NAME )

Returns the value of an attribute.

name – Name of the attribute.

function init(){
  let speed = this.attribute('Speed');
}

Scene – API Reference

By |

Scene

The Scene API handles aspects of game at the 2D/3D world level.

Function Description
addChild(entity)

Adds the given Entity as a child of the Scene.

addLevel(name)

Adds a level to the queue of levels to be displayed. The engine will look for a Level Section (aka Scene) with the given name and schedule it for adding. As soon as the engine needs a new level section to display, it will look for scheduled ones first.

addScoreCoin(value)

Increases the user’s coins by the amount given in value. You can also pass a negative value to decrease the user’s coins. Note: A coin is typically something the user can spend, while a point is not.

addScorePoint(value)

Increases the user’s score by the amount given in value. You can also pass a negative value to decrease the user’s points. Note: A coin is typically something the user can spend, while a point is not.

bestCoins()

Returns the highest value of coins in the current 2D/3D World.

bestGlobalCoins()

Returns the highest amount of coins in the entire game.

bestGlobalPoints()

Returns the highest amount of points in the entire game.

bestPoints()

Returns the highest amount of points in the current 2D/3D World.

camera()

Returns the Camera of the Scene.

characters()

Returns an array of Characters that are currently in the Scene. Remember, there are two kinds of assets that can be placed in a Scene: characters and objects.

clone(originalEntity)

Clones originalEntity and returns a copy of it. Note that the cloned entity’s start() function will be ran when it is cloned.

create(name, parent)

Creates a new Entity using the Asset with the specified name, Asset, or id.

createLinker(name)

Creates and returns a new Linker with the given name (optional).

currentCoins()

Returns the current amount of coins in the 2D/3D World.

currentGlobalCoins()

Returns the current amount of coins in the overall game.

currentGlobalPoints()

Returns the current amount of points in the overall game.

currentLevelName()

Returns the name of the current level.

currentPoints()

Returns the current amount of points in the 2D/3D World.

decreaseCoins(value)

Decreases the user’s coins by the amount given in value.

decreaseCurrentPoints(value)

Decreases the user’s points by the amount given in value.

decreaseTotalPoints(value)

Decreases the given value from the total points in the current 2D/3D World.

entities()

Returns an array of all Entities in the Scene.

find(name)

Returns the array of Entities with the specified name.

findFirst(name)

Returns the first Entity found in the scene with the given name. Useful if you’re sure you only have one Object in the Scene with that name. Returns null if the Entity is not found.

path()

Returns Path object of the Scene.

physicsWorld()

Returns the Scene’s PhysicsWorld object. For more information, look at the PhysicsWorld API reference page.

purgeLevel()

Removes the current first level section.

pushNavigation(name)

Activates a Navigation button with the given name on the current UI, as if the user touched it.

rayTest(from, to)

Returns objects (AABBs) that collide with a ray between the two given Vec3 coordinates. The object returned has the properties bodies and sensors.

restart()

Restarts the scene.

setBackgroundColor(r, g, b)

Sets the background color of the Scene.

setGravity(x, y, z)

Sets the gravity of the Scene.

setScorePoint(value)

Sets the current points to the given value. Note: A coin is typically something the user can spend, while a point is not.

setShadowsEnabled(enabled)

Use this function to turn shadows on or off globally. This can make your game run better on certain devices. Defaults to true (shadows on).

setSpeed(value)

Sets the path speed.

shadowsEnabled()

Returns whether shadows are enabled in the Scene.

speed()

Returns the path speed.

totalCoins()

Returns the total amount of coins in the current 2D/3D World. Note: A coin is typically something the user can spend, while a point is not.

totalGlobalCoins()

Returns the total amount of coins in the overall game.

totalGlobalPoints()

Returns the total amount of points in the overall game.

totalPoints()

Returns the total amount of points in the current 2D/3D World. Note: A coin is typically something the user can spend, while a point is not.


addChild(entity)

Adds the given Entity as a child of the Scene.

Parameters

Entity entity – the Entity to add to the Scene.

↑ Back to top


addLevel(name)

Adds a level to the queue of levels to be displayed. The engine will look for a Level Section (aka Scene) with the given name and schedule it for adding. As soon as the engine needs a new level section to display, it will look for scheduled ones first.

Parameters

string name – the name of the level to be added

// scheduling levels sections ahead of time. Keep in mind that all level sections will not be placed all at once, but based on current distance.
function init(){
  this.scene().addLevel('LevelM1');
  this.scene().addLevel('LevelM2');
  this.scene().addLevel('LevelM3');
  this.scene().addLevel('LevelM2');
  this.scene().addLevel('LevelM1');
}

↑ Back to top


addScoreCoin(value)

Increases the user’s coins by the amount given in value. You can also pass a negative value to decrease the user’s coins. Note: A coin is typically something the user can spend, while a point is not.

Parameters

number value – the number to increase the coins by.

log(this.scene().totalCoins()); // output: 5
this.scene().addScoreCoin(2);
log(this.scene().totalCoins()); // output: 7

↑ Back to top


addScorePoint(value)

Increases the user’s score by the amount given in value. You can also pass a negative value to decrease the user’s points. Note: A coin is typically something the user can spend, while a point is not.

Parameters

number value – the number to increase the score by.

  this.scene().addScorePoint(45);

↑ Back to top


bestCoins()

Returns the highest value of coins in the current 2D/3D World.

Returns

number the highest value of coins in the current 2D/3D World

↑ Back to top


bestGlobalCoins()

Returns the highest amount of coins in the entire game.

Returns

number the highest amount of coins in the entire game

↑ Back to top


bestGlobalPoints()

Returns the highest amount of points in the entire game.

Returns

number the highest amount of points in the entire game

↑ Back to top


bestPoints()

Returns the highest amount of points in the current 2D/3D World.

Returns

number the highest amount of points in the current 2D/3D World

↑ Back to top


camera()

Returns the Camera of the Scene.

Returns

Camera the Camera object of the Scene

↑ Back to top


characters()

Returns an array of Characters that are currently in the Scene. Remember, there are two kinds of assets that can be placed in a Scene: characters and objects.

Returns

array an array of Entities with the Character label in the Scene.

this.scene().characters()[0].setPosition(0, 0, 0); //set the position of the first character returned by characters()

↑ Back to top


clone(originalEntity)

Clones originalEntity and returns a copy of it. Note that the cloned entity’s start() function will be ran when it is cloned.

Parameters

Entity originalEntity – the Entity you’d like to copy

Returns

Entity the new entity

// clone the current entity
let entity = this.scene().findFirst("Cone");
let copiedEntity = this.scene().clone(entity);
this.scene.addChild(copiedEntity);

↑ Back to top


create(name, parent)

Creates a new Entity using the Asset with the specified name, Asset, or id. For using an id, see attribute().

Parameters

string/Asset/number name – The asset the new Entity should use.
Scene/LevelSector parent – (Optional) the parent Entity or LevelSector

Returns

Entity The entity that was just created.

let ent = this.scene().create('Obstacle1');
ent.setPosition(0, 20, 0);

↑ Back to top


createLinker()

Creates and returns a new Linker with the given name (optional). After creation, use methods like setEntities(a, b) and setPosition(vec3) to set up your linker.

Parameters

string the name of the linker (optional)

Returns

Linker the new linker

↑ Back to top


currentCoins()

Returns the current amount of coins in the 2D/3D World.

Returns

number the current amount of coins in the 2D/3D World

↑ Back to top


currentGlobalCoins()

Returns the current amount of coins in the overall game.

Returns

number the current amount of coins in the overall game

↑ Back to top


currentGlobalPoints()

Returns the current amount of points in the overall game.

Returns

number the current amount of points in the overall game

↑ Back to top


currentLevelName()

Returns the name of the current level.

Returns

string the name of the current level

↑ Back to top


currentPoints()

Returns the current amount of points in the 2D/3D World.

Returns

number the current amount of points in the 2D/3D World

↑ Back to top


decreaseCoins(value)

Decreases the user’s coins by the amount given in value.

Parameters

number value – the amount to decrease the coins by

↑ Back to top


decreaseCurrentPoints(value)

Decreases the user’s points by the amount given in value.

Parameters

number value – the amount to decrease the points by

↑ Back to top


decreaseTotalPoints(value)

Decreases the given value from the total points in the current 2D/3D World.

Parameters

number value – the amount to decrease the total points by

↑ Back to top


entities()

Returns an array of all Entities in the Scene.

Returns

array – the entities in the Scene

↑ Back to top


find(name)

Returns the array of Entities with the specified name.

Parameters

string name – The name of the Entity to be searched for.

Returns

array The array of Entities which match that name.

let ents = this.scene().find('myObstacle');
for(let i = 0; i < ents.length; i++){
  // increase the y position of each entity
  let ent = ents[i];
  let pos = ent.position();
  ent.setPosition(pos.x, pos.y + 1, pos.z);
}

let player = this.scene().find('Character')[0]; // use this shorthand if you're confident in only getting one result from the search

↑ Back to top


findFirst(name)

Returns the first Entity found in the scene with the given name. Useful if you’re sure you only have one Object in the Scene with that name. Returns null if the Entity is not found.

Parameters

string name – The name of the Entity to be searched for.

Returns

Entity – the entity found with the given name

↑ Back to top


path()

Returns Path object of the Scene.

Returns

ScenePath the Scene’s path object

↑ Back to top


physicsWorld()

Returns the Scene’s PhysicsWorld object. For more information, look at the PhysicsWorld API reference page.

Returns

PhysicsWorld the Scene’s physics world object

↑ Back to top


purgeLevel()

Removes the current first level section.

↑ Back to top


pushNavigation(name)

Activates a Navigation button with the given name on the current UI, as if the user touched it.

Parameters

string name – the name of the button to push

↑ Back to top


rayTest(from, to)

Returns objects (AABBs) that collide with a ray between the two given Vec3 coordinates. Optionally takes a filter as the third parameter, such as: “kAll”, “kSensor”, “kEnemy”, “kPlatform”, “kCoin”, “kCharacter”.

The object returned by this function has the properties {bodies, sensors} which are each arrays. The bodies and sensors objects have the properties {object, hitPoint}.

Parameters

Vec3 from – the starting coordinate
Vec3 to – the ending coordinate
string filter – optional

Returns

Object with properties {bodies, sensors}.

// choose an object as the Selected Character with ray testing. pt is where the user tapped.

let ray = cam.screenRay(pt);
let rtst = this.scene().rayTest(
        ray.origin,
        ray.origin.add(ray.direction.scale(100))
    );
    
    for (let i = 0; i < rtst.bodies.length; ++i) {
        let hit = rtst.bodies[i];
        Settings.selectedCharacter = hit.object.parentEntity().name();
        return;
    }
    

↑ Back to top


restart()

Restarts the scene.

↑ Back to top


setBackgroundColor(r, g, b)

Sets the background color of the Scene.

Parameters

number r – the red component of the color
number g – the green component of the color
number b – the blue component of the color

↑ Back to top


setGravity(x, y, z)

Sets the gravity of the Scene.

Parameters

number x – the x-axis component of the new gravity
number y – the y-axis component of the new gravity
number z – the z-axis component of the new gravity

↑ Back to top


setScorePoint(value)

Sets the current points to the given value. Note: A coin is typically something the user can spend, while a point is not.

Parameters

number value – the new score value

↑ Back to top


setShadowsEnabled(enabled)

Use this function to turn shadows on or off globally. This can make your game run better on certain devices. Defaults to true (shadows on).

Parameters

boolean enabled – turn on/off shadows globally.

↑ Back to top


setSpeed(value)

Sets the path speed.

Parameters

number value – the new path speed

↑ Back to top


shadowsEnabled()

Returns whether shadows are enabled in the Scene.

Returns

boolean – true if shadows are enabled, false if not.

↑ Back to top


speed()

Returns the path speed.

Returns

number the current path speed

↑ Back to top


totalCoins()

Returns the total amount of coins in the current 2D/3D World. Note: A coin is typically something the user can spend, while a point is not.

Returns

number the total amount of coins in the current 2D/3D World

↑ Back to top


totalGlobalCoins()

Returns the total amount of coins in the overall game.

Returns

number the total amount of coins in the overall game

↑ Back to top


totalGlobalPoints()

Returns the total amount of points in the overall game.

Returns

number the total amount of points in the overall game

↑ Back to top


totalPoints()

Returns the total amount of points in the current 2D/3D World. Note: A coin is typically something the user can spend, while a point is not.

Returns

number the total amount of points in the current 2D/3D World

↑ Back to top

Did you find this page useful?

Please give it a rating:

Please provide some feedback!

This is very helpful to us.

Tell us how we can improve this post?

UI – API Reference

By |

Ui

Function Description
camera()

Returns the Ui’s Camera object. The Ui has an independent camera from the 3D world it’s attached to. It’s recommended to only use the Ui’s Camera for things like screenToWorld() (See the Camera API for more info).

create(name, parent)

Create a new Entity within the Ui based on the given asset name. A parent entity can be passed optionally.

find(name)

Returns the array of Entities, Buttons, or Images with the specified name.

playBackgroundMusic()

Plays the background music of this Ui.

stopBackgroundMusic()

Stops playing the background music of this Ui.


camera()

Returns the Ui’s Camera object. The Ui has an independent camera from the 3D world it’s attached to. It’s recommended to only use the Ui’s Camera for things like screenToWorld() (See the Camera API for more info).

Returns

Camera the Ui’s Camera object

↑ Back to top


create(name, parent)

Create a new Entity within the Ui based on the given asset name. A parent entity can be passed optionally.

Parameters

string name – the asset the new Entity should contain
Entity parent (optional) the parent of the newly created Entity

↑ Back to top


find(name)

Returns the array of Entities, Buttons, or Images with the specified name.

Parameters

string name – the name of the objects you wish to find

Returns

array the array of Ui objects with the specified name

↑ Back to top


playBackgroundMusic()

Plays the background music of this Ui.

↑ Back to top


stopBackgroundMusic()

Stops playing the background music of this Ui.

↑ Back to top

Did you find this page useful?

Please give it a rating:

Please provide some feedback!

This is very helpful to us.

Tell us how we can improve this post?

emitSignal(name, value) – API Reference

By |

EMITSIGNAL(NAME, VALUE)

Sends a signal via an output connection.

name – Name of the output used to emit the signal.
value – Data sent in the signal.

function init(){
  this.emitSignal('Object', 'Obstacle');
}

Script – API Reference

By |

Script Node

With BuildBox you can make games without any coding expertise. However, it’s also possible to add code to your game by using JavaScript. Most standard JavaScript will work within BuildBox. This API will help you control many aspects of your game with BuildBox-specific functionality.

Script Nodes (also called Script Components) consist of four functions by default: init(), start(), update(dt), signal(name, value, sender, source), and requested(name, arg, sender).

Function Description
animation(name) Given the string name of an Animation node, this method returns the matching Animation node object. Returns null if no node is found with that name.
attribute(name) Returns the value of an attribute of this script node. See more about creating attributes here. Attributes can be various data types such as number, Vector3D, boolean, etc., so this method will return the data in that format.
attributeNames() Returns an array of the names of all the attributes within the script node.
attributeType(name) Returns the data type of the given attribute.
disableTouch() Disables touch events. Used together with enableTouch().
emitSignal(name, value) Sends a signal via an output connection.
Output connections are added to a script node by clicking the “+” button on the right side of the node.
enableTouch(swallow, priority) Enables touch events. The entity will start receiving events for three functions: touchBegan(point), touchMove(point), and touchEnd().
entity() Returns the Entity object of this script node.
init() init() is called when the entity is initialized. This happens only once.
log(value) Outputs a message to the console (displayed on the right side of the Preview window). You can use this to debug your code by logging variables. Works the same as console.log() in a normal JavaScript context.
requested(name, arg, sender) This function is tied to the purple circle on the right side of some nodes. While emitSignal() sends out data, attribute(name) can be used to request data. If you connect the purple circle of one script node to an input connection on another script node, the data from the requested() function of the first script node will be sent to the second one whenever the second one calls this.attribute(name).
scene() Returns the Scene. The Scene object handles things at the 2D/3D world level.
signal(name, value, sender, source) An entity’s signal() function is called upon receiving a signal from an input connection.
start() start() is called after all inits on other objects are called.
touchBegan(point) Triggered when the player touches the screen. To add custom behavior when a player touches the screen, you’ll need to add this function to your script, as in the example below.
touchEnd() Triggered when the player lifts their finger. To add custom behavior when a player touches the screen, you’ll need to add this function to your script, like in the example below.
touchMove(point) Triggered when the player moves their finger. To add custom behavior when a player touches the screen, you’ll need to add this function to your script, like in the example below.
ui() Returns the Ui object connected to this 3D world on the mind map.
update(dt) An entity’s update() function is called every frame.
Putting complex logic in this function can negatively affect performance.

animation(name)

Given the string name of an Animation node, this method returns the matching Animation node object. Returns null if no node is found with that name.

Parameters

string name – The name of the Animation node

Returns

Animation Animation node object

↑ Back to top


attribute(name)

Returns the value of an attribute of this script node. See more about creating attributes here. Attributes can be various data types such as number, Vector3D, boolean, etc., so this method will return the data in that format.

Note: For Asset attributes, this.attribute(‘MyAsset’).id() will return an id which can be used with the create() method.

Input and output connections also qualify as attributes. If this.attribute(name) is called on an input or output connection, an array of all the nodes connected to this attribute will be returned. For example, with three 3D Models connected to a script’s input called “My Models”, this.attribute(“My Models”) will return an array of the three 3D Models. The order of this array depends on the order the nodes were connected in.

See also requested().

Parameters

string name – Name of the attribute.
Value arg – (Optional) Value to pass to requested() as an argument. This can be any data type; string, number, boolean, object, etc.

Returns

Value/Array Value of the attribute, or array containing all values connected to the attribute.

Example

function init(){
     let lives = this.attribute('maxLives');
     log(lives); // prints: 3
}


A screenshot showing the maxLives number attribute used in the code example.

↑ Back to top


attributeNames()

Returns an array of the names of all the attributes within the script node. All script nodes have the attributes “Name” and “Self”. “Name” is the name of the node, and “Self” refers to the purple output connection of the node (see requested() and the Custom Nodes for more information). Any custom attributes that have been added will be returned as well.

Returns

array – An array containing the string names of all the attributes within the script node.

This example uses the same attribute setup as the example in attribute().

function start(){
  log(this.attributeNames()); // prints: [Name, Self, maxLives]
}

↑ Back to top


attributeType(name)

Returns the data type of the given attribute.

Parameters

name – The name of the attribute

Returns

string – The data type of the attribute. Possible values: “number”, “bool”, “vec2”, “vec3”, “color”, “string”, “animation2d”, “mesh”, “texture”, “asset”, “sound”, “input”, “output”

↑ Back to top


disableTouch()

Disables touch events. Used together with enableTouch(). Can be seen used in the Is Touched node.

↑ Back to top


emitSignal(name, value)

Sends a signal via an output connection.
Output connections are added to a script node by clicking the “+” button on the right side of the node.

Parameters

name – The name of the output connection.
value – Data sent through the signal. This can be any data type; string, number, boolean, object, etc.

function init(){
  // this script has an output connection named "Initialized".
  this.emitSignal("Initialized", true);
}

↑ Back to top


enableTouch(swallow, priority)

Enables touch events. The entity will start receiving events for three functions: touchBegan(point), touchMove(point), and touchEnd().

Parameters

boolean swallow – (optional) If this boolean is set to true, then entities with lower priority will not receive the touch event.
number priority – (optional) The priority of this component’s touch event. This value decides which entity’s touch events will be triggered first when multiple are touched at once. This value must be greater than zero.

function start(){
  this.enableTouch();
}
component.touchBegan = function (pt) {
  log("Touch began at ", pt);
}

component.touchMove = function (pt) {
   // warning: this function will keep firing as you keep touching, so this log will probably slow your game down
   log("Touch move at ", pt);
}

component.touchEnded = function (pt) {
   log("Touch ended at ", pt);
}

↑ Back to top


entity()

Returns the Entity object of this script node.

function start(){
  let ent = this.entity();
  ent.setPosition(0, 1, 0);
}

↑ Back to top


init()

init() is called when the entity is initialized. This happens only once.

function init(){
  // Called when the entity is initialized.
}

↑ Back to top


log(value)

Outputs a message to the console (displayed on the right side of the Preview window). You can use this to debug your code by logging variables. Works the same as console.log() in a normal JavaScript context.

let myString = "world!";
log("Hello ", myString); // output: Hello world!
let x = 5;
log("Value of x=", x); // output: Value of x=5

↑ Back to top


requested(name, arg, sender)

This function is tied to the purple circle on the right side of some nodes. While emitSignal() sends out data, attribute(name) can be used to request data. If you connect the purple circle of one script node to an input connection on another script node, the data from the requested() function of the first script node will be sent to the second one whenever the second one calls this.attribute(name). Note that this.attribute(name) returns an array when the referenced attribute is an input or output connection, because more than one node can be connected at once. The order of this array depends on the order the nodes were connected in.

Parameters

string name The name attribute being requested (“Self” if using purple connection)
Value arg The data received through the request. This can be any data type; string, number, boolean, object, etc. Undefined if no data was sent.
ComponentScript sender The script node that sent the request.

// Script1
let health = 5;
function requested(name, arg, sender) {
  if (arg == "Current") {
    return health;
  }
}
// Script2
function start() {
  log(this.attribute("InputHealth", "Current")[0]); // logs: 5
}

↑ Back to top


scene()

Returns the Scene. object. The Scene object handles things at the 2D/3D world level.

function signal(name, value){
  // search for objects with the name passed from the signal.
  if(name == 'Object' && value){
    let scene = this.scene();
    let objs = scene.find(value);
  }
}

↑ Back to top


signal(name, value, sender, source)

An entity’s signal() function is called upon receiving a signal from an input connection. See more about creating input connections here.

Parameters

string name The name of the input connection that received the signal.
Value value The data received through the signal. This can be any data type; string, number, boolean, object, etc.
ComponentScript sender The script node that sent the signal.
string source The name of the output connection (within the sender) that sent the signal.

function signal(name, value, sender, source){
  if(value) { //value is a boolean in this case
        let amount = this.attribute('Amount');
        this.scene().addScorePoint( amount );
    }
}

↑ Back to top


start()

start() is called after all inits on other objects are called.

function start(){
  // Called after all inits on other objects are called.
}

↑ Back to top


touchBegan(point)

Triggered when the player touches the screen. To add custom behavior when a player touches the screen, you’ll need to add this function to your script, as in the example below.

Parameters

object point – {x, y} coordinates of where the user touched on the screen. Screen coordinates start at {0, 0} in the upper left corner of the screen, going up to the resolution chosen ({640, 1136} for iPhone 5, for example).

Returns

boolean swallowed If your touchBegan() function returns true, other touch events will lower priority will not receive the touch event.

function touchBegan(point){
  originalPos = this.entity().position();
  this.entity().setPosition(point.x, point.y, originalPos.z);
  return true; 
}
component.touchBegan = touchBegan;

↑ Back to top


touchEnd()

Triggered when the player lifts their finger. To add custom behavior when a player touches the screen, you’ll need to add this function to your script, like in the example below.

function touchEnded(){
  if (this.entity().position().x > 500){
    //invalid position, return to original spot
    this.entity.setPosition(originalPosition);
  }
}
component.touchEnded = touchEnded;

↑ Back to top


touchMove(point)

Triggered when the player moves their finger. To add custom behavior when a player touches the screen, you’ll need to add this function to your script, like in the example below.

Parameters

point – {x, y} coordinates of where the user moved their finger on the screen. Screen coordinates start at {0, 0} in the upper left corner of the screen, going up to the resolution chosen ({640, 1136} for iPhone 5, for example).

function touchMove(point){
  this.entity().setPosition(point.x, point.y, originalPos.z);
}
component.touchMove = touchMove;

↑ Back to top


ui()

Returns the Ui object connected to this 3D world on the mind map.

Returns

Ui the UI object connected to this 3D world on the mind map

let ui = this.ui();
let buttons = ui.find('Start Game');

↑ Back to top


update(dt)

An entity’s update() function is called every frame.
Putting complex logic in this function can negatively affect performance.

number dt Delta time, in seconds. The elapsed time since the last call to update().

function update(dt){

}

↑ Back to top

Did you find this page useful?

Please give it a rating:

Please provide some feedback!

This is very helpful to us.

Tell us how we can improve this post?

Entity – API Reference

By |

Entity

An Entity is an object that exists in your 2D/3D world.

Function Description
addChild(entity) Makes the specified entity a child of the current Entity.
camera() Returns the Camera within the world where the Entity exists. This could be a UI camera or a Scene camera.
children() Returns the array of child Entities that this Entity has.
color() Returns the color of the Entity’s 3D Model in RGBA (Red, Green, Blue, Alpha) format.
component(name) Searches for the component with the specified name in the current Entity and returns it if it’s found. Otherwise, returns null.
components() Returns an array containing all the entity’s components (also known as nodes).
createComponent(type, name) Creates a new component (Node) and activates it. Currently these components can be created programmatically: Animation, 3D Model, and If Collide.
When creating a 3D Model node this way, a mesh must be set before functions like setPosition() can be used on it.
Please see setMesh() for more notes on recommended usage.
forceInit() Forces a newly created Entity to initialize right away.
globalDepth() Returns the depth of the Entity in a 2D World.
id() Returns the Entity’s unique id. Each instance of the Entity in the Scene will have a different id.
isCharacter() Returns if the Entity is a Character (vs an Object).
isRemoved() Returns if the Entity has been removed or not.
isVisible() Returns if the Entity is visible.
level() Returns the LevelSector that the Entity exists in. This refers to the Scene Selector “level” at the bottom of the screen in a 2D/3D World.
linkedEntities() Returns the array of Entities linked to this one with the Linker.
localAABB() Returns the local “axis-aligned bounding box” for the Entity. These coordinates form a box with the minimum corner at (left, bottom, near) and the maximum corner at (right, top, far). Useful for checking the size of your Entity and for ray casting.
name() Returns the Asset name assigned to Entity.
parent() Returns the Entity’s parent, or null if it doesn’t have one.
physics() Returns the Physics Component of the Entity. null is returned if physics is not enabled for this Entity (the checkbox on the Start node).
position() Returns the Entity’s position relative to its parent. If the Entity has no parent, the result of this function will be no different than worldPosition.
remove() Removes the Entity from the Scene.
removeComponent(component) Removes the given component on the next frame.
rotation() Returns the rotation of the Entity (relative to its parent) in degrees (Vec3).
rotationQuat() Returns the rotation of the Entity (relative to its parent) in Quaternion.
setParent(parent) Sets the Entity as a child of the given parent entity or scene, removing it from its current parent.
scale() Returns the scale of the Entity in Vec3.
setAnimate(value) Enables/disables animation on the Entity. In this case, “animation” is referring to the position, rotation, and scale animations in the editor.
setCastShadows(value) Enables/disables shadow casting of all 3D Models within the Entity at once.
setColor(r, g, b, a) Sets the color of every 3D model within the Entity. Uses RGB (Red, Green, Blue) format. The minimum value is 0 and the maximum is 255. For example, {r:0, g:0, b:0} is black and {r:255, g:255, b:255} is white. Alpha is an optional parameter and sets the opacity of the 3D Model.
setGlobalDepth(value) Set the depth of the Entity in a 2D World.
setOpacity(value) Sets opacity of every 3D model and animation in the Entity.
setPosition(x, y, z) Sets the position of the Entity relative to the Entity’s parent. If the Entity has no parent, this sets the world position of the Entity.
setWorldPosition(x, y, z) Sets the world position of the Entity, not relative to any parent.
setReceiveShadows(value) Enables/disables shadow receiving of all 3D Models within the Entity at once.
setRotation(x, y, z) Sets the rotation value of the Entity, in degrees. This will affect the rotation of every 3D Model within the Entity.
setRotationQuat(value) Sets rotation value in Quaternion.
setScale(x, y, z) Sets the scale of the Entity. This will affect the scale of every 3D Model within the Entity.
setTexture(name) Applies a new texture to all 3D Models within the Entity. The texture should be present in Sprite Editor.
setVisible(value) Sets the visibility of the Entity; true if the Entity is visible, false if not.
transform() Returns the transformation matrix of the Entity.
transformedAABB() Returns the relative “axis-aligned bounding box” for the Entity. These coordinates form a box with the minimum corner at (left, bottom, near) and the maximum corner at (right, top, far). Useful for checking the size of your Entity relative to its parent, and for ray casting.
transformedOBB() Returns the “oriented bounding box” for the Entity relative to its parent. OBB is more precise but more performance-heavy than AABB.
worldAABB() Returns the world (absolute) “axis-aligned bounding box” for the Entity. These coordinates form a box with the minimum corner at (left, bottom, near) and the maximum corner at (right, top, far). Useful for checking the size of your Entity and for ray casting.
worldOBB() Returns the “oriented bounding box” for the Entity. OBB is more precise but more performance-heavy than AABB.
worldPosition() Returns the entity’s position coordinates in the world (absolute position).
worldRotation() Returns the entity’s rotation in degrees (not relative to its parent).
worldRotationQuat() Returns the entity’s rotation in Quaternion (not relative to its parent).

addChild(entity)

Makes the specified entity a child of the current Entity.

Entity entity the Entity object that will become the child of the Entity you’re calling the method on.

let ent = this.scene().create('Cone');
this.entity().addChild(ent);

↑ Back to top


camera()

Returns the Camera within the world where the Entity exists. This could be a UI camera or a Scene camera.

Returns

Camera The Camera within the world where the Entity exists.

↑ Back to top


children()

Returns the array of child Entites that this Entity has.

Returns

array The array of children Entities that this Entity has.

let children = this.entity().children();
// reset position of all children
for (i = 0; i <children.length; i++){
  children[i].setPosition(0, 0, 0);
}

↑ Back to top


color()

Returns the color of the Entity’s 3D Model in RGBA (Red, Green, Blue, Alpha) format. The minimum value is 0 and the maximum is 255. For example, {r:0, g:0, b:0, a:1} is black and {r:255, g:255, b:255, a:1} is white.
Note, if the Entity has multiple 3D Model nodes, this function will return the color of the first one that was created.

Returns

Object {r, g, b, a} color pigment of the Entity’s 3D Model.

let ent = this.scene().create("Cube");
log(ent.color().r); // result: the red component of the cube's color.

↑ Back to top


component(name)

Searches for the component with the specified name in the current Entity and returns it if it’s found. Otherwise, returns null. Note that if you have multiple components with the same name, component() will simply return the first one it finds. It’s advised to give your components unique names to avoid bugs.

Parameters

string name – The name of the component to search for.

Returns

Component The Component object found with the given name.

let collide = this.entity().component('If Collide');
if(collide){ // check that collide is not null
  let obj = collide.collisionObject();
}

↑ Back to top


components()

Returns an array containing all the components in the Entity’s mind map.

Returns

Array – array of components

↑ Back to top


createComponent(type, name)

Creates a new component (Node) and activates it. Currently these components can be created programmatically: Animation, 3D Model, and If Collide.
When creating a 3D Model node this way, a mesh must be set before functions like setPosition() can be used on it.
Please see setMesh() for more notes on recommended usage.

Parameters

string type – Possible inputs: “Animation”, “3D Model”, or “If Collide”
string name – The name of the new component.

Returns

Component the newly created component (Animation, 3D Model, or If Collide).

let modelComp = this.entity().createComponent("3D Model", "myModel");
modelComp.setMesh(this.attribute("meshAttribute"));
modelComp.setPosition(0, 10, 0); // now that a mesh is set, any Model 3D functions can be used

let animComp = this.entity().createComponent("Animation", "myAnim");
animComp.setAnimation(this.attribute("bounceAnimation"));

let collideComp = this.entity().createComponent("If Collide", "myCollide");
collideComp.setActive(true);

↑ Back to top


forceInit()

Normally Entities are initialized (which includes running their scripts’ init() functions) on the next frame. Calling this function on a newly created Entity will initialize right away instead. This can be useful if you have variables or functions being created in the Entity’s init() function that will need to be accessed immediately by the Entity creating it. Check out the simple example below.

// first entity
function start(){
    let ent = this.scene().create('Enemy');
    ent.forceInit();
    // without forceInit(), this call would fail because ent isn't initialized yet:
    ent.testFunction("Hello World");
}

// spawned entity
function init(){
    this.entity().testFunction = (string) => log(string); // logs the string passed to it
}

↑ Back to top


globalDepth()

Returns the global depth of the Entity. Depth decides which object is on top in a 2D World. A higher value will be above a lower number in the Scene.

Returns

number – The global depth of the Entity

↑ Back to top


id()

Returns the Entity’s unique id. Each instance of the Entity in the Scene will have a different id, including Entities that are made using this.scene().create().

Returns

number – the Entity’s id

↑ Back to top


isCharacter()

Returns if the Entity is a Character (vs an Object).

Returns

boolean true if the Entity is a Character, false if the Entity is not.

↑ Back to top


isRemoved()

Returns if the Entity has been removed or not.

Returns

boolean true if the Entity is has been removed, false if not.

↑ Back to top


isVisible()

Returns if the Entity is visible.

Returns

boolean true if the Entity is visible, false if the Entity is not.

↑ Back to top


level()

Returns the LevelSector that the Entity exists in. This refers to the Scene Selector “level” at the bottom of the screen in a 2D/3D World.

Returns

LevelSector The LevelSector that the Entity exists in.

// remove all cubes from level 5
let cubes = this.scene().find("Cube");
for (int i=0; i<cubes.length; i++){
  if (cubes[i].level().name() == "5"){
    cubes[i].remove();
  }
}

↑ Back to top


linkedEntities()

Returns the array of Entities linked to this one with the Linker.

Returns

Array of Entities linked to this one.

↑ Back to top


localAABB()

Returns the local “axis-aligned bounding box” for the Entity. These coordinates form a box with the minimum corner at (left, bottom, near) and the maximum corner at (right, top, far). Useful for checking the size of your Entity and for ray casting.

Returns

AABB AABB object containing relative AABB data

↑ Back to top


name()

Returns the Asset name assigned to Entity.

Returns

string name of the Asset assigned to the Entity.

↑ Back to top


parent()

Returns the Entity’s parent, or null if it doesn’t have one.

Returns

Entity the parent Entity of this Entity, or null if no parent exists.

let parent = this.entity().parent();

↑ Back to top


physics()

Returns the Physics Component of the Entity. null is returned if physics is not enabled for this Entity (the checkbox on the Start node).

Returns

Physics Component the Physics Component of the entity

let physics = this.entity().physics();
if(physics){ // check that physics is not null
  physics.setLinearVelocity(0, 20, 0);
}

↑ Back to top


position()

Returns the Entity’s position relative to its parent. If the Entity has no parent, the result of this function will be no different than worldPosition.

Returns

Vec3 {x, y, z} relative coordinates of the Entity.

let pos = this.entity().position();
log('X position = ' + pos.x);

↑ Back to top


remove()

Removes the Entity from the Scene. Note that if you try to access this Entity after it has been removed, you’ll get a null reference error. To protect from this, check this.entity().isRemoved() first.

//Remove an entity if it goes further than 10 units on the X axis.
let pos = this.entity().position()
if(pos.x > 10){
  this.entity().remove();
}

↑ Back to top


removeComponent(component)

Removes the given component on the next frame. The component object is required as a parameter; this can be found by name with this.entity().component(name) for example.

Parameters

Component component The component to remove

↑ Back to top


rotation()

Returns the rotation of the Entity in degrees (Vec3) relative to its parent.

Returns

Vec3 The {x, y, z} rotation value of the Entity, in degrees.

↑ Back to top


rotationQuat()

Returns the rotation of the Entity in Quaternion relative to its parent.

Returns

Quaternion The rotation value in Quaternion.

↑ Back to top


setParent(parent)

Sets the Entity as a child of the given parent entity or scene, removing it from its current parent. If passed null, the Entity will be parented to the current scene.

Parameters

Entity/LevelSector parent The new parent of the Entity

↑ Back to top


scale()

Returns the scale of the Entity in Vec3.

Returns

Vec3 The {x, y, z} scale value of the Entity.

↑ Back to top


setAnimate(value)

Enables/disables animation on the Entity. In this case, “animation” is referring to the position, rotation, and scale animations in the editor.

Parameters

boolean value – True if enabled, false if disabled.

↑ Back to top


setCastShadows(value)

Enables/disables shadow casting of all 3D Models within the Entity at once.

Parameters

boolean value – True if enabled, false if disabled.

↑ Back to top


setColor(r, g, b, a)

Sets the color of every 3D model within the Entity. Uses RGB (Red, Green, Blue) format. The minimum value is 0 and the maximum is 255. For example, {r:0, g:0, b:0} is black and {r:255, g:255, b:255} is white. Alpha is an optional parameter and sets the opacity of the 3D Model.

Parameters

number r – the red component of the color
number g – the green component of the color
number b – the blue component of the color
number a – (Optional) The alpha component of the color. Range: 0-255

↑ Back to top


setGlobalDepth(value)

Sets the global depth of the Entity. Depth decides which object is on top in a 2D World. A higher value will be above a lower number in the Scene.

Parameters

number value – The global depth of the Entity

↑ Back to top


setOpacity(value)

Sets opacity of every 3D model and animation in the Entity.

Parameters

number value – Range: 0-1. The lower the value, the less opaque the Entity becomes.

↑ Back to top


setPosition(x, y, z)

Sets the position of the Entity relative to the Entity’s parent. If the Entity has no parent, this sets the world position of the Entity.

Parameters

number x – X coordinate in 3D space
number y – Y coordinate in 3D space
number z – Z coordinate in 3D space

↑ Back to top


setWorldPosition(x, y, z)

Sets the world position of the Entity, not relative to any parent.

Parameters

number x – X coordinate in 3D space
number y – Y coordinate in 3D space
number z – Z coordinate in 3D space

↑ Back to top


setReceiveShadows(value)

Enables/disables shadow receiving of all 3D Models within the Entity at once.

Parameters

boolean value – True if enabled, false if disabled.

↑ Back to top


setRotation(x, y, z)

Sets the rotation value of the Entity, in degrees. This will affect the rotation of every 3D Model within the Entity. Note that if you go past 360 degrees in either direction, the rotation will reset back to under 360 to avoid numbers getting extremely large. For example, if you pass 365 to this function and then checked the rotation with this.entity().rotation();, it would return 5.

Parameters

number x – the X rotation of the Entity
number y – the Y rotation of the Entity
number z – the Z rotation of the Entity

↑ Back to top


setRotationQuat(value)

Sets rotation value in Quaternion.

Parameters

Quaternion value – The Quaternion rotation value to be set

↑ Back to top


setScale(x, y, z)

Sets the scale of the Entity. This will affect the scale of every 3D Model within the Entity.

Parameters

number x – The scale of the Entity on the x axis
number y – The scale of the Entity on the y axis
number z – The scale of the Entity on the z axis

↑ Back to top


setTexture(name)

Applies a new texture to all 3D Models within the Entity. The texture should be present in Sprite Editor.

Parameters

string name – The name of the texture to be applied.

if(health <= 0.5){
  this.entity().setTexture('Damaged Texture');
}

↑ Back to top


setVisible(value)

Sets the visibility of the Entity; true if the Entity is visible, false if not.

Parameters

boolean value – the visibility of the Entity.

↑ Back to top


transform()

Returns the transformation matrix of the Entity.

Returns

Mat4 the Mat4 transformation matrix of the Entity.

↑ Back to top


transformedAABB()

Returns the relative “axis-aligned bounding box” for the Entity. These coordinates form a box with the minimum corner at (left, bottom, near) and the maximum corner at (right, top, far). Useful for checking the size of your Entity relative to its parent, and for ray casting.

Returns

AABB AABB object containing relative AABB data

↑ Back to top


transformedOBB()

Returns the “oriented bounding box” for the Entity relative to its parent. OBB is more precise but more performance-heavy than AABB.

Returns

OBB OBB object containing relative OBB data

↑ Back to top


worldAABB()

Returns the world (absolute) “axis-aligned bounding box” for the Entity. These coordinates form a box with the minimum corner at (left, bottom, near) and the maximum corner at (right, top, far). Useful for checking the size of your Entity and for ray casting.

Returns

AABB AABB object containing world AABB data

↑ Back to top


worldOBB()

Returns the “oriented bounding box” for the Entity. OBB is more precise but more performance-heavy than AABB.

Returns

OBB OBB object containing OBB data

↑ Back to top


worldPosition()

Returns the entity’s position coordinates in the world (absolute position).

Returns

Vec3 the {x, y, z} world coordinates of the Entity

let pos = this.entity().worldPosition();
log('X World Position = ' + pos.x);

↑ Back to top


worldRotation()

Returns the Entity’s rotation in the world (not relative to its parent) in degrees.

Returns

Vec3 the {x, y, z} world rotation of the Entity

let rot = this.entity().worldRotation();
log('World Rotation = ' + rot);

↑ Back to top


worldRotationQuat()

Returns the Entity’s rotation in the world (not relative to its parent) in Quaternion.

Returns

Quaternion the world rotation of the Entity in Quaternion
↑ Back to top

Did you find this page useful?

Please give it a rating:

Please provide some feedback!

This is very helpful to us.

Tell us how we can improve this post?