The hierarchy system allows an object to act as a parent of others, so when the parent moves or rotates, all its children follow it. This is extremely useful for UI organization, modular objects, or building more complex structures such as a player with a weapon, a spaceship with lights, or an enemy with multiple components.
Besides the basic setParent() and getParent() functions,
the engine provides additional utilities for hierarchical management:
getAllChildren(), detachAllChildren() and
destroyAllChildren().
The easiest and most visual way to create parentโchild relationships is by using the Hierarchy panel inside the Editor.
setParent(child, parent) in code.When an object has a parent, the Inspector shows the parent's name with a small X icon next to it.
setParent(obj, null) in code.setParent(child, parent)
Assigns a child object to a parent.
Automatically calculates localX, localY and localRotation
to preserve its current visual position and orientation.
setParent(child, parent)
null, the child is detached.// Make the arm a child of the body
const arm = getObjectByName("Arm");
const body = getObjectByName("Body");
setParent(arm, body);
// Detach the arm
setParent(arm, null);
null removes parenting and inheritance.
getParent(child)Returns the parent of an object, or null if none exists.
const parent = getParent(child);
function Update(o, dt) {
const parent = getParent(o);
if (parent) {
console.log(o.name + " belongs to " + parent.name);
}
}
getAllChildren(obj, recursive)
Returns all direct children, or if recursive is true,
all descendants in the hierarchy.
const all = getAllChildren("Parent", true);
detachAllChildren(obj)Detaches all children of an object (same as setParent(child, null) for each).
detachAllChildren("EnemyBoss");
destroyAllChildren(obj)Removes all children and descendants recursively.
destroyAllChildren("EnemyBoss");
When a parent rotates, all its children rotate around the parentโs center, keeping their relative angle and distance.
setRotation(obj, angle)getRotation(obj)rotateObject(obj, delta)const player = getObjectByName("Player");
const weapon = getObjectByName("Weapon");
setParent(weapon, player);
rotateObject(player, 30);
localRotation is calculated when parenting.