Do a action recursively on multiple instances of the same object

Hey

To keep things short, im basically building a family tree , each time i spawn a new family member it gets added to a array of the previous member, so it ends up something like this

Member0-Member1 has a reference to Member0 as a parent, Member2 has a reference to member 1 as a parent, Member3 has a reference to member 2 as a parent and so on

Basically i need to run a “getter” function recursively that gets all the parents starting from parent0, so it would get the parent of member1, then member 2, then 3 and so on until it reaches a non-valid result ( point on which i can assume it reached the end of the chain ) and eventually be able to return these as an array so i can loop trough it manually

Any help is REALLY apppreciated!

you should make it a doubly linked list by adding a child reference. then you can search through it in either direction.

Hey, sorry but could you please elaborate further? im not entirely sure i got you

When you spawn a new family member, you currently store the new member in the array of its parent. Omnicypher suggested to also store the parent in a variable of the newly spawned member. That way, any tree element knows all its direct children (your array) as well as its parent (the new variable).

So you can then recursively loop over the child arrays to reach a family member’s great-great-grandchildren, but you can also get the great-great-grandparents by recursively accessing the parent variable.

Thank you both, i understand now but im still confused about one thing… How would i be able to keep running the ¨getter¨function until it reaches the end of the line/array?, I can easly a get a getter function for the Current parent but im kinda confused on how to do something along "Get this parent grand parent and keep going further until you reach the last one "

Still thanks for the help so far its really appreciated

There are several ways to go about this.

The simplest solution is to use a simple While loop:

  • Create a variable to store one family member and initialize it with the inital object’s parent.
  • As a while condition use an IsValid check for this variable.
  • In the while loop, add the variable content to your array.

Alternatively, you could do it recursively like you already suggested:

  • Create a function which gets an array of family members as an Call-by-Reference input parameter.
  • Call this function for the object whose parents you want to collect and pass it an empty array variable.
  • In the function, check if the current object (self) has a parent.
  • If yes, add it to the input array and call the function on the parent, passing it the changed array.
  • If no, just return the unchanged array.
  • Because it’s Call-by-Reference, the array variable you initially passed into the function will be filled with all the object’s parents.

Either way, you’ll end up with an array of parent objects all the way up to the original ancestor.

Thank you so much, it makes sense now!