moved py controller functions from SCA_PythonController to SCA_IController - the base controller class so python can get the sensors & actuators from any controller (not just SCA_PythonController types)

also deprecated getActuators() and getSensors() for 'sensors' and 'actuators' attributes.

an example of getting every sensor connected to an object.
 all_sensors = [s for c in ob.controllers for s in c.sensors]
This commit is contained in:
2009-05-06 09:12:08 +00:00
parent 25b415f310
commit c1e1091f02
10 changed files with 266 additions and 164 deletions

View File

@@ -440,7 +440,7 @@ class Vector:
Example::
wrappedObject = Object.getAttribute() #this is wrapped data
print wrappedObject.wrapped #prints 'True'
copyOfObject = Object(wrappedObject) #creates a copy of the object
copyOfObject = wrappedObject.copy() #creates a copy of the object
secondPointer = wrappedObject #creates a second pointer to the same data
print wrappedObject.attribute #prints '5'
secondPointer.attribute = 10
@@ -564,7 +564,7 @@ class Euler:
Example::
wrappedObject = Object.getAttribute() #this is wrapped data
print wrappedObject.wrapped #prints 'True'
copyOfObject = Object(wrappedObject) #creates a copy of the object
copyOfObject = wrappedObject.copy() #creates a copy of the object
secondPointer = wrappedObject #creates a second pointer to the same data
print wrappedObject.attribute #prints '5'
secondPointer.attribute = 10
@@ -652,7 +652,7 @@ class Quaternion:
Example::
wrappedObject = Object.getAttribute() #this is wrapped data
print wrappedObject.wrapped #prints 'True'
copyOfObject = Object(wrappedObject) #creates a copy of the object
copyOfObject = wrappedObject.copy() #creates a copy of the object
secondPointer = wrappedObject #creates a second pointer to the same data
print wrappedObject.attribute #prints '5'
secondPointer.attribute = 10
@@ -760,7 +760,7 @@ class Matrix:
Example::
wrappedObject = Object.getAttribute() #this is wrapped data
print wrappedObject.wrapped #prints 'True'
copyOfObject = Object(wrappedObject) #creates a copy of the object
copyOfObject = wrappedObject.copy() #creates a copy of the object
secondPointer = wrappedObject #creates a second pointer to the same data
print wrappedObject.attribute #prints '5'
secondPointer.attribute = 10

View File

