lua-users home
lua-l archive

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


Greetings everyone,

I've been racking my brain over how to recursively traverse the
globals table from the C API.

I can easily traverse one level of the globals table using lua_next like so:

int idx=LUA_GLOBALSINDEX;
lua_getfenv(l,idx);

while(lua_next(l,idx)!=0)
{
		const char* key=lua_tostring(l,-2);

		//get value at -1 index and check type of it

		//do somthing with key and value based on type of value
		//if value is a table call this function again to traverse it, then
continue here


		lua_pop(l,1);
}

The issue is when i come across a value that is a table i need to
traverse it too recursively.

it seems in theory it should be as simple as doing another lua_next,
however I've not been able to get it to work, and I end up crashing,
likely due to corupting the stack; either my logic or how I'm going
about it is wrong, an example of how to do this in the C API would be
really helpful.

Thanks in advance :)

-Raymond Jacobs
Ethereal Darkness Interactive
www.edigames.com