Bge spawned object targetting

Hi guys, 1st post on this site. I’ve been looking around here and google searching for a week or 2 now, and tho I’ve found a lot of useful information I still haven’t been able to put together a solution. needless to say I’m a noob at game design.

I have an empty, spawning copies of an object I made that is in an inactive layer. I have an enemy wandering around that is suppose to be able to detect these spawned objects using a near sensor and a property of the origin object each copy should also have. I’ve found code for making lists of objects with a given property, and checking an objects distance, angle, and line of sight on each object in the list for picking one to target, and then giving that target to a steering actuator. then of course activating that actuator.

the problem I’m having is that the enemy does not seem to target any of the spawned object that enter its detection range. I intended to have the spawner spawn each object with an assigned name, but based on what I’ve learned from this site and others I found, objects created in the game engine can’t be referenced by name, right? so weather they have the same name or not should make no difference.

I’ve started wondering if it’s because they are copies of another objects that they were considered the same, so the enemy would be targeting the original in an inactive layer. I’ve read on here however that created objects are created with their own reference that can be saved to a variable and added to a list. but attempting to reference an object that no longer exists can cause errors. is there a way to add each object that’s created to a list AND be able tell that list to remove specific object references when those objects are being removed from the game?

also, it’s been a while since I’ve programmed, and never for a game or in blender before. so I’m not familiar with how to reference a script from another or a specific module in a script from another. checking the list in a script from a script running on enemies that will be spawning in throughout the game.

text walls ik, sorry. any and all help is greatly appreciated.

in upbge objects can have a unique name.

however I think you need to be sure your targeting the correct object

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

Near = cont.sensors['Near']

if Near.positive:
    Nearest = Near.hitObject
    #get closest target in hitObject list
    for obj in Near.hitObjectList:
         if own.getDistanceTo(obj) < own.getDistanceTo(Nearest):
              Nearest = obj

also remember - near uses collision !!
make sure the bounds / collision layer the near are looking for are correct.

[[email protected]|attachment]
thank you @BluePrintRandom for the advice. unfortunately I still haven’t been able to get things working.

haven’t used upbge, I’ve seen a bit about it tho.
I’ve tried using that code and still no luck. since you mentioned collision layers I’ve tried messing with those settings on the enemy, player, and the object that is getting copied. also no luck. in the code you provided I tried both leaving the ‘near’ alone, and also changing it to the name of the near sensor that is activating the python controller, because code from another thread I’ve found on this site seemed to suggest that for actuators activated by that code. neither way worked.

(upload://[email protected]) (3.9 MB)

the near sensor is definitely detecting the spawned objects because it will activate the enemies walking animation. but still the enemy does not navigate to the objects. I’ve uploaded the file hoping that maybe someone will be able to look at it and see what I’m doing wrong. the enemy, player, and spawner are on layer10.

you can see the enemy detects and navigates to the player just fine, but the player is already set as the target of that steering actuator. and it detects the spawned objects just fine. so I can only imagine that I must be doing something wrong with the code. is the code you provided suppose to be used with upbge?

and thanks again for any further help.

Just use near.hitObject - it should automatically return the nearest detected object. Then set the steering target to that object:
target.blend (545.6 KB) (UPBGE)
Hope this helps!

hm, seems I really am gunna need upbge to get past this then.
I tried running the thing you put together in blender and nothing happened, even though I could find no reason why it wouldn’t work. I even tried adding an empty and having it spawn in the blue cube using logic nodes and that wasn’t working for some reason.

but after downloading and opening upbge, then opening your file in that and running it, it worked just fine.

don’t know anything about upbge yet. thought it was an addon for blender (no clue how to get and add in addons yet, which is why I didn’t have it already) but I guess it’s a whole separate program? will have to spend some time learning about it now.

anyways thank you @Thatrius and @BluePrintRandom for your help. hopefully now I will be able to continue work on my game without any other odd road blocks using upbge. :slight_smile:

Strange… didn’t think there was anything UPBGE exclusive in the blend. Looks like anything saved in UPBGE won’t work in other versions.

Made the exact same thing again in regular BGE, and it works perfectly from my end:
target2.blend (489.5 KB)

UPBGE is not backwards compatible

I have been able to recover upbge / bge files by re-appending everything into a new blend.

that target2 file does indeed work in normal blender for me too now :slight_smile:
I did notice that when I opened my game file in upbge, my enemy was turning sideways as it fallows the player. so that’s a translation problem between regular blender and upbge? and opening a file from one, resaving it in the other, and then reopening it fixes those translation issues?