@@ -30,6 +30,7 @@
#include "SCA_LogicManager.h"
#include "SCA_IActuator.h"
#include "SCA_ISensor.h"
#include "PyObjectPlus.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
@@ -207,3 +208,176 @@ void SCA_IController::ApplyState(unsigned int state)
}
}
/* Python api */
PyTypeObject SCA_IController::Type = {
#if (PY_VERSION_HEX >= 0x02060000)
PyVarObject_HEAD_INIT(NULL, 0)
#else
/* python 2.5 and below */
PyObject_HEAD_INIT( NULL ) /* required py macro */
0, /* ob_size */
#endif
"SCA_IController",
sizeof(PyObjectPlus_Proxy),
0,
py_base_dealloc,
0,
0,
0,
0,
py_base_repr,
0,0,0,0,0,0,
py_base_getattro,
py_base_setattro,
0,0,0,0,0,0,0,0,0,
Methods
};
PyParentObject SCA_IController::Parents[] = {
&SCA_IController::Type,
&CValue::Type,
NULL
};
PyMethodDef SCA_IController::Methods[] = {
{"getActuator", (PyCFunction) SCA_IController::sPyGetActuator, METH_O},
{"getSensor", (PyCFunction) SCA_IController::sPyGetSensor, METH_O},
//Deprecated functions ------>
{"getSensors", (PyCFunction) SCA_IController::sPyGetSensors, METH_NOARGS},
{"getActuators", (PyCFunction) SCA_IController::sPyGetActuators, METH_NOARGS},
{"getState", (PyCFunction) SCA_IController::sPyGetState, METH_NOARGS},
//<----- Deprecated
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_IController::Attributes[] = {
KX_PYATTRIBUTE_RO_FUNCTION("state", SCA_IController, pyattr_get_state),
KX_PYATTRIBUTE_RO_FUNCTION("sensors", SCA_IController, pyattr_get_sensors),
KX_PYATTRIBUTE_RO_FUNCTION("actuators", SCA_IController, pyattr_get_actuators),
{ NULL } //Sentinel
};
PyObject* SCA_IController::py_getattro(PyObject *attr)
{
py_getattro_up(SCA_ILogicBrick);
}
PyObject* SCA_IController::py_getattro_dict() {
py_getattro_dict_up(SCA_ILogicBrick);
}
int SCA_IController::py_setattro(PyObject *attr, PyObject *value)
{
py_setattro_up(SCA_ILogicBrick);
}
PyObject* SCA_IController::PyGetActuators()
{
ShowDeprecationWarning("getActuators()", "the actuators property");
PyObject* resultlist = PyList_New(m_linkedactuators.size());
for (unsigned int index=0;index<m_linkedactuators.size();index++)
{
PyList_SET_ITEM(resultlist,index, m_linkedactuators[index]->GetProxy());
}
return resultlist;
}
PyObject* SCA_IController::PyGetSensor(PyObject* value)
{
char *scriptArg = PyString_AsString(value);
if (scriptArg==NULL) {
PyErr_SetString(PyExc_TypeError, "controller.getSensor(string): Python Controller, expected a string (sensor name)");
return NULL;
}
for (unsigned int index=0;index<m_linkedsensors.size();index++)
{
SCA_ISensor* sensor = m_linkedsensors[index];
STR_String realname = sensor->GetName();
if (realname == scriptArg)
{
return sensor->GetProxy();
}
}
PyErr_Format(PyExc_AttributeError, "controller.getSensor(string): Python Controller, unable to find requested sensor \"%s\"", scriptArg);
return NULL;
}
PyObject* SCA_IController::PyGetActuator(PyObject* value)
{
char *scriptArg = PyString_AsString(value);
if (scriptArg==NULL) {
PyErr_SetString(PyExc_TypeError, "controller.getActuator(string): Python Controller, expected a string (actuator name)");
return NULL;
}
for (unsigned int index=0;index<m_linkedactuators.size();index++)
{
SCA_IActuator* actua = m_linkedactuators[index];
if (actua->GetName() == scriptArg)
{
return actua->GetProxy();
}
}
PyErr_Format(PyExc_AttributeError, "controller.getActuator(string): Python Controller, unable to find requested actuator \"%s\"", scriptArg);
return NULL;
}
PyObject* SCA_IController::PyGetSensors()
{
ShowDeprecationWarning("getSensors()", "the sensors property");
PyObject* resultlist = PyList_New(m_linkedsensors.size());
for (unsigned int index=0;index<m_linkedsensors.size();index++)
{
PyList_SET_ITEM(resultlist,index, m_linkedsensors[index]->GetProxy());
}
return resultlist;
}
PyObject* SCA_IController::PyGetState()
{
ShowDeprecationWarning("getState()", "the state property");
return PyInt_FromLong(m_statemask);
}
PyObject* SCA_IController::pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
SCA_IController* self= static_cast<SCA_IController*>(self_v);
return PyInt_FromLong(self->m_statemask);
}
PyObject* SCA_IController::pyattr_get_sensors(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
SCA_IController* self= static_cast<SCA_IController*>(self_v);
vector<SCA_ISensor*> linkedsensors = self->GetLinkedSensors();
PyObject* resultlist = PyList_New(linkedsensors.size());
for (unsigned int index=0;index<linkedsensors.size();index++)
PyList_SET_ITEM(resultlist,index, linkedsensors[index]->GetProxy());
return resultlist;
}
PyObject* SCA_IController::pyattr_get_actuators(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
SCA_IController* self= static_cast<SCA_IController*>(self_v);
vector<SCA_IActuator*> linkedactuators = self->GetLinkedActuators();
PyObject* resultlist = PyList_New(linkedactuators.size());
for (unsigned int index=0;index<linkedactuators.size();index++)
PyList_SET_ITEM(resultlist,index, linkedactuators[index]->GetProxy());
return resultlist;
}

