Original Post
Hello, I'm learning how to use Boost.Python, and was recently applying it to game states to see what I could do. So I have a class in C++ called IState (it is an interface) that I have successfully wrapped with Boost.Python. I have derived this class in my Python script, and then I want to send this derived class back to C++ to use. IState: My Python derived class: I instanciate that class: Now my error is as follows: Traceback (most recent call last): File "", line 36, in Boost.Python.ArgumentError: Python argument types in Game.PushState(StateTitleScreen) did not match C++ signature: PushState(class ToH::IState *) Here are the functions and bindings: This is pretty frustrating as I've been googling for solutions for the last three hours... The documentation doesn't have any examples of this that I could find either. The error makes sense, but I don't think that typecasting is possible in Python (am I right?) If you have any suggestions about how I could make game states inside of Python then please let me know. Thanks.
class IState
{
public:
virtual void Enter() = 0;
virtual void Exit() = 0;
virtual bool BeginFrame(const Ogre::FrameEvent &evt) = 0;
virtual bool EndFrame(const Ogre::FrameEvent &evt) = 0;
};
class StateTitleScreen(Game.State):
def Enter(self):
print "Python Entered!";
def Exit(self):
print "Python Exited!";
def BeginFrame(self, event):
pass
def EndFrame(self, event):
pass
gamestate = StateTitleScreen();
Game.PushState(gamestate); #and then send it back to C++
void GamePushState(IState* state) {
Engine::getSingletonPtr()->pushState(state);
}
class StateWrapper : public IState, public wrapper<IState> {
void Enter() {
this->get_override("Enter")();
}
void Exit() {
this->get_override("Exit")();
}
bool BeginFrame(const Ogre::FrameEvent &evt) {
return call<bool>(this->get_override("BeginFrame").ptr());
}
bool EndFrame(const Ogre::FrameEvent &evt) {
return call<bool>(this->get_override("EndFrame").ptr());
}
};
BOOST_PYTHON_MODULE(Game) {
//class_<IState, boost::noncopyable>("State", no_init);
class_<StateWrapper, boost::noncopyable>("State", no_init)
.def("Enter", pure_virtual(&IState::Enter))
.def("Exit", pure_virtual(&IState::Exit))
.def("BeginFrame", pure_virtual(&IState::BeginFrame))
.def("EndFrame", pure_virtual(&IState::EndFrame))
;
class_<Ogre::FrameEvent>("OgreFrameEvent")
.def_readonly("timeSinceLastEvent", &Ogre::FrameEvent::timeSinceLastEvent)
.def_readonly("timeSinceLastFrame", &Ogre::FrameEvent::timeSinceLastFrame)
;
def("PushState", GamePushState);
}