lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Dear experts,

Recently I started investigating LUA starting with version 4.01. I exposed C++ objects to LUA using:

int RegisterObject(lua_State *pState, const char *classname)
{
	lua_pushcfunction(pState, &InstantiateObject);
	lua_setglobal(pState, classname);
	int newtag = lua_newtag (pState);
	lua_pushcfunction(pState, &DisposeObject);
	lua_settagmethod(pState, newtag, "gc");

	m_tag2class.insert(make_pair(newtag, classname));
	m_class2tag.insert(make_pair(classname, newtag));
	...
}		

The problem that I face is that I want LUA to be an auxillary scripting engine outside of my game engine. So I defined global 'InstantiateObject' and 'DisposeObject' functions that try to lookup the class for me. One problem arises here. When the InstantiateObject function is called (corresponding potential 'a=Object{}' LUA code) I sometimes can see that the name of the object I wish to create is within the lua_State->Mbuffer. But off course this is the all-wrong method to do it. I haven't found an other, for I don't comletely understand the stack at that point.

int InstantiateObject(lua_State *pState)
{
	const char * classToCreate = pState->MBuffer;
	...
}

So, I wish to multiplex my class object creation. So I thought I'd have a peek at LUA 5.0 and it's metatables. But I must say it still puzzles me.

Is there a simple example (or explaination of how the stack works for c++ object registration) on how to use these metatables to implement a object and register an Instantiation-callback? Furthermore, on object instantiation, is there a way to retrieve the metatable responsable for instantiation and derive the 'wanted' object name?

Please, your expert advice.
Regards,
Ron