Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Help with luabind::object

Started by Svenjamin Jul 16, 2008 at 6:14 PM 0 replies 1.7k views
Original Post
Svenjamin
Svenjamin
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

Topic Locked

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

Sign in to reply to this topic.