near sensor with python

ok is it posible to get the objects that are inside of a near sensors range in python if so how

You use getHitObjectList()

So do you guys (fireside mainly) have some ultimate repository of these,or did you mem them off of the command that gets every option?

i mem them (the few i know)

import GameLogic as g
cont = g.getCurrentController()
own = cont.getOwner()
near = cont.getSensor("near")
obj = near.getHitObjectList()
if near: = 1
    #where "show" is the property the near sensor is looking for

but for some reson this dosent work the object with “show” as its property dosent ever get the message and the prop never changes

You need to use
if near.isPositive():

Also, remember that getHitObjectList is returning a list of objects, not an object.

if near.isPositive()
    hitList = near.getHitObjectList()
    for obj in hitList:
       if hasattr(obj,"show")
   = 1

You might want to use hasattr(obj,“show”), otherwise you could hit something without show and get in trouble.

Saxofoner: Our repository is

still not working and there is no error in the dos window

Use a print statement.


right after = 1
Then check the obj property sensor to make sure it’s working.

Also, I changed the code on hasattr(obj,“show”). It needs quotes because it looks for a string.

the sensors are fine the dos window has nuthing that comes up here is the .blend

Your sensors weren’t working in the file you uploaded. The box with the property needs to be an actor. Also, the reset should be higher than the distance. It’s also better to get the hitObjectList after you check for postive if you have more than one sensor. It still should work, though, I think.
Also, your only getting one sensor when you specify a name so it’s:
near = cont.getSensor(“near”)

wooo it works that sensors thing was wrong its “Sensor not Sensors” aperently, thanks a lot for all the help i relisise im dificult to work with

Glad I could help.

wait, hold on. I just got a little confused, what if I want to change the properties of alot of objects with one near sensor? Like for the range of a grenade.

If they are in the near sensor hitObjectList(), the code should work, it returns all the objects in the near sensor range. The problem would be applying a force in a direction away from the granade. You would have to get the object position and the granade position and through subraction find out the force needed in the x and y planes, then you would probably have to send a message with those values to the object so it could apply the force, or probably better, change two force properties and a boolean that would start the action and have a script read the properties into the motion actuator. Some people have done this with a big invisible bubble that you increase the size rapidly. It’s a lot simpler.