[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.
Benoit Bolsee
benoit.bolsee at online.be
Fri Jan 2 11:57:54 CET 2009
Hi Campbell,
Thanks for these advices, you are the Python master.
I will change as you suggest.
Regarding value clamping versus error, it seems a better thing to do but
I will keep both options as in some cases clamping doesn't make sense.
Benoit
> 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;
>
More information about the Bf-committers
mailing list