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
This commit is contained in:
@@ -359,8 +359,7 @@ static STR_String gPyGetCurrentScene_doc =
|
||||
"Gets a reference to the current scene.\n";
|
||||
static PyObject* gPyGetCurrentScene(PyObject* self)
|
||||
{
|
||||
Py_INCREF(gp_KetsjiScene);
|
||||
return (PyObject*) gp_KetsjiScene;
|
||||
return gp_KetsjiScene->GetProxy();
|
||||
}
|
||||
|
||||
static STR_String gPyGetSceneList_doc =
|
||||
@@ -369,7 +368,6 @@ static STR_String gPyGetSceneList_doc =
|
||||
static PyObject* gPyGetSceneList(PyObject* self)
|
||||
{
|
||||
KX_KetsjiEngine* m_engine = KX_GetActiveEngine();
|
||||
//CListValue* list = new CListValue();
|
||||
PyObject* list;
|
||||
KX_SceneList* scenes = m_engine->CurrentScenes();
|
||||
int numScenes = scenes->size();
|
||||
@@ -380,13 +378,10 @@ static PyObject* gPyGetSceneList(PyObject* self)
|
||||
for (i=0;i<numScenes;i++)
|
||||
{
|
||||
KX_Scene* scene = scenes->at(i);
|
||||
//list->Add(scene);
|
||||
PyList_SET_ITEM(list, i, scene);
|
||||
Py_INCREF(scene);
|
||||
|
||||
PyList_SET_ITEM(list, i, scene->GetProxy());
|
||||
}
|
||||
|
||||
return (PyObject*)list;
|
||||
return list;
|
||||
}
|
||||
|
||||
static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
|
||||
|
||||
Reference in New Issue
Block a user