Skip to main content
gamedev.net
🔒 Locked

Using derived Python classes in C++ (Boost.Python)

Started by disanti 0 replies 1.6k views
Original Post
disanti
disanti
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:
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;
};
My Python derived class:
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
I instanciate that class:
gamestate = StateTitleScreen();
Game.PushState(gamestate); #and then send it back to C++
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:
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);
}
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.
My game development blog: http://rykerlabs.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.