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.
This commit is contained in:
@@ -233,6 +233,7 @@ PyMethodDef CListValue::Methods[] = {
|
|||||||
{"reverse", (PyCFunction)CListValue::sPyreverse,METH_NOARGS},
|
{"reverse", (PyCFunction)CListValue::sPyreverse,METH_NOARGS},
|
||||||
{"index", (PyCFunction)CListValue::sPyindex,METH_O},
|
{"index", (PyCFunction)CListValue::sPyindex,METH_O},
|
||||||
{"count", (PyCFunction)CListValue::sPycount,METH_O},
|
{"count", (PyCFunction)CListValue::sPycount,METH_O},
|
||||||
|
{"from_id", (PyCFunction)CListValue::sPyfrom_id,METH_O},
|
||||||
|
|
||||||
{NULL,NULL} //Sentinel
|
{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<numelem;i++)
|
||||||
|
{
|
||||||
|
if (reinterpret_cast<BGE_ID_TYPE>(static_cast<PyObject*>(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
|
* Some stuff taken from the header
|
||||||
* --------------------------------------------------------------------- */
|
* --------------------------------------------------------------------- */
|
||||||
|
@@ -71,6 +71,7 @@ public:
|
|||||||
KX_PYMETHOD_NOARGS(CListValue,reverse);
|
KX_PYMETHOD_NOARGS(CListValue,reverse);
|
||||||
KX_PYMETHOD_O(CListValue,index);
|
KX_PYMETHOD_O(CListValue,index);
|
||||||
KX_PYMETHOD_O(CListValue,count);
|
KX_PYMETHOD_O(CListValue,count);
|
||||||
|
KX_PYMETHOD_O(CListValue,from_id);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@@ -404,9 +404,9 @@ public:
|
|||||||
// }; // decref method
|
// }; // decref method
|
||||||
|
|
||||||
virtual PyObject *py_getattro(PyObject *attr); // py_getattro 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);
|
static PyObject* py_get_attrdef(void *self, const PyAttributeDef *attrdef);
|
||||||
@@ -419,13 +419,12 @@ public:
|
|||||||
|
|
||||||
virtual int py_delattro(PyObject *attr);
|
virtual int py_delattro(PyObject *attr);
|
||||||
virtual int py_setattro(PyObject *attr, PyObject *value); // py_setattro method
|
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.
|
static int py_base_setattro(PyObject *self, PyObject *attr, PyObject *value) // the PyType should reference this
|
||||||
PyObject *attr,
|
{
|
||||||
PyObject *value)
|
|
||||||
{
|
|
||||||
if (value==NULL)
|
if (value==NULL)
|
||||||
return ((PyObjectPlus*) PyObj)->py_delattro(attr);
|
return ((PyObjectPlus*) self)->py_delattro(attr);
|
||||||
return ((PyObjectPlus*) PyObj)->py_setattro(attr, value);
|
|
||||||
|
return ((PyObjectPlus*) self)->py_setattro(attr, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual PyObject *py_repr(void); // py_repr method
|
virtual PyObject *py_repr(void); // py_repr method
|
||||||
|
@@ -1588,7 +1588,7 @@ PyTypeObject KX_Scene::Type = {
|
|||||||
py_base_repr,
|
py_base_repr,
|
||||||
0,0,0,0,0,0,
|
0,0,0,0,0,0,
|
||||||
py_base_getattro,
|
py_base_getattro,
|
||||||
py_base_setattro,
|
py_base_setattro_scene, /* unlike almost all other types we need out own because user attributes are supported */
|
||||||
0,0,0,0,0,0,0,0,0,
|
0,0,0,0,0,0,0,0,0,
|
||||||
Methods
|
Methods
|
||||||
};
|
};
|
||||||
@@ -1669,11 +1669,9 @@ int KX_Scene::py_delattro(PyObject *attr)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* py_base_setattro_scene deals with setting the dict, it will run if this returns an error */
|
||||||
int KX_Scene::py_setattro(PyObject *attr, PyObject *pyvalue)
|
int KX_Scene::py_setattro(PyObject *attr, PyObject *pyvalue)
|
||||||
{
|
{
|
||||||
if (!PyDict_SetItem(m_attrlist, attr, pyvalue))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return PyObjectPlus::py_setattro(attr, pyvalue);
|
return PyObjectPlus::py_setattro(attr, pyvalue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -590,6 +590,27 @@ public:
|
|||||||
/* for dir(), python3 uses __dir__() */
|
/* for dir(), python3 uses __dir__() */
|
||||||
static PyObject* pyattr_get_dir_dict(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
|
static PyObject* pyattr_get_dir_dict(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
|
||||||
|
|
||||||
|
static int py_base_setattro_scene(PyObject * self, PyObject *attr, PyObject *value)
|
||||||
|
{
|
||||||
|
if (value==NULL)
|
||||||
|
return ((PyObjectPlus*) self)->py_delattro(attr);
|
||||||
|
|
||||||
|
int ret= ((PyObjectPlus*) self)->py_setattro(attr, value);
|
||||||
|
|
||||||
|
if (ret) {
|
||||||
|
if (!PyDict_SetItem(((KX_Scene *) self)->m_attrlist, attr, value)) {
|
||||||
|
PyErr_Clear();
|
||||||
|
ret= 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ret= -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
virtual PyObject* py_getattro(PyObject *attr); /* name, active_camera, gravity, suspended, viewport, framing, activity_culling, activity_culling_radius */
|
virtual PyObject* py_getattro(PyObject *attr); /* name, active_camera, gravity, suspended, viewport, framing, activity_culling, activity_culling_radius */
|
||||||
virtual int py_setattro(PyObject *attr, PyObject *pyvalue);
|
virtual int py_setattro(PyObject *attr, PyObject *pyvalue);
|
||||||
|
@@ -33,7 +33,27 @@ class CListValue: # (PyObjectPlus)
|
|||||||
@rtype: integer
|
@rtype: integer
|
||||||
@return: The index of the value in the list.
|
@return: The index of the value in the list.
|
||||||
"""
|
"""
|
||||||
def reverse(val):
|
def reverse():
|
||||||
"""
|
"""
|
||||||
Reverse the order of the list.
|
Reverse the order of the list.
|
||||||
|
"""
|
||||||
|
def from_id(id):
|
||||||
|
"""
|
||||||
|
This is a funtion especially for the game engine to return a value with a spesific id.
|
||||||
|
|
||||||
|
Since object names are not always unique, the id of an object can be used to get an object from the CValueList.
|
||||||
|
|
||||||
|
Example.
|
||||||
|
|
||||||
|
C{myObID = id(gameObject)}
|
||||||
|
|
||||||
|
C{...}
|
||||||
|
|
||||||
|
C{ob= scene.objects.from_id(myObID)}
|
||||||
|
|
||||||
|
Where myObID is an int or long from the id function.
|
||||||
|
|
||||||
|
This has the advantage that you can store the id in places you could not store a gameObject.
|
||||||
|
|
||||||
|
Warning: the id is derived from a memory location and will be different each time the game engine starts.
|
||||||
"""
|
"""
|
Reference in New Issue
Block a user