diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-04-19 16:46:39 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-04-19 16:46:39 +0400 |
commit | 8d2cb5bea44f4245dd17f2d82cbd0251d8090fd5 (patch) | |
tree | b28e45f1edaf083c15d2176079836a4497685e57 /source/gameengine/GameLogic | |
parent | 92cea7c1b1540d11ed9729bacfabd23ccb7a79c7 (diff) |
BGE Python API
This changes how the BGE classes and Python work together, which hasnt changed since blender went opensource.
The main difference is PyObjectPlus - the base class for most game engine classes, no longer inherit from PyObject, and cannot be cast to a PyObject.
This has the advantage that the BGE does not have to keep 2 reference counts valid for C++ and Python.
Previously C++ classes would never be freed while python held a reference, however this reference could be problematic eg: a GameObject that isnt in a scene anymore should not be used by python, doing so could even crash blender in some cases.
Instead PyObjectPlus has a member "PyObject *m_proxy" which is lazily initialized when python needs it. m_proxy reference counts are managed by python, though it should never be freed while the C++ class exists since it holds a reference to avoid making and freeing it all the time.
When the C++ class is free'd it sets the m_proxy reference to NULL, If python accesses this variable it will raise a RuntimeError, (check the isValid attribute to see if its valid without raising an error).
- This replaces the m_zombie bool and IsZombie() tests added recently.
In python return values that used to be..
return value->AddRef();
Are now
return value->GetProxy();
or...
return value->NewProxy(true); // true means python owns this C++ value which will be deleted when the PyObject is freed
Diffstat (limited to 'source/gameengine/GameLogic')
-rw-r--r-- | source/gameengine/GameLogic/SCA_ILogicBrick.cpp | 3 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_PythonController.cpp | 16 |
2 files changed, 9 insertions, 10 deletions
diff --git a/source/gameengine/GameLogic/SCA_ILogicBrick.cpp b/source/gameengine/GameLogic/SCA_ILogicBrick.cpp index 46e132c65fc..7fa55cfb1ee 100644 --- a/source/gameengine/GameLogic/SCA_ILogicBrick.cpp +++ b/source/gameengine/GameLogic/SCA_ILogicBrick.cpp @@ -294,8 +294,7 @@ PyObject* SCA_ILogicBrick::PyGetOwner(PyObject* self) CValue* parent = GetParent(); if (parent) { - parent->AddRef(); - return parent; + return parent->GetProxy(); } printf("ERROR: Python scriptblock without owner\n"); diff --git a/source/gameengine/GameLogic/SCA_PythonController.cpp b/source/gameengine/GameLogic/SCA_PythonController.cpp index 1c5b597f937..687ae421af1 100644 --- a/source/gameengine/GameLogic/SCA_PythonController.cpp +++ b/source/gameengine/GameLogic/SCA_PythonController.cpp @@ -157,7 +157,7 @@ static const char* sPyGetCurrentController__doc__; PyObject* SCA_PythonController::sPyGetCurrentController(PyObject* self) { - return m_sCurrentController->AddRef(); + return m_sCurrentController->GetProxy(); } SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value) @@ -176,10 +176,10 @@ SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value) } } } - else { - /* Expecting an actuator type */ + else if (BGE_PROXY_CHECK_TYPE(value)) { + PyObjectPlus *value_plus= BGE_PROXY_REF(value); /* Expecting an actuator type */ // XXX TODO - CHECK TYPE for(it = lacts.begin(); it!= lacts.end(); it++) { - if( static_cast<SCA_IActuator*>(value) == (*it) ) { + if( static_cast<SCA_IActuator*>(value_plus) == (*it) ) { return *it; } } @@ -413,7 +413,7 @@ PyObject* SCA_PythonController::PyGetActuators(PyObject* self) 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]->AddRef()); + PyList_SET_ITEM(resultlist,index, m_linkedactuators[index]->GetProxy()); } return resultlist; @@ -437,7 +437,7 @@ SCA_PythonController::PyGetSensor(PyObject* self, PyObject* value) STR_String realname = sensor->GetName(); if (realname == scriptArg) { - return sensor->AddRef(); + return sensor->GetProxy(); } } @@ -466,7 +466,7 @@ SCA_PythonController::PyGetActuator(PyObject* self, PyObject* value) SCA_IActuator* actua = m_linkedactuators[index]; if (actua->GetName() == scriptArg) { - return actua->AddRef(); + return actua->GetProxy(); } } @@ -484,7 +484,7 @@ SCA_PythonController::PyGetSensors(PyObject* self) 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]->AddRef()); + PyList_SET_ITEM(resultlist,index, m_linkedsensors[index]->GetProxy()); } return resultlist; |