Collect list of children and parents

This is a rough draft to turn from pseudo code into code

Psudo code
Check all children and parents, if they have the property target, and my own target does not equal “Empty”
change their target to mine.


import bge

Children = own.childrenRecursive
for objects in Children
   
    if own['Target'] != null // how do I check to see if a property exists?
    object['Target'] = own['Target']


Any help thinking this through would be appreciated,

is there any way to check if a item is the “root” of a object hierarchy?

I need to get a list of all items linked to a gameobject with parenting.

So all the way to the “root”


#Check if 'root' object
if len(obj.children) > 0:
  #Loop through child objects
  for child in obj.children:
    #Check for property
    if 'target' in child and cont.owner['target'] != 'Empty':
      #Change target script"

I’m not sure if I understand your first question, but if all you’re trying to do is check if the ‘Target’ property exists and does not equal the string ‘Empty’ then all you need is:

if 'Target' in own and own['Target'] != 'Empty':
    # do stuff

To get all the parents of an object as well as the “root” object, you can use this function:

# Get a list of all the parents of an object
# The last object in the list is the root
# Returns an empty list if the object doesn't have a parent
def get_parents(obj):
    parentList = []
    while obj.parent:
        obj = obj.parent
        parentList.append(obj)
    return parentList

If you need to simply check if an object is the root, just check if it has a parent. If it doesn’t, then it’s obviously the root.


def isRoot(gameObject):
  return gameObject.parent==None


  gameObject.children

see Mobious’ post

Ok here it is :smiley:


import bge




cont = bge.logic.getCurrentController()
own = cont.owner




def get_parents(obj):
    parentList = []
    while obj.parent:
        obj = obj.parent
        if 'target' in obj and own['target'] !='Empty':
            obj['target']=own['target']
        parentList.append(obj)
        
    return parentList
get_parents(own)




#Check if 'root' object
if len(own.children) > 0:
    for child in own.children:
        if 'target' in child and own['target'] != 'Empty':
            child['target']=own['target']      

Attachments

PassParentAndChildTarget.blend (515 KB)

Now, will I need does parent have other children?

So message goes Down to all children and children’s children and the parents parents, but what about “siblings”?

Parents children? parents parents children?

Now, will I need does parent have other children?

So message goes Down to all children and children’s children and the parents parents, but what about “siblings”?

Parents children? parents parents children?

Got it

now I just need the same thing in a “power supply” to pass “power”

and a few other components, like anti-gravity and “healing nanites”

Attachments

PassParentChildSiblingstarget.blend (518 KB)

If you need all the objects that are children of the root object, just get the root object using the method above. Then all you need is:

children = rootObj.childrenRecursive