Post new topic    
Metal Slime
Send private message
Get count of all slice children (and their children, etc)? 
 PostWed Feb 06, 2019 5:12 am
Send private message Reply with quote
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.
Metal King Slime
Send private message
 
 PostWed Feb 06, 2019 6:16 am
Send private message Reply with quote
Yes, recursion.

Code:
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
Metal Slime
Send private message
 
 PostWed Feb 06, 2019 8:07 am
Send private message Reply with quote
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.
Display posts from previous: