Python TipsAndTricks: Difference between revisions
(Created page with 'Some tips to help you out == Class Defines == If you add no_init to a class define then that class cant be constructed via python. This is good for classes like players, weapon…') |
No edit summary |
||
Line 6: | Line 6: | ||
Example: | Example: | ||
<source=cpp> | <source lang=cpp> | ||
bp::class_< NoNew >("CantMakeMe", bp::no_init); | bp::class_< NoNew >("CantMakeMe", bp::no_init); | ||
</source> | </source> | ||
Line 14: | Line 14: | ||
Alot of classes in Source are non copyable (i.e. you use pointers to reference them not stack objects) and thus you will need to state that they are non copyable otherwise strange compile errors or run time errors occur. This doesnt mean you cant make multiplie objects in python that point to this, just that boost wont try and copy it in the python bindings. | Alot of classes in Source are non copyable (i.e. you use pointers to reference them not stack objects) and thus you will need to state that they are non copyable otherwise strange compile errors or run time errors occur. This doesnt mean you cant make multiplie objects in python that point to this, just that boost wont try and copy it in the python bindings. | ||
<source=cpp> | <source lang=cpp> | ||
bp::class_< NoCopy, boost::noncopyable >("BoostCantCopyMe", bp::no_init); | bp::class_< NoCopy, boost::noncopyable >("BoostCantCopyMe", bp::no_init); | ||
</source> | </source> | ||
Line 25: | Line 25: | ||
Example New Object: | Example New Object: | ||
<source=cpp> | <source lang=cpp> | ||
MyClass* pyGetNewClass() | MyClass* pyGetNewClass() | ||
{ | { | ||
Line 36: | Line 36: | ||
Example Existing Object: | Example Existing Object: | ||
<source=cpp> | <source lang=cpp> | ||
CBasePlayer* pyGetPlayerByIndex(int index) | CBasePlayer* pyGetPlayerByIndex(int index) | ||
{ | { |
Revision as of 02:34, 6 September 2009
Some tips to help you out
Class Defines
If you add no_init to a class define then that class cant be constructed via python. This is good for classes like players, weapons and entitys that have a special method of constuction.
Example:
bp::class_< NoNew >("CantMakeMe", bp::no_init);
Alot of classes in Source are non copyable (i.e. you use pointers to reference them not stack objects) and thus you will need to state that they are non copyable otherwise strange compile errors or run time errors occur. This doesnt mean you cant make multiplie objects in python that point to this, just that boost wont try and copy it in the python bindings.
bp::class_< NoCopy, boost::noncopyable >("BoostCantCopyMe", bp::no_init);
Function Defines
When a function (or member function) your rapping returns a pointer you will have to tell boost how to handle it. If your return a new object, boost needs to know to hang onto and if you return a pointer to an existing object you have to boost to just use it. If you do the latter your python classes shouldnt hold on to the object as boost only ensures its valid for the lifetime of the function call.
Example New Object:
MyClass* pyGetNewClass()
{
return new MyClass();
}
def("NewClass", pyGetNewClass, return_value_policy<manage_new_object>()); //callee needs to delete it
Example Existing Object:
CBasePlayer* pyGetPlayerByIndex(int index)
{
return UTIL_PlayerByIndex(index);
}
def("GetPlayerByIndex", pyGetPlayerByIndex, return_value_policy<reference_existing_object>()); //callee doesnt need to delete it