[Bf-committers] [Bf-blender-cvs] SVN commit: /data/svn/bf-blender [18203] trunk/blender/source/gameengine: BGE API cleanup: introduction of a generic framework to link Python attributes to logic brick class member .

Campbell Barton ideasman42 at gmail.com
Thu Jan 1 07:29:35 CET 2009


Hey Benoit,
best not use PyFloat_Check() - it almost always isnt what you want.
Since that means giving func(1) to a function will raise an error
where func(1.0) wont.

Running PyFloat_AsDouble and then checking for errors is better, since
it will run the PyTypes float conversion function if it exists.

Another thing, Id prefer the API clamp the input rather then raising
an error (especially for floats),
This forces the python scripter to work out range for some attribute
and add the clamping to the python script, worse, the errors could
only happen in rare cases (possibly float precision), it would be
possible to overlook before releasing a game.

example of float conversion...
+                                                       float *var =
reinterpret_cast<float*>(ptr);
+                                                       double val =
PyFloat_AsDouble(item);
+                                                       ptr += sizeof(float);
+                                                       if ((val==-1.0
&& PyErr_Occurred()) == 0)
+                                                       {
+                                                               if
(val < attrdef->m_fmin || val > attrdef->m_fmax)
+                                                               {
+
 PyErr_SetString(PyExc_ValueError, "item value out of range");
+
 goto UNDO_AND_ERROR;
+                                                               }
+                                                               *var =
(float)val;
+                                                       }
+                                                       else
+                                                       {
+
PyErr_SetString(PyExc_TypeError, "expected a float");
+                                                               goto
UNDO_AND_ERROR;
+                                                       }
+                                                       break;

On Wed, Dec 31, 2008 at 12:35 PM, Benoit Bolsee <benoit.bolsee at online.be> wrote:
> Revision: 18203
>          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=18203
> Author:   ben2610
> Date:     2008-12-31 21:35:20 +0100 (Wed, 31 Dec 2008)
>
> Log Message:
> -----------
> BGE API cleanup: introduction of a generic framework to link Python attributes to logic brick class member. See KX_PYATTRIBUTE macros in PyObjectPlus.h.
>
> Modified Paths:
> --------------
>    trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp
>    trunk/blender/source/gameengine/Expressions/PyObjectPlus.h
>    trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.cpp
>    trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.h
>    trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
>    trunk/blender/source/gameengine/GameLogic/SCA_MouseSensor.cpp
>    trunk/blender/source/gameengine/GameLogic/SCA_MouseSensor.h


More information about the Bf-committers mailing list