Jump to content
  • Advertisement
Sign in to follow this  
disanti

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

This topic is 4255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 "<string>", line 36, in <module> 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.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!