From 1c663bbc7e53cda1fe35579302574b0d98aa8db3 Mon Sep 17 00:00:00 2001 From: Benoit Bolsee Date: Mon, 29 Dec 2008 16:36:58 +0000 Subject: First batch of GE API cleanup. The principle is to replace most get/set methods of logic bricks by direct property access. To make porting of game code easier, the properties have usually the same type and use than the return values/parameters of the get/set methods. More details on http://wiki.blender.org/index.php/GameEngineDev/Python_API_Clean_Up Old methods are still available but will produce deprecation warnings on the console: " is deprecated, use the property instead" You can avoid these messages by turning on the "Ignore deprecation warnings" option in Game menu. PyDoc is updated to include the new properties and display a deprecation warning for the get/set methods that are being deprecated. --- source/gameengine/GameLogic/SCA_PropertySensor.cpp | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'source/gameengine/GameLogic/SCA_PropertySensor.cpp') diff --git a/source/gameengine/GameLogic/SCA_PropertySensor.cpp b/source/gameengine/GameLogic/SCA_PropertySensor.cpp index 64e3d49c6cb..19ee85207bb 100644 --- a/source/gameengine/GameLogic/SCA_PropertySensor.cpp +++ b/source/gameengine/GameLogic/SCA_PropertySensor.cpp @@ -333,25 +333,72 @@ PyParentObject SCA_PropertySensor::Parents[] = { }; PyMethodDef SCA_PropertySensor::Methods[] = { + //Deprecated functions ------> {"getType", (PyCFunction) SCA_PropertySensor::sPyGetType, METH_VARARGS, (PY_METHODCHAR)GetType_doc}, {"setType", (PyCFunction) SCA_PropertySensor::sPySetType, METH_VARARGS, (PY_METHODCHAR)SetType_doc}, {"getProperty", (PyCFunction) SCA_PropertySensor::sPyGetProperty, METH_VARARGS, (PY_METHODCHAR)GetProperty_doc}, {"setProperty", (PyCFunction) SCA_PropertySensor::sPySetProperty, METH_VARARGS, (PY_METHODCHAR)SetProperty_doc}, {"getValue", (PyCFunction) SCA_PropertySensor::sPyGetValue, METH_VARARGS, (PY_METHODCHAR)GetValue_doc}, {"setValue", (PyCFunction) SCA_PropertySensor::sPySetValue, METH_VARARGS, (PY_METHODCHAR)SetValue_doc}, + //<----- Deprecated {NULL,NULL} //Sentinel }; PyObject* SCA_PropertySensor::_getattr(const STR_String& attr) { + if (attr == "type") { + return PyInt_FromLong(m_checktype); + } + if (attr == "property") { + return PyString_FromString(m_checkpropname); + } + if (attr == "value") { + return PyString_FromString(m_checkpropval); + } _getattr_up(SCA_ISensor); /* implicit return! */ } +int SCA_PropertySensor::_setattr(const STR_String& attr, PyObject *value) { + if (PyInt_Check(value)) { + int ival = PyInt_AsLong(value); + if (attr == "type") { + if ((ival <= KX_PROPSENSOR_NODEF) || (ival >= KX_PROPSENSOR_MAX)) { + PyErr_SetString(PyExc_ValueError, "type out of range"); + return 1; + } + m_checktype = ival; + } + return 0; + } + if (PyString_Check(value)) { + char* sval = PyString_AsString(value); + if (attr == "property") { + CValue *prop = FindIdentifier(STR_String(sval)); + bool error = prop->IsError(); + prop->Release(); + if (error) { + PyErr_SetString(PyExc_ValueError, "string does not correspond to a property"); + return 1; + } + m_checkpropname = sval; + } else if (attr == "value") { + if (!validValueForProperty(sval, m_checkpropname)) { + PyErr_SetString(PyExc_ValueError, "string does not represent a suitable value for the property"); + return 1; + } + m_checkpropval = sval; + } + return 0; + } + return SCA_ISensor::_setattr(attr, value); +} + /* 1. getType */ const char SCA_PropertySensor::GetType_doc[] = "getType()\n" "\tReturns the type of check this sensor performs.\n"; PyObject* SCA_PropertySensor::PyGetType(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getType()", "the type property"); return PyInt_FromLong(m_checktype); } @@ -364,6 +411,7 @@ const char SCA_PropertySensor::SetType_doc[] = "\tSet the type of check to perform.\n"; PyObject* SCA_PropertySensor::PySetType(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setType()", "the type property"); int typeArg; if (!PyArg_ParseTuple(args, "i", &typeArg)) { @@ -384,6 +432,7 @@ const char SCA_PropertySensor::GetProperty_doc[] = "\tReturn the property with which the sensor operates.\n"; PyObject* SCA_PropertySensor::PyGetProperty(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getProperty()", "the 'property' property"); return PyString_FromString(m_checkpropname); } @@ -395,6 +444,7 @@ const char SCA_PropertySensor::SetProperty_doc[] = "\tof this name, the call is ignored.\n"; PyObject* SCA_PropertySensor::PySetProperty(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setProperty()", "the 'property' property"); /* We should query whether the name exists. Or should we create a prop */ /* on the fly? */ char *propNameArg = NULL; @@ -419,6 +469,7 @@ const char SCA_PropertySensor::GetValue_doc[] = "\tReturns the value with which the sensor operates.\n"; PyObject* SCA_PropertySensor::PyGetValue(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getValue()", "the value property"); return PyString_FromString(m_checkpropval); } @@ -431,6 +482,7 @@ const char SCA_PropertySensor::SetValue_doc[] = "\t action is ignored.\n"; PyObject* SCA_PropertySensor::PySetValue(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setValue()", "the value property"); /* Here, we need to check whether the value is 'valid' for this property.*/ /* We know that the property exists, or is NULL. */ char *propValArg = NULL; -- cgit v1.2.3