From 4cd088b1059afa2e7b998c184b2c9deecd4be4a9 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 11 Apr 2009 20:58:09 +0000 Subject: BGE Py API - setting the scene attributes would always add to the scenes custom dictionary. - new CListValue method from_id(id) so you can store a Game Objects id and use it to get the game object back. ob_id = id(gameOb) ... gameOb = scene.objects.from_id(ob_id) This is useful because names are not always unique. --- source/gameengine/Expressions/ListValue.cpp | 29 ++++++++++++++++++++++++++++ source/gameengine/Expressions/ListValue.h | 1 + source/gameengine/Expressions/PyObjectPlus.h | 15 +++++++------- 3 files changed, 37 insertions(+), 8 deletions(-) (limited to 'source/gameengine/Expressions') diff --git a/source/gameengine/Expressions/ListValue.cpp b/source/gameengine/Expressions/ListValue.cpp index d0aec645468..16b4fbef6b7 100644 --- a/source/gameengine/Expressions/ListValue.cpp +++ b/source/gameengine/Expressions/ListValue.cpp @@ -233,6 +233,7 @@ PyMethodDef CListValue::Methods[] = { {"reverse", (PyCFunction)CListValue::sPyreverse,METH_NOARGS}, {"index", (PyCFunction)CListValue::sPyindex,METH_O}, {"count", (PyCFunction)CListValue::sPycount,METH_O}, + {"from_id", (PyCFunction)CListValue::sPyfrom_id,METH_O}, {NULL,NULL} //Sentinel }; @@ -502,6 +503,34 @@ PyObject* CListValue::Pycount(PyObject* self, PyObject* value) +PyObject* CListValue::Pyfrom_id(PyObject* self, PyObject* value) +{ +#if SIZEOF_VOID_P <= SIZEOF_LONG +#define BGE_ID_TYPE unsigned long + BGE_ID_TYPE id= PyLong_AsUnsignedLong(value); +#else +#define BGE_ID_TYPE unsigned long long + BGE_ID_TYPE id= PyLong_FromUnsignedLongLong(value); +#endif + + if (id==-1 && PyErr_Occurred()) + return NULL; + + int numelem = GetCount(); + for (int i=0;i(static_cast(m_pValueArray[i])) == id) + return GetValue(i); + + } + PyErr_SetString(PyExc_IndexError, "from_id(#), id not found in CValueList"); + return NULL; + +} + +#undef BGE_ID_TYPE + + /* --------------------------------------------------------------------- * Some stuff taken from the header * --------------------------------------------------------------------- */ diff --git a/source/gameengine/Expressions/ListValue.h b/source/gameengine/Expressions/ListValue.h index 1c01c2d221d..6f70acb9367 100644 --- a/source/gameengine/Expressions/ListValue.h +++ b/source/gameengine/Expressions/ListValue.h @@ -71,6 +71,7 @@ public: KX_PYMETHOD_NOARGS(CListValue,reverse); KX_PYMETHOD_O(CListValue,index); KX_PYMETHOD_O(CListValue,count); + KX_PYMETHOD_O(CListValue,from_id); private: diff --git a/source/gameengine/Expressions/PyObjectPlus.h b/source/gameengine/Expressions/PyObjectPlus.h index 786ca1fdc4f..42168461634 100644 --- a/source/gameengine/Expressions/PyObjectPlus.h +++ b/source/gameengine/Expressions/PyObjectPlus.h @@ -404,9 +404,9 @@ public: // }; // decref method virtual PyObject *py_getattro(PyObject *attr); // py_getattro method - static PyObject *py_base_getattro(PyObject * PyObj, PyObject *attr) // This should be the entry in Type. + static PyObject *py_base_getattro(PyObject * self, PyObject *attr) // This should be the entry in Type. { - return ((PyObjectPlus*) PyObj)->py_getattro(attr); + return ((PyObjectPlus*) self)->py_getattro(attr); } static PyObject* py_get_attrdef(void *self, const PyAttributeDef *attrdef); @@ -419,13 +419,12 @@ public: virtual int py_delattro(PyObject *attr); virtual int py_setattro(PyObject *attr, PyObject *value); // py_setattro method - static int py_base_setattro(PyObject *PyObj, // This should be the entry in Type. - PyObject *attr, - PyObject *value) - { + static int py_base_setattro(PyObject *self, PyObject *attr, PyObject *value) // the PyType should reference this + { if (value==NULL) - return ((PyObjectPlus*) PyObj)->py_delattro(attr); - return ((PyObjectPlus*) PyObj)->py_setattro(attr, value); + return ((PyObjectPlus*) self)->py_delattro(attr); + + return ((PyObjectPlus*) self)->py_setattro(attr, value); } virtual PyObject *py_repr(void); // py_repr method -- cgit v1.2.3