diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-04-12 18:22:51 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-04-12 18:22:51 +0400 |
commit | 332032fb9925091da49efa2e9c3653bdb3e68cba (patch) | |
tree | b482f26a2fe9fa953fbc501ddf39cb927ac0c2a6 /source/gameengine/Ketsji/KX_GameObject.h | |
parent | ee24c829b56ebddb47ae2bcfe30db5d857610450 (diff) |
BGE Python API
Support for assigning any Type to a KX_GameObject
so you can do...
gameOb.follow = otherGameOb
gameOb[otherGameOb] = distanceTo
gameOb["path"] = [(x,y,x), (x,y,x)]
del gameOb[mesh]
* types that cannot be converted into CValue types are written into the KX_GameObject dict
* the KX_GameObject dict is only initialized when needed
* Python properties in this dict cannot be accessed by logic bricks
* dir(ob) and ob.getPropertyNames() return items from both CValue and Py dictionary properties.
Also found that CType was converting python lists to CType Lists but very buggy, would crash after printing the list most times.
Use python lists instead since logic bricks dont deal with lists.
Diffstat (limited to 'source/gameengine/Ketsji/KX_GameObject.h')
-rw-r--r-- | source/gameengine/Ketsji/KX_GameObject.h | 119 |
1 files changed, 118 insertions, 1 deletions
diff --git a/source/gameengine/Ketsji/KX_GameObject.h b/source/gameengine/Ketsji/KX_GameObject.h index 58f2c56c1da..172272150c0 100644 --- a/source/gameengine/Ketsji/KX_GameObject.h +++ b/source/gameengine/Ketsji/KX_GameObject.h @@ -103,6 +103,26 @@ protected: public: bool m_isDeformable; + // Python attributes that wont convert into CValue + // + // there are 2 places attributes can be stored, in the CValue, + // where attributes are converted into BGE's CValue types + // these can be used with property actuators + // + // For the python API, For types that cannot be converted into CValues (lists, dicts, GameObjects) + // these will be put into "m_attrlist", logic bricks cannot access them. + // + // rules for setting attributes. + // + // * there should NEVER be a CValue and a m_attrlist attribute with matching names. get/sets make sure of this. + // * if CValue conversion fails, use a PyObject in "m_attrlist" + // * when assigning a value, first see if it can be a CValue, if it can remove the "m_attrlist" and set the CValue + // + + PyObject* m_attrlist; + + + virtual void /* This function should be virtual - derived classed override it */ Relink( GEN_Map<GEN_HashedPtr, void*> *map @@ -768,11 +788,108 @@ public: /** * @section Python interface functions. */ - + virtual PyObject* py_getattro(PyObject *attr); virtual int py_setattro(PyObject *attr, PyObject *value); // py_setattro method virtual PyObject* py_repr(void) { return PyString_FromString(GetName().ReadPtr()); } + /* we need our own getattr and setattr types */ + /* See m_attrlist definition for rules on how this works */ + static PyObject *py_base_getattro_gameobject(PyObject * self, PyObject *attr) + { + PyObject *object= ((KX_GameObject *) self)->py_getattro(attr); + + if (object==NULL && ((KX_GameObject *) self)->m_attrlist) { + /* backup the exception incase the attr doesnt exist in the dict either */ + PyObject *err_type, *err_value, *err_tb; + PyErr_Fetch(&err_type, &err_value, &err_tb); + + object= PyDict_GetItem(((KX_GameObject *) self)->m_attrlist, attr); + if (object) { + Py_INCREF(object); + + PyErr_Clear(); + Py_XDECREF( err_type ); + Py_XDECREF( err_value ); + Py_XDECREF( err_tb ); + } + else { + PyErr_Restore(err_type, err_value, err_tb); /* use the error from the parent function */ + } + } + return object; + } + + static int py_base_setattro_gameobject(PyObject * self, PyObject *attr, PyObject *value) + { + int ret; + + /* Delete the item */ + if (value==NULL) + { + ret= ((PyObjectPlus*) self)->py_delattro(attr); + + if (ret != 0) /* CValue attribute failed, try KX_GameObject m_attrlist dict */ + { + if (((KX_GameObject *) self)->m_attrlist) + { + /* backup the exception incase the attr doesnt exist in the dict either */ + PyObject *err_type, *err_value, *err_tb; + PyErr_Fetch(&err_type, &err_value, &err_tb); + + if (PyDict_DelItem(((KX_GameObject *) self)->m_attrlist, attr) == 0) + { + ret= 0; + PyErr_Clear(); + Py_XDECREF( err_type ); + Py_XDECREF( err_value ); + Py_XDECREF( err_tb ); + } + else { + PyErr_Restore(err_type, err_value, err_tb); /* use the error from the parent function */ + } + } + } + return ret; + } + + + ret= ((PyObjectPlus*) self)->py_setattro(attr, value); + + if (ret==PY_SET_ATTR_SUCCESS) { + /* remove attribute in our own dict to avoid double ups */ + if (((KX_GameObject *) self)->m_attrlist) { + if (PyDict_DelItem(((KX_GameObject *) self)->m_attrlist, attr) != 0) + PyErr_Clear(); + } + } + + if (ret==PY_SET_ATTR_COERCE_FAIL) { + /* CValue attribute exists, remove and add dict value */ + ((KX_GameObject *) self)->RemoveProperty(STR_String(PyString_AsString(attr))); + ret= PY_SET_ATTR_MISSING; + } + + if (ret==PY_SET_ATTR_MISSING) { + /* Lazy initialization */ + if (((KX_GameObject *) self)->m_attrlist==NULL) + ((KX_GameObject *) self)->m_attrlist = PyDict_New(); + + if (PyDict_SetItem(((KX_GameObject *) self)->m_attrlist, attr, value)==0) { + PyErr_Clear(); + ret= PY_SET_ATTR_SUCCESS; + } + else { + PyErr_Format(PyExc_AttributeError, "failed assigning value to KX_GameObject internal dictionary"); + ret= PY_SET_ATTR_FAIL; + } + } + + return ret; + } + + + KX_PYMETHOD_NOARGS(KX_GameObject,GetPosition); KX_PYMETHOD_O(KX_GameObject,SetPosition); KX_PYMETHOD_O(KX_GameObject,SetWorldPosition); |