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.
#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.
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']