Get count of all slice children (and their children, etc)?

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Get count of all slice children (and their children, etc)?

Post by Foxley »

I want a script that has a slice as its argument that returns the child count of that slice, as well as the child count of all of that slice's children (and its children, and its children, and so forth). Just a grand total of all the slices parented to a slice.

What's the best way to pull this off? Is there some way to do recursive for loops that would be able to go through each slice's children until it hits a slice with no children? I could probably figure this out all on my own eventually, but kind of on a time crunch due to the 2 week nature of this game jam.
Last edited by Foxley on Wed Feb 06, 2019 5:13 am, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Yes, recursion.

Code: Select all

script, descendent count, sl, begin
  variable (ret, child)
  child := first child (sl)
  while (child) do (
    ret += 1 + descendent count (child)
    child := next sibling (child)
  )
  return (ret)
end
Note: it doesn't count the slice you pass in to it - give it a slice with no children and it returns 0.

Edit: heh, turns out this script was already in the Script dump
Last edited by TMC on Wed Feb 06, 2019 6:19 am, edited 4 times in total.
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

Thanks! It's doing exactly what I needed it to do, and it let me get rid of a janky "slice complexity" system I had rigged up.
Post Reply