Nested Hierarchy?

0 favourites
  • 4 posts
From the Asset Store
includes an animated character, 2 monsters and over 600 sprites between props, backgrounds and tiles
  • I have a parent with a child. That child is a parent with a child.That child is a parent with a child. Etc, etc...

    When I check how many children the original parent has, it tells me 1.

    Is there a way to have the system count (essentially) grandchildren as children?

    I have to keep them separated like this, because the player is able to separate them, and the separated child becomes the parent.

    Think of it like stacking cards. The parent being the bottom card. When you cut the deck half way, the bottom of the new stack becomes the parent. I want to be able to quickly count how many cards are in each stack.

    I'd have thought this would be in by default.

  • Currently there isn't an expression that will return that value.

    To get around that you can use the Pick Children condition with the "All" option and then count the picked instances.

    construct.net/en/make-games/manuals/construct-3/plugin-reference/common-features/common-conditions

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Looks like you can do it by recursively picking each child and adding up all the child counts. Simpler enough if all the object types are the same from the looks of it but it would complicate things with differing types.

    Function descendentCount(uid)
    Sprite: Pick by uid: uid
    — local number count
    — set count to sprite.childCount
    — Repeat count times
    — sprite: pick “sprite” child loopindex
    — — add descendentCount(sprite.uid) to count
    — set return to count

    Or depending on how the js api lets you access children it may work better because it doesn’t care about object types.

    function descendentCount(obj)
    {
     let count = obj.children.length;
     for(let i=0; i<obj.children.length;i++) {
     count+=descendentCount(obj.children[i]);
     }
     return count;
    }
  • Thanks for the help everyone :)

    I was able to do it using the "pick all" event.

    Hopefully we'll get something like this in the future, along with a "Is Parent of" event ;)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)