Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 4338 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
	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):
	def EndFrame(self, event):
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) {

class StateWrapper : public IState, public wrapper<IState> {
	void Enter() {
	void 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());

	//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))

		.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
Sign in to follow this  

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!