View File

@@ -30,9 +30,11 @@
#define __KX_ICONTROLLER
#include "SCA_ILogicBrick.h"
#include "PyObjectPlus.h"
class SCA_IController : public SCA_ILogicBrick
{
Py_Header;
protected:
std::vector<class SCA_ISensor*> m_linkedsensors;
std::vector<class SCA_IActuator*> m_linkedactuators;
@@ -51,7 +53,20 @@ public:
void UnlinkSensor(class SCA_ISensor* sensor);
void SetState(unsigned int state) { m_statemask = state; }
void ApplyState(unsigned int state);
virtual PyObject* py_getattro(PyObject *attr);
virtual PyObject* py_getattro_dict();
virtual int py_setattro(PyObject *attr, PyObject *value);
KX_PYMETHOD_NOARGS(SCA_IController,GetSensors);
KX_PYMETHOD_NOARGS(SCA_IController,GetActuators);
KX_PYMETHOD_O(SCA_IController,GetSensor);
KX_PYMETHOD_O(SCA_IController,GetActuator);
KX_PYMETHOD_NOARGS(SCA_IController,GetState);
static PyObject* pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
static PyObject* pyattr_get_sensors(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
static PyObject* pyattr_get_actuators(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
};

View File

@@ -157,10 +157,6 @@ int SCA_PythonController::IsTriggered(class SCA_ISensor* sensor)
return 0;
}
#if 0
static const char* sPyGetCurrentController__doc__;
#endif
/* warning, self is not the SCA_PythonController, its a PyObjectPlus_Proxy */
PyObject* SCA_PythonController::sPyGetCurrentController(PyObject *self)
{
@@ -189,7 +185,7 @@ SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value)
}
}
else if (BGE_PROXY_CHECK_TYPE(value)) {
PyObjectPlus *value_plus= BGE_PROXY_REF(value); /* Expecting an actuator type */ // XXX TODO - CHECK TYPE
PyObjectPlus *value_plus= BGE_PROXY_REF(value);
for(it = lacts.begin(); it!= lacts.end(); it++) {
if( static_cast<SCA_IActuator*>(value_plus) == (*it) ) {
return *it;
@@ -205,13 +201,11 @@ SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value)
return false;
}
#if 0
static const char* sPyAddActiveActuator__doc__;
#endif
/* warning, self is not the SCA_PythonController, its a PyObjectPlus_Proxy */
PyObject* SCA_PythonController::sPyAddActiveActuator(PyObject* self, PyObject* args)
{
ShowDeprecationWarning("GameLogic.addActiveActuator(act, bool)", "controller.activate(act) or controller.deactivate(act)");
PyObject* ob1;
int activate;
if (!PyArg_ParseTuple(args, "Oi:addActiveActuator", &ob1,&activate))
@@ -227,10 +221,8 @@ PyObject* SCA_PythonController::sPyAddActiveActuator(PyObject* self, PyObject* a
Py_RETURN_NONE;
}
const char* SCA_PythonController::sPyGetCurrentController__doc__ = "getCurrentController()";
const char* SCA_PythonController::sPyAddActiveActuator__doc__= "addActiveActuator(actuator,bool)";
const char SCA_PythonController::GetActuators_doc[] = "getActuator";
PyTypeObject SCA_PythonController::Type = {
#if (PY_VERSION_HEX >= 0x02060000)
@@ -266,20 +258,14 @@ PyMethodDef SCA_PythonController::Methods[] = {
{"activate", (PyCFunction) SCA_PythonController::sPyActivate, METH_O},
{"deactivate", (PyCFunction) SCA_PythonController::sPyDeActivate, METH_O},
{"getActuators", (PyCFunction) SCA_PythonController::sPyGetActuators, METH_NOARGS, (PY_METHODCHAR)SCA_PythonController::GetActuators_doc},
{"getActuator", (PyCFunction) SCA_PythonController::sPyGetActuator, METH_O, (PY_METHODCHAR)SCA_PythonController::GetActuator_doc},
{"getSensors", (PyCFunction) SCA_PythonController::sPyGetSensors, METH_NOARGS, (PY_METHODCHAR)SCA_PythonController::GetSensors_doc},
{"getSensor", (PyCFunction) SCA_PythonController::sPyGetSensor, METH_O, (PY_METHODCHAR)SCA_PythonController::GetSensor_doc},
//Deprecated functions ------>
{"setScript", (PyCFunction) SCA_PythonController::sPySetScript, METH_O},
{"getScript", (PyCFunction) SCA_PythonController::sPyGetScript, METH_NOARGS},
{"getState", (PyCFunction) SCA_PythonController::sPyGetState, METH_NOARGS},
//<----- Deprecated
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_PythonController::Attributes[] = {
KX_PYATTRIBUTE_RO_FUNCTION("state", SCA_PythonController, pyattr_get_state),
KX_PYATTRIBUTE_RW_FUNCTION("script", SCA_PythonController, pyattr_get_script, pyattr_set_script),
{ NULL } //Sentinel
};
@@ -498,6 +484,10 @@ PyObject* SCA_PythonController::py_getattro(PyObject *attr)
py_getattro_up(SCA_IController);
}
PyObject* SCA_PythonController::py_getattro_dict() {
py_getattro_dict_up(SCA_IController);
}
int SCA_PythonController::py_setattro(PyObject *attr, PyObject *value)
{
py_setattro_up(SCA_IController);
@@ -527,84 +517,6 @@ PyObject* SCA_PythonController::PyDeActivate(PyObject *value)
Py_RETURN_NONE;
}
PyObject* SCA_PythonController::PyGetActuators()
{
PyObject* resultlist = PyList_New(m_linkedactuators.size());
for (unsigned int index=0;index<m_linkedactuators.size();index++)
{
PyList_SET_ITEM(resultlist,index, m_linkedactuators[index]->GetProxy());
}
return resultlist;
}
const char SCA_PythonController::GetSensor_doc[] =
"getSensor (char sensorname) return linked sensor that is named [sensorname]\n";
PyObject*
SCA_PythonController::PyGetSensor(PyObject* value)
{
char *scriptArg = PyString_AsString(value);
if (scriptArg==NULL) {
PyErr_SetString(PyExc_TypeError, "controller.getSensor(string): Python Controller, expected a string (sensor name)");
return NULL;
}
for (unsigned int index=0;index<m_linkedsensors.size();index++)
{
SCA_ISensor* sensor = m_linkedsensors[index];
STR_String realname = sensor->GetName();
if (realname == scriptArg)
{
return sensor->GetProxy();
}
}
PyErr_Format(PyExc_AttributeError, "controller.getSensor(string): Python Controller, unable to find requested sensor \"%s\"", scriptArg);
return NULL;
}
const char SCA_PythonController::GetActuator_doc[] =
"getActuator (char sensorname) return linked actuator that is named [actuatorname]\n";
PyObject*
SCA_PythonController::PyGetActuator(PyObject* value)
{
char *scriptArg = PyString_AsString(value);
if (scriptArg==NULL) {
PyErr_SetString(PyExc_TypeError, "controller.getActuator(string): Python Controller, expected a string (actuator name)");
return NULL;
}
for (unsigned int index=0;index<m_linkedactuators.size();index++)
{
SCA_IActuator* actua = m_linkedactuators[index];
if (actua->GetName() == scriptArg)
{
return actua->GetProxy();
}
}
PyErr_Format(PyExc_AttributeError, "controller.getActuator(string): Python Controller, unable to find requested actuator \"%s\"", scriptArg);
return NULL;
}
const char SCA_PythonController::GetSensors_doc[] = "getSensors returns a list of all attached sensors";
PyObject*
SCA_PythonController::PyGetSensors()
{
PyObject* resultlist = PyList_New(m_linkedsensors.size());
for (unsigned int index=0;index<m_linkedsensors.size();index++)
{
PyList_SET_ITEM(resultlist,index, m_linkedsensors[index]->GetProxy());
}
return resultlist;
}
/* 1. getScript */
PyObject* SCA_PythonController::PyGetScript()
{
@@ -632,19 +544,6 @@ PyObject* SCA_PythonController::PySetScript(PyObject* value)
Py_RETURN_NONE;
}
/* 1. getScript */
PyObject* SCA_PythonController::PyGetState()
{
ShowDeprecationWarning("getState()", "the state property");
return PyInt_FromLong(m_statemask);
}
PyObject* SCA_PythonController::pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
SCA_PythonController* self= static_cast<SCA_PythonController*>(self_v);
return PyInt_FromLong(self->m_statemask);
}
PyObject* SCA_PythonController::pyattr_get_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
SCA_PythonController* self= static_cast<SCA_PythonController*>(self_v);

View File

@@ -96,21 +96,18 @@ class SCA_PythonController : public SCA_IController
static PyObject* sPyAddActiveActuator(PyObject* self,
PyObject* args);
static SCA_IActuator* LinkedActuatorFromPy(PyObject *value);
virtual PyObject* py_getattro(PyObject *attr);
virtual PyObject* py_getattro_dict();
virtual int py_setattro(PyObject *attr, PyObject *value);
KX_PYMETHOD_O(SCA_PythonController,Activate);
KX_PYMETHOD_O(SCA_PythonController,DeActivate);
KX_PYMETHOD_DOC_NOARGS(SCA_PythonController,GetSensors);
KX_PYMETHOD_DOC_NOARGS(SCA_PythonController,GetActuators);
KX_PYMETHOD_DOC_O(SCA_PythonController,GetSensor);
KX_PYMETHOD_DOC_O(SCA_PythonController,GetActuator);
KX_PYMETHOD_O(SCA_PythonController,SetScript);
KX_PYMETHOD_NOARGS(SCA_PythonController,GetScript);
KX_PYMETHOD_NOARGS(SCA_PythonController,GetState);
static PyObject* pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
static PyObject* pyattr_get_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
static int pyattr_set_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);

View File

@@ -84,6 +84,7 @@
#include "SCA_PropertySensor.h"
#include "SCA_PythonController.h"
#include "SCA_RandomActuator.h"
#include "SCA_IController.h"
void initPyObjectPlusType(PyTypeObject **parents)
@@ -225,9 +226,7 @@ void initPyTypes(void)
PyType_Ready_Attr(dict, SCA_RandomSensor);
PyType_Ready_Attr(dict, SCA_XNORController);
PyType_Ready_Attr(dict, SCA_XORController);
PyType_Ready_Attr(dict, SCA_IController);
}
#endif

