BGE Python API
Use 'const char *' rather then the C++ 'STR_String' type for the attribute identifier of python attributes. Each attribute and method access from python was allocating and freeing the string. A simple test with getting an attribute a loop shows this speeds up attribute lookups a bit over 2x.
This commit is contained in:
@@ -233,8 +233,8 @@ PyMethodDef SCA_PythonController::Methods[] = {
|
||||
{"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},
|
||||
{"setScript", (PyCFunction) SCA_PythonController::sPySetScript, METH_O},
|
||||
//Deprecated functions ------>
|
||||
{"setScript", (PyCFunction) SCA_PythonController::sPySetScript, METH_O},
|
||||
{"getScript", (PyCFunction) SCA_PythonController::sPyGetScript, METH_NOARGS},
|
||||
{"getState", (PyCFunction) SCA_PythonController::sPyGetState, METH_NOARGS},
|
||||
//<----- Deprecated
|
||||
@@ -329,25 +329,35 @@ void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
|
||||
|
||||
|
||||
|
||||
PyObject* SCA_PythonController::_getattr(const STR_String& attr)
|
||||
PyObject* SCA_PythonController::_getattr(const char *attr)
|
||||
{
|
||||
if (attr == "script") {
|
||||
return PyString_FromString(m_scriptText);
|
||||
}
|
||||
if (attr == "state") {
|
||||
if (!strcmp(attr,"state")) {
|
||||
return PyInt_FromLong(m_statemask);
|
||||
}
|
||||
if (!strcmp(attr,"script")) {
|
||||
return PyString_FromString(m_scriptText);
|
||||
}
|
||||
_getattr_up(SCA_IController);
|
||||
}
|
||||
|
||||
int SCA_PythonController::_setattr(const STR_String& attr, PyObject *value)
|
||||
int SCA_PythonController::_setattr(const char *attr, PyObject *value)
|
||||
{
|
||||
if (attr == "script") {
|
||||
PyErr_SetString(PyExc_AttributeError, "script is read only, use setScript() to update the script");
|
||||
if (!strcmp(attr,"state")) {
|
||||
PyErr_SetString(PyExc_AttributeError, "state is read only");
|
||||
return 1;
|
||||
}
|
||||
if (attr == "state") {
|
||||
PyErr_SetString(PyExc_AttributeError, "state is read only");
|
||||
if (!strcmp(attr,"script")) {
|
||||
char *scriptArg = PyString_AsString(value);
|
||||
|
||||
if (scriptArg==NULL) {
|
||||
PyErr_SetString(PyExc_TypeError, "expected a string (script name)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* set scripttext sets m_bModified to true,
|
||||
so next time the script is needed, a reparse into byte code is done */
|
||||
this->SetScriptText(scriptArg);
|
||||
|
||||
return 1;
|
||||
}
|
||||
return SCA_IController::_setattr(attr, value);
|
||||
@@ -448,6 +458,9 @@ PyObject* SCA_PythonController::PyGetScript(PyObject* self)
|
||||
PyObject* SCA_PythonController::PySetScript(PyObject* self, PyObject* value)
|
||||
{
|
||||
char *scriptArg = PyString_AsString(value);
|
||||
|
||||
ShowDeprecationWarning("setScript()", "the script property");
|
||||
|
||||
if (scriptArg==NULL) {
|
||||
PyErr_SetString(PyExc_TypeError, "expected a string (script name)");
|
||||
return NULL;
|
||||
|
||||
Reference in New Issue
Block a user