please help me i’m very bad at python i always use logic only, but now i want to use python with a near sensors.
what i’m trying to do is make a cube do something ( play a sound for exemple ) when my player is near the cube ( exemple trigger distance = 5.00 )
noob code don’t work of course:
cont = bge.logic.getCurrentController()
own = cont.owner
sens = cont.sensors['mySensor']
NearSensor.property = Player
NearSensor.distance = 5.00
NearSensor.reset_distance = 6.00
actu = cont.actuators['myActuator']
Your code gets be executed by a custom python controller C at frame f.
That means at least one connected sensor was triggering controller C withing frame f but before executing the controller.
Rule: sensors evaluate before controllers execute
You code searches for a connected sensor named “mySensor”. If it does not find one … it will result in error.
Then you reconfigure the sensor with certain parameters, overriding whatever was set before. This does not mean the sensor gets evaluated again! You basically prepare it for the next frame.
Then you check the status of the sensor “mySensor”, which is the result of the evaluation performed before this controller was triggered. It will not consider the configuration changes you applied earlier in this script.
Then you activate/deactivate a connected actuator called “myActuator”. This does not mean the actuator will run at that moment. It is just told it should activate/deactivate. The actuator will run later within frame f.
Rule: actuators will run when active and after all controllers executed. Activation and Deactivation signals are considered by the actuator. Deactivation does not necessarily make the actuator not active immediately (see action actuator in flipper mode).
-> Check Cotaks’ post, you have an undefined variable (that should pop up in the console)
-> Have a look at the API documentation when you are unsure about the attributes (bge.types.KX_NearSensor.resetDistance)
-> Remove “main()” from your code. It makes the code look more complex without providing any benefits.
In your file, set the distances in the near brick.
now you see errors in the console.
yes sensor.property does not exist, that is true to change the property of the near sensor you need to use sensor.propName.
Now that that is working, it still doesn’t work does it? that is due to the (u guess) bug of setting distances. If you set the distance in the near sensor it works. if you set near to 0.0 and set the distance in the script it fails. if you set both it works.
define a function (module) near_activator.
functions have build in controller support, first passing command will be the controller, so to grab the controller we start with ‘def near_activator(cont)’ now in order to run this script, you need to rename your script from ‘Text’ to ‘Text.py’ now in the python brick you put it from script to module and type in ‘Text.near_activator’. Now you are using the function we have created.
You see i removed some code, all of it where not needed. You grab own, this is the object that runs the script, so we can change it’s property right away without using/activating more bricks and in this case a near sensor deactivates it self when reset distance is met, so deactivating it has no use and will result in positive state due to sensor check wil be within distance so it turns itself to True again.
Unless you want to reproduce the near sensor’s behavior with python code, you will always need the near sensor.
There is a nice GUI that allows you to configure the sensor quite easily. Why you try to avoid it?
As long as you do not need dynamic reconfiguration of the sensor (e.g. change the sensor range based on situation while in game) you make your development process more complicated than it needs to be.
The BGE is designed around logic bricks. They allow easy setup of static behavior which is typically 90% of the game. You can get dynamic behavior via custom python controllers. Be aware it is still a controller even when it can be used to act as sensor or as actuator.
Hint: If you want to become “python only” you can
write Blender scripts that generate logic bricks for you (obviously before the game starts) in conjunction with custom Python controllers.
switch to an according game engine that support Python only behavior description (like panda3d)