Near sensor nearly working

hi all…

so i have near sensor in my game tied to a small python script.

i have it setup so script always is getting list of objects it is hitting using hitObjectList method. I am looking for object with specific property, as marked by near sensor. i am trying to get a list of all local coins in area of player, but when I run game I get follow effect:

no error in console
list prints as blank

when I change near sensor to search for any objects near it by leaving property box blank, it returns one object is in list. The object is far away, and has the same physics settings as all other objects. My distance and reset distance for sensor is very far… 200 BU and 205 BU. what could be going wrong?

Code I am using:

near = cont.sensors['near']
     List = near_sensor.hitObjectList
         print(str(obList)) #for debug

        if near_sensor.positive:
                        ob = List[0]

again… this is tied to near sensor named near with true level pulsing on…

If anybody can show me why this is faulting, would appreciate.

Obviously the near sensor is sensing this single object only.

Be aware, that static objects can only be sensed when the actor flag is set (physics tab).

What’s obList? You defined List, but tried to print obList.
That should raise an error shouldn’t it?

Make sure also, along with all the other stuff that has already been posted, the object with the near sensor on it is colliding with the correct collision groups (in other words, make sure the collision masks are for the correct layer in physics panel.)

yes thank you for help! it was actors and collision maskings problems, and with code it was copy paste to forums error :).

now though i have new problems, i link a collision sensor to my script… to handle now colliding with a coin. but it says that the sensor does not exist, even though it has same name as suggested in script, and is linked correctly…

what could cause this?

Did you cut and paste? Sometimes I used to cut and paste cont.actuators instead of cont.sensors. Check it’s the right one. :slight_smile:

This is exact code:

coins = cont.sensors[‘Collision’]

and picture of the sensor, it is attached to python controller that worked without this sensor code in it…

i have other sensors defined the exact same way… no issues with code.

exact error message received:

Keyerror: ’ requested item “Collision” does not exist’ and it directs me to the line of code that is declaration of sensor variable…

As this logic is not complete we can’t tell you what is wrong. Here are some remarks:

  • check what controller prints this error.
  • check if the object that has this controller.
  • check if you really need True Level Triggering to be on (it does not match the description from above)

I have tried changing the settings on sensor… no effect…

the controller that prints this error is infact the script controller that has the sensor in it, and when sensor removed, script run correctly.

i have also just now tried added more sensors above the error line, and they all work to be added… so it is something i am doing with the Collision sensor!

can collision sensor, no be applied to script on other object like all other sensors i have tried?



to see what sensors are connected

edit: maybe you have a space in the sensors name

i thought maybe space too, but i check and no, it is not the case.

i run print statement, it displays all sensors are connected EXCEPT the Collision…

i even add new sensors, like message, near, and always. they all display, but I add more collisions and it is the same problem. i have infact now tried every sensor type, all work EXCEPT collisions I add… so then this is ether bug, or collision does not work with python controller that is not on different object?

so instead of simply using collision sensor in code, i must use collision to send message to object with python, and connect that to controller instead??

Try opening this Blend and see if it prints all three sensors:
collision.blend (486 KB)

It has a python controller and two collision sensors, one on each object. I guess there could be something wrong with your version of Blender but I doubt it.

yes that works perfectly… i am using 2.71, so it is not a bug, but rather i have done something terribly wrong with my file :(.

You’re not by any chance trying to add a collision sensor to a no collision object?

I tried that once before, and got a sensor error before realizing the sensor can’t possibly work if your mesh doesn’t receive collisions.

ah yes that was problem thank you!

but now i have one final question… i have collision almost working. now, we i collide… when i hit coins, with logic brick setup of:

Collision -> and -> endObject

it works and detects the collision as soon as coins are hit. but when i connect the exact same collision brick to python, it says it is NEVER positive, despite i am touching same coin in same area… i also try use print statement:


and it prints “None”, even when the collision works with other logic. i tried now to attach collision brick to both python block, as well as and controller which moves coin on impact. Again a remarkable thing! the coin moves showing a collision, but the python reports there is no hitObject, and that it is never positive! how can this be, it is seeming as if, they are contradicting to each other, yes?

nevermind i have found error, it lie in a seperate function that i have now fixed. how to mark this as solved?

thanks again all who helped me!

edit your top post in advanced mode