View File

@@ -59,7 +59,7 @@ Documentation for the GameLogic Module.
actuator = co.getActuator("actuatorname")
# Activate an actuator
GameLogic.addActiveActuator(actuator, True)
controller.activate(actuator)
See the actuator's reference for available methods:
- L{2DFilterActuator<SCA_2DFilterActuator.SCA_2DFilterActuator>}
@@ -307,6 +307,8 @@ KX_SCENE_RESUME: See L{KX_SceneActuator}
@var VIEWMATRIX_INVERSE:
@var VIEWMATRIX_INVERSETRANSPOSE:
@var VIEWMATRIX_TRANSPOSE:
@group Deprecated: addActiveActuator
"""
# TODO
@@ -335,7 +337,7 @@ def getSceneList():
"""
def addActiveActuator(actuator, activate):
"""
Activates the given actuator.
Activates the given actuator. (B{deprecated})
@type actuator: L{SCA_IActuator} or the actuator name as a string.
@type activate: boolean

View File

@@ -20,7 +20,7 @@ class KX_SceneActuator(SCA_IActuator):
@type camera: L{KX_Camera} on read, string or L{KX_Camera} on write
@ivar useRestart: Set flag to True to restart the sene
@type useRestart: bool
@type mode: The mode of the actuator
@ivar mode: The mode of the actuator
@type mode: int from 0 to 5 L{GameLogic.Scene Actuator}
"""
def setUseRestart(flag):

