You may want to try having Py++ do an example wrapping of a header file or two, thats always what I did while I was wrapping parts of my code that I was not sure about the right conventions in boost python.

That being said, I have also never tried passing a pointer to python for python to edit and give back. That kind of design kind of stinks to me, I think a better solution would be to write an interface for a python visitor that you could apply to your C++ game objects, but that is not important here.

There are a couple of suggestions I can make, if you switch back to passing by reference, you need to remember to use boost::ref() for the arguments you do not want copied.
Eg:
call_method<void>(self, "TickCharacter", boost::ref( character ), boost:;ref( field ), ticks)

I use a constant ref in some of my functions and I found I always need to tell boost about it other wise the object does not work right.

Also, why are you using call_method like this?
I thought the right way for calling python functions was

if( bp::override func = bp::get_override( "TickCharacter" ) )
func( boost::ref( character ), boost::ref( field ), ticks );

That is what I do anyway and it was in the docs at some point. Maybe the difference in calling convention is causing boost python to not accurately pass your pointers?

Charles

On , Jim Bosch <tallji...@gmail.com> wrote:
On 05/25/2011 02:26 PM, Jay Riley wrote:


I have some class functions that takes in parameters by pointer.

However, when I expose and override this class/these functions in

boost::python, any changes I make to the parameters are not reflected

back to the original, meaning it isn't being passed by pointer as I

expect. I previously had them as pass-by-reference, but the same problem

still occurred. For an example of what I mean, here's some of my code

with unimportant functions and details omitted.






I note two spots that bear further investigation.



The most likely source is here:




//In the actual scripting code

void StatusEffectWrapper::TickCharacter(Game::Character::BaseCharacter*

character, Game::Battles::BattleField *field, int ticks)

{

call_method(self, "TickCharacter", character, field, ticks);

}




I think there's a very good chance that call_method is copying your character argument when it converts it to Python. Boost.Python is extra-careful about making sure the C++ objects it puts inside Python objects don't turn into dangling references, and it has no way to control the lifetime of a raw pointer or reference. I think you could probably fix this by using shared_ptr here, because Boost.Python does know how to convert that safely to Python without copying the pointee.



If that fails, look here:




.def("TickCharacter", &StatusEffect::TickCharacter,

&StatusEffectWrapper::TickCharacterDefault)




I've personally never seen this invocation of .def, with two member function pointer arguments. That doesn't necessarily mean it's incorrect, because overloading virtual methods in Python is feature I almost never use, and it seems like there are a half-dozen ways to do it and I never know which one is considered best (perhaps someone else on this list does?). But you might want to make sure this does what you expect.



HTH



Jim Bosch

_______________________________________________

Cplusplus-sig mailing list

Cplusplus-sig@python.org

http://mail.python.org/mailman/listinfo/cplusplus-sig


_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to