diff options
author | Campbell Barton <ideasman42@gmail.com> | 2008-08-14 12:58:25 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2008-08-14 12:58:25 +0400 |
commit | 47c2271d673173ee93b9d91926de9ea41415d411 (patch) | |
tree | 0a13432423c321cd415a4ea5771e89d7ee56d3d9 /source/gameengine/Ketsji/KX_GameObject.cpp | |
parent | 639f3e12a98f6459cb1f7d948e7a3f2658570b31 (diff) |
Python API get/setObject update for Actuators. (SetParent, AddObject, Camera and TrackTo)
* bugfix for BGE python api - SetParent actuator getObject would segfault if the object was not set.
* Added utility function ConvertPythonToGameObject() that can take a GameObject, string or None and set the game object from this since it was being done in a number of places.
* allow setObject(None), since no object is valid for actuators, Python should be able to set this.
* added optional argument for getObject() so it returns the KX_GameObject rather then its name, would prefer this be default but it could break existing games.
Diffstat (limited to 'source/gameengine/Ketsji/KX_GameObject.cpp')
-rw-r--r-- | source/gameengine/Ketsji/KX_GameObject.cpp | 84 |
1 files changed, 70 insertions, 14 deletions
diff --git a/source/gameengine/Ketsji/KX_GameObject.cpp b/source/gameengine/Ketsji/KX_GameObject.cpp index ed05cd98ed0..700cc00e996 100644 --- a/source/gameengine/Ketsji/KX_GameObject.cpp +++ b/source/gameengine/Ketsji/KX_GameObject.cpp @@ -1551,9 +1551,9 @@ KX_PYMETHODDEF_DOC(KX_GameObject, getDistanceTo, PyErr_Clear(); PyObject *pyother; - if (PyArg_ParseTuple(args, "O!", &KX_GameObject::Type, &pyother)) + KX_GameObject *other; + if (PyArg_ParseTuple(args, "O", &pyother) && ConvertPythonToGameObject(pyother, &other, false)) { - KX_GameObject *other = static_cast<KX_GameObject*>(pyother); return PyFloat_FromDouble(NodeGetWorldPosition().distance(other->NodeGetWorldPosition())); } @@ -1574,11 +1574,12 @@ KX_PYMETHODDEF_DOC(KX_GameObject, getVectTo, if (!PyVecArgTo(args, toPoint)) { PyErr_Clear(); - if (PyArg_ParseTuple(args, "O!", &KX_GameObject::Type, &pyother)) + + KX_GameObject *other; + if (PyArg_ParseTuple(args, "O", &pyother) && ConvertPythonToGameObject(pyother, &other, false)) { - KX_GameObject *other = static_cast<KX_GameObject*>(pyother); toPoint = other->NodeGetWorldPosition(); - }else + } else { PyErr_SetString(PyExc_TypeError, "Expected a 3D Vector or GameObject type"); return NULL; @@ -1648,12 +1649,15 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCastTo, { KX_GameObject *other; PyErr_Clear(); - if (!PyType_IsSubtype(pyarg->ob_type, &KX_GameObject::Type)) { + + if (ConvertPythonToGameObject(pyarg, &other, false)) + { + toPoint = other->NodeGetWorldPosition(); + } else + { PyErr_SetString(PyExc_TypeError, "the first argument to rayCastTo must be a vector or a KX_GameObject"); return NULL; } - other = static_cast<KX_GameObject*>(pyarg); - toPoint = other->NodeGetWorldPosition(); } MT_Point3 fromPoint = NodeGetWorldPosition(); if (dist != 0.0f) @@ -1712,12 +1716,15 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCast, if (!PyVecTo(pyto, toPoint)) { PyErr_Clear(); - if (!PyType_IsSubtype(pyto->ob_type, &KX_GameObject::Type)) { + + if (ConvertPythonToGameObject(pyto, &other, false)) + { + toPoint = other->NodeGetWorldPosition(); + } else + { PyErr_SetString(PyExc_TypeError, "the first argument to rayCast must be a vector or a KX_GameObject"); return NULL; } - other = static_cast<KX_GameObject*>(pyto); - toPoint = other->NodeGetWorldPosition(); } if (!pyfrom || pyfrom == Py_None) { @@ -1726,12 +1733,15 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCast, else if (!PyVecTo(pyfrom, fromPoint)) { PyErr_Clear(); - if (!PyType_IsSubtype(pyfrom->ob_type, &KX_GameObject::Type)) { + + if (ConvertPythonToGameObject(pyfrom, &other, false)) + { + fromPoint = other->NodeGetWorldPosition(); + } else + { PyErr_SetString(PyExc_TypeError, "the second optional argument to rayCast must be a vector or a KX_GameObject"); return NULL; } - other = static_cast<KX_GameObject*>(pyfrom); - fromPoint = other->NodeGetWorldPosition(); } if (dist != 0.0f) { @@ -1798,3 +1808,49 @@ void KX_GameObject::Relink(GEN_Map<GEN_HashedPtr, void*> *map_parameter) } } +bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py_none_ok) +{ + if (value==NULL) { + PyErr_SetString(PyExc_TypeError, "Error in ConvertPythonToGameObject, python pointer NULL, should never happen"); + *object = NULL; + return false; + } + + if (value==Py_None) { + *object = NULL; + + if (py_none_ok) { + return true; + } else { + PyErr_SetString(PyExc_TypeError, "Expected KX_GameObject or a string for a name of a KX_GameObject, None is invalid"); + return false; + } + return (py_none_ok ? true : false); + } + + if (PyString_Check(value)) { + *object = (KX_GameObject *)SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_String( PyString_AsString(value) )); + + if (*object) { + return true; + } else { + PyErr_SetString(PyExc_ValueError, "Requested name did not match any KX_GameObject"); + return false; + } + } + + if (PyObject_TypeCheck(value, &KX_GameObject::Type)) { + *object = static_cast<KX_GameObject*>(value); + return true; + } + + *object = NULL; + + if (py_none_ok) { + PyErr_SetString(PyExc_TypeError, "Expect a KX_GameObject, a string or None"); + } else { + PyErr_SetString(PyExc_TypeError, "Expect a KX_GameObject or a string"); + } + + return false; +} |