View File

@@ -5,5 +5,57 @@ from SCA_ILogicBrick import *
class SCA_IController(SCA_ILogicBrick):
"""
Base class for all controller logic bricks.
@ivar state: the controllers state bitmask.
This can be used with the GameObject's state to test if the controller is active.
@type state: int bitmask
@ivar sensors: a list of sensors linked to this controller
- note: the sensors are not necessarily owned by the same object.
- note: when objects are instanced in dupligroups links may be lost from objects outside the dupligroup.
@type sensors: list
@ivar actuators: a list of actuators linked to this controller.
- note: the sensors are not necessarily owned by the same object.
- note: when objects are instanced in dupligroups links may be lost from objects outside the dupligroup.
@type actuators: list
@group Deprecated: getState, getSensors, getActuators
"""
def getState():
"""
DEPRECATED: use the state property
Get the controllers state bitmask, this can be used with the GameObject's state to test if the the controller is active.
This for instance will always be true however you could compare with a previous state to see when the state was activated.
GameLogic.getCurrentController().getState() & GameLogic.getCurrentController().getOwner().getState()
@rtype: int
"""
def getSensors():
"""
DEPRECATED: use the sensors property
Gets a list of all sensors attached to this controller.
@rtype: list [L{SCA_ISensor}]
"""
def getSensor(name):
"""
Gets the named linked sensor.
@type name: string
@rtype: L{SCA_ISensor}
"""
def getActuators():
"""
DEPRECATED: use the actuators property
Gets a list of all actuators linked to this controller.
@rtype: list [L{SCA_IActuator}]
"""
def getActuator(name):
"""
Gets the named linked actuator.
@type name: string
@rtype: L{SCA_IActuator}
"""

View File

@@ -11,9 +11,8 @@ class SCA_PythonController(SCA_IController):
@ivar script: the Python script this controller executes
@type script: string, read-only
@ivar state: the controllers state bitmask.
This can be used with the GameObject's state to test if the controller is active.
@type state: integer
@group Deprecated: getScript, setScript
"""
def activate(actuator):
"""
@@ -25,33 +24,6 @@ class SCA_PythonController(SCA_IController):
Deactivates an actuator attached to this controller.
@type actuator: actuator or the actuator name as a string
"""
def getSensors():
"""
Gets a list of all sensors attached to this controller.
@rtype: list [L{SCA_ISensor}]
"""
def getSensor(name):
"""
Gets the named linked sensor.
@type name: string
@rtype: L{SCA_ISensor}
"""
def getActuators():
"""
Gets a list of all actuators linked to this controller.
@rtype: list [L{SCA_IActuator}]
"""
def getActuator(name):
"""
Gets the named linked actuator.
@type name: string
@rtype: L{SCA_IActuator}
"""
def getScript():
"""
DEPRECATED: use the script property
@@ -61,17 +33,9 @@ class SCA_PythonController(SCA_IController):
"""
def setScript(script):
"""
DEPRECATED: use the script property
Sets the Python script this controller executes.
@type script: string.
"""
def getState():
"""
DEPRECATED: use the state property
Get the controllers state bitmask, this can be used with the GameObject's state to test if the the controller is active.
This for instance will always be true however you could compare with a previous state to see when the state was activated.
GameLogic.getCurrentController().getState() & GameLogic.getCurrentController().getOwner().getState()
@rtype: int
"""