# Can You Help Me? BGE Properties in Python

I want to wright a script to output the closest object with a spicific game property say “bad guy” So i can edit an aiming script to select the closest enemy in my game ensted of having to put the name of the enemy in my script. Here is my aim script.

import math
pi = 3.141592653589793238462
controller = GameLogic.getCurrentController()
p2 = controller.getOwner()
print p2
p1 = GameLogic.getCurrentScene().getObjectList()[“OBp1”]
p1x = p1.getPosition()[0]
p1y = p1.getPosition()[1]
p1z = p1.getPosition()[2]
pex = p2.getPosition()[0]
pey = p2.getPosition()[1]
pez = p2.getPosition()[2]
xdist = (p1x - pex)
ydist = (p1y - pey)

p1ang = [0.0, 0.0, (((math.atan((ydist/xdist)))))]
p1rot= [0.0, 0.0, (pi+((math.atan((ydist/xdist)))))]
print p1ang
if xdist > 0:
p2.setOrientation(p1ang)
if xdist < 0:
p2.setOrientation(p1rot)

basicly it uses trigenomatry to find the angle between the script object and the object p1 and sets its angle to that. Is it possable to use the distance formula to select the closest “bad guy” for aiming at.

oh ps
this script was for an enemy to aim at the player aka p1 short for “Player 1” which works great because there is only one player in the game. but i want an auto aim feture in my game and it would be nice if it would aim at the closest enemy and not just the predefined one.

Thanks

If there is another way i would like to know that as well. And im prety much a noob at python so please explain in detail.

Why not use hasattr? Something along the lines of (untested code, I haven’t coded BGE python in a little while so I’m a little rusty, but this should give you something to start with)

for object in objectlist:
if hasattr(object, “property”):
#execute

Something along those lines should work. Or you could simply do:

for object in objectlist
if object != nil:
#execute

That should help with selecting, your question was a bit vague so excuse me if I missed something.

But im a noob at python and im trying figure it out.

so it checks all of the objects in the list “Objectlist” to see if they have the property “Property”
and then its just an if then statement.

i did not get the second code at all.

oh How do you add an object to a list. (sorry im a noob)

what i want is a script that looks at all objects in the scene and checks if the have the property “bad guy” and if they do then add them to a list called “enemy”. once that is done i think i can figure it out from there.

Thanks

Woah sorry about that haha. The second block should be:

for object in objectlist:
if object[‘property’] != nil:
#execute

basically if the object doesn’t have the property “property” it’s going to return a nil value when checking, “!=” means not equal to. So it only executes if it’s not equal to nil (nothing).

You’re correct on the first one, hasattr simple means “Has Attribute”. In blender python though “Attribute” = Property. So in blender hasattr literally means has property.

To add them all to a list the easiest way would be to simply do:

enemies = []

for object in objectlist:
if hasattr(object, “property”):
enemies.append(object)

to give you a headstart on what to do next (you said you’re finding whichever is closest?) you’re basically going to run another for loop iterating through the enemies list comparing each xyz data with your characters. For this however I’d personally recommend storing data in a dictionary rather than a list. It would keep your data more clean and keep your enemies position checking loop lightweight. Like this:

enemies = {}
for object in objectlist:
if hasattr(object, “property”):
enemies[object] = object.position

then basically you have your object name which would be the key and the position (xyz list or tuple, don’t remember which it is in blender 2.5) would be the value. Then you don’t have to run object.position a thousand times when comparing against the player.

Hope this helped

That makes more sence, but you gave me 3 codes do i need all three. and if i put them in order would it work. could you write one code that would do all of that and when it is done print everything in the list enemies. are just the object names put into the list enemies, or are there locations put in as well. Thank you so much for helping me

Thanx so much i just figured it out. I’ve got objects with the “property” (i changed it to “bg”) to be appended to a list. i could not get it to go into the dictonary, because append does not work for dictonaries and i don’t know what does. oh and objectlist had to be pre-defined so i used “objectlist = GameLogic.getCurrentScene().getObjectList()” Thanks Killer You were a big help. Here is what i have so far:

``````
objectlist = GameLogic.getCurrentScene().getObjectList()
p1 = GameLogic.getCurrentScene().getObjectList()["OBp1"]
enemies = []
for object in objectlist:
if hasattr(object, "bg"):
enemies.append(object)

print enemies

``````

quick question for anyone out there do you know how to pull out the smallest # in a list?

oh never mind i found it

min(“list_name”) duhh

but i also want it to tell me which # on the list it was exmple:
0 1 2 3 4 5 6
a = [2, 3, 5, 9, 1, 7, 8]
min(a)=>
1
(fill this in)(a)=>
4

This code will find all of the objects with the proporty “bg” (short for bad guy i know cheesy right) and find the closest and print its name. Hope this thread was helpfull to people. i sure lurned a lot. Thanx again killer you were very helpfull.

heres the code:

``````
import math
objectlist = GameLogic.getCurrentScene().getObjectList()
p1 = GameLogic.getCurrentScene().getObjectList()["OBp1"]
enemies = []
distance = []
for object in objectlist:
if hasattr(object, "bg"):
enemies.append(object)

print enemies
for object in enemies:
x = object.getPosition()[0]
y = object.getPosition()[1]
p1x = p1.getPosition()[0]
p1y = p1.getPosition()[1]
dist = math.sqrt(math.pow(x,2) + math.pow(y,2))
distance.append(dist)

print distance
close = min(distance)
print close
idx = distance.index(close)
closob = enemies[idx]
print closob

``````

How are you finding the closest object? I don’t see any comparative code from player to enemy?

And glad I was of help