Jump to content
  • Advertisement
Sign in to follow this  
Svenjamin

Help with luabind::object

This topic is 3720 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

Howdy, So I've recently started incorporating lua in my game via luabind, and I'm having some issues. I'm trying to use a scripted state machine like the one in Mat Buckland's book: Programming Game AI by Example. Here is my listing:
template <class T>
class ScriptedStateMachine
{
private:
	T *m_owner;
	
	State<T> *m_globalState;
	
	luabind::object m_currentState;
	
public:
	ScriptedStateMachine(T *owner):m_owner(owner), m_globalState(NULL){}
	~ScriptedStateMachine();
	
	void SetGlobalState(State<T> *state);
	
	void SetCurrentState(const luabind::object &state);
	void Update();
	void ChangeState(const luabind::object &newState);
	
	const luabind::object &CurrentState()const{return m_currentState;}
};


template <class T>
ScriptedStateMachine<T>::~ScriptedStateMachine()
{
	if (m_globalState != NULL)
	{
		delete m_globalState;
		m_globalState = NULL;
	}
}


template <class T>
void ScriptedStateMachine<T>::SetGlobalState(State<T> *state)
{
	m_globalState = state;
	
	if (m_globalState != NULL)
		m_globalState->Enter(m_owner);
}


template <class T>
void ScriptedStateMachine<T>::SetCurrentState(const luabind::object &state)
{
	m_currentState = state;
	
	if (m_currentState.is_valid())
		m_currentState["Enter"](m_owner);
}


template <class T>
void ScriptedStateMachine<T>::Update()
{
	if (m_globalState != NULL)
		m_globalState->Execute(m_owner);

	if (m_currentState.is_valid())
		m_currentState["Execute"](m_owner);
}


template <class T>
void ScriptedStateMachine<T>::ChangeState(const luabind::object &newState)
{
	if (m_currentState.is_valid())
		m_currentState["Exit"](m_owner);
	
	m_currentState = newState;
	
	if (m_currentState.is_valid())
		m_currentState["Enter"](m_owner);
}

So then to initialize the current state I do something like this:
SetCurrentState(luabind::globals(Lua::State())["PlayerState_Think"]);

This appears to work fine if I assign the PlayerState_Think state to only one player's ScriptedStateMachine, but as soon as I try to assign it a to a second or third, it segfaults. GDB says it happens in "getcurrenv" in the lua source somewhere. Anyway, I've been wading through the documentation and mailing lists trying to solve this, but I haven't had any luck so far. Any ideas? Cheers, Svenjamin

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!