3
$\begingroup$

Like you can connect a And, or Or controller between a sensor and an Actuator, but does it makes sense to connect a Python controller to an sctuator?
In that case, what would it take to trigger the actuator: a True value returned by the Python module? (if the controller is in module mode, I believe a script doesn't have a return value)

And by the way, is it possible to get a Sensor's value (eg. a property) in a Python controller it's connected to, or are sensors just triggers?

$\endgroup$

2 Answers 2

3
$\begingroup$

It depends on what you want to do with the python controller. It can just be triggered and do something, but it can also read the data of sensors and activate actuators. It doesn't matter though if your controller is in Module mode or in Script mode. Also scripts don't return something (no matter in which mode they're running). They only read data and manipulate.

Controller

But to do anything first you need to know the controller. After importing the bge module (import bge) that is needed for almost everything you want to do in bge, you have to get the current controller your script is executing from. cont = bge.logic.getCurrentController() From now on you can get the sensors and actuators attached to the controller and the object the controller is in.

Sensors

Besides activating controllers they can be read from scripts. There they can either gather data or read if the sensor is active. But to do anything you need the sensor. sensor = cont.sensors['SensorName'] does that. Listing every attribute would be too much. So here's a page where you can look everything up. Generally this page (http://www.tutorialsforblender3d.com/) is worth taking a look at. And you can't only read the data, but also manipulate the properties of the sensor. It's generally like any other attribute (thus sensor.attribute = value). But what you probably need the most is knowing if a sensor sends a positive pulse.

if sensor.positive:
    #do something

is what you're looking for.

Actuators

You get the actuator the same way you get the sensor: actuator = cont.actuators['ActuatorName']. Again, you should take a look at the page to know how to manipulate the actuators through the script, this time at the actuators. Activating the actuator is done through the controller: cont.activate(actuator)

Object Properties

Last but not least there are the game properties in objects. The properties you need the most are the ones in the object your controller's in as well. So we first need to get the object: own = cont.owner Now to read or edit it's game properties you need to use own['property']. So var = own['property'] reads a property and own['property'] = var edits the property.

Conclusion

Yes, you can connect sensors and actuators to a python controller and read their data or activate actuators. Also you can read and edit an object's properties.

$\endgroup$
3
$\begingroup$

does it makes sense to connect a Python controller to an actuator?

A Python controller is a controller as all other controllers. It's behavior is custom. So it depends on you what exactly it does. So, yes it supposed to evaluate sensors status and activate/deactivate connected actuators.

In that case, what would it take to trigger the actuator?

You activate an actuator with activate(actuator) You deactivate an actuator with deactivate(actuator)

Example:

import bge
controller = bge.logic.getCurrentController()
messageActuator = controller.actuators["message"]
controller.activate(messageActuator)

alternative:

import bge
controller = bge.logic.getCurrentController()
controller.activate("message")

Be aware: It is your job to explicitly activate/deactivate actuators. If you do not do that, their status will remain as it is.

And by the way, is it possible to get a Sensor's value (eg. a property) in a Python controller it's connected to, or are sensors just triggers?

They are both. Sensors trigger connected controllers. So you need at least on trigger to run the controller's code.

The controller can check any value of any connected sensor, regardless of the current sensor state.

For more details I suggest to read

Sensor's value (eg. a property)

Sensors have no properties, but the game objects have. So you can get properties from the sensor's owner and of sensed objects.

Sample: import bge controller = bge.logic.getCurrentController()

owner = controller.owner 
owner["property"] = "a string value"

sensorOwner = controller.sensors[0].owner
sensorOwner["sensorOwnerProperty"] = 3.1214

sensedObject = sensor.hitObject
if sensedObject:
    hitObject["sensedObjectProperty"] = 123
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .