Q: Is there a way to make child objects unique when spawned in BGE

(Brvdbury) #1


So in my game I have NPCs that are spawned in, and they have their own health bar that’s a child to the hitbox of the NPC.

So lets say I damage the NPC and there’s multiple versions of the NPC spawned, all of their health bars will take the same amount of damage like they all use the same programming when spawned in.
I want to make it so the child (Health Bar) will only update if its parent takes damage.

If this makes any sense, does anyone know how I could tackle this with Python (I’m not familiar enough with Python)


(edderkop) #2

you can take a look at this old test of mine, it does what you describe.


(Brvdbury) #3

Thank you for this example. The script does what I need for my game.

I also noticed a few other scripts in the example that could be very useful too, so thank you for that too.

Much appreciated!

(edderkop) #4

you are welcome to do so :wink:

(jesusmora) #5

is this upbge?

in any version (bge or upbge), what i do is put an int property in the object that you will spawn, called something like “ID” or “NPC”. then i create all objects in python:

obj = cont.owner
scene = bge.logic.getCurrentScene()
#find all objects with the property and count them
def findObjects():
     aec = [abc for abc in scene.objects if "NPC" in abc]
     if aec:
          return len(aec)
          return 0
#spawning an object will spawn all it's children
ob1 = scene.addObject("enemy", obj, 0)
ob1.worldPosition = obj.worldPosition
ob1.worldOrientation = obj.worldOrientation
ob1["NPC"] = findObjects()

all objects will have a different number in their property, you can find the correct object by looking at the one with the property with the matching number:

objList = bge.logic.getCurrentScene().objects
sel = 2
targets = [abc for abc in objList if "NPC" in abc]
if targets:
     #find the object 2
     selected = [aec for aec in objList if aec["NPC"] == sel]
     if selected:
          #you can change the object here

i think what you wanted to do was access the children of a parent object, you can just do object.childrenRecursive[0], but only in bge. in upbge it’s read only.

in upbge what i did was put a script in the lifebar and do:
par = obj.parent
obj.color = [par["unitlife"]/100*par["tlife"], 0.0, 1.0, 1.0]
it is much better for lifebar and also works in bge.

in upbge you can also rename objects by: ob1.name = "name"

i hope what i wrote is of use.

(theoldguy) #6

That’s really cool. Thank you Jesus. :):grinning::smiley: