Javascript to check for children of an object


#1

How do I check for the child count of an object in Javascript?


#2

Hi @MJ_Linane,

the methods you need are:

item.getChildrenCount() <- returns the number of children of an item. Returns 0 if it has no children.
item.getChild(childIndex: number) <- returns the child by its index. Returns null if no child is found.

Here’s an example in action

let tree = Scene.getItem("tree") as GameItem;
let childrenCount = tree.getChildrenCount();
let parrot = tree.getChild(0);

tree.say("I've got " + childrenCount + " parrots on me!");
parrot.say("Squawk!");

These two methods are currently marked as internal. Your code editor won’t recognise them, but they still work. We plan to make these and other methods public for your code editor to see in the future!