🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

LUA: getting global functions + variables

Started by
3 comments, last by MofuX 20 years, 10 months ago
hi, i''m kinda new to lua and writing a console in use for a game. for this console i''d like to have an overview what lua-variables are created and what user defined functions there are. Console: a=100 Console: b=200 Console: c={test="sometest",number=0} Console: function inc(var) var=var+1 return var end ok, for example i type this into my console, lua does all this stuff and now i wan''t to get this: global Lua Variables: a,b,c global Lua Functions: inc does anybody know how to do that? I read something about an variable listing by using lua_gettop but it diddn''t work regards MofuX
Advertisement
All Lua globals are stored in a table . You can easily step through a table either using the table library (from actual Lua code), or the lua_next function from C code. The manual is very lucid on this subject.

From C(well, C++ to use STL), something like this should work:

std::list< std::string > vars;
std::list< std::string > funcs;

/* (copied from manual */
lua_pushnil(L); /* first key */

while (lua_next(L,LUA_GLOBALSINDEX) != 0)
{
/* ‘key’ is at index -2 and ‘value’ at index -1 */

if(lua_isfunction(L,-1) || lua_iscfunction(L,-1))
funcs.push_back( lua_tostring(L,-1) );
else
vars.push_back( lua_tostring(L,-1) );

lua_pop(L, 1); /* removes ‘value’; keeps ‘key’ for next iteration */
}

It''s not perfect and possibly somewhat redundant but it should work.

I know you can step through a table in Lua code, but for the minute I can''t work out how to extract the global table (or even if it''s possible) so you''ll need to do some more research if that''s what you want.
thanks, that helped a bit but i''m using LUA 4.0 which doesn''t seem to know the LUA_GLOBALSINDEX constant. I also tried the
constant from 5.0 which is -10001 but the app crashed.
Is there any way to get this constant in LUA 4.0?

I also found another way which might work...
lua_getglobals pushes a table of globals on the stack.
now there must be a way to read this table somehow with lua_gettable or lua_next. If anybody knows how... please help
Oh right, yeah you should be able to use getglobals in much the same way:

std::list< std::string > vars;std::list< std::string > funcs;lua_getglobals(L);lua_pushnil(L); /* first key */while (lua_next(L,-2) != 0) {/* ‘key’ is at index -2 and ‘value’ at index -1 */if(lua_isfunction(L,-1) || lua_iscfunction(L,-1))funcs.push_back( lua_tostring(L,-1) );elsevars.push_back( lua_tostring(L,-1) );lua_pop(L, 1); /* removes ‘value’; keeps ‘key’ for next iteration */}
a thousand thanks to you, i got this working now!
I also tried the way you posted before, but it didn't worked (i think i used the wrong indicies)

http://www.itstudents.ch/users/dave/files/Console.jpg

hmm... won't show the image... just open it with your browser :D
this shows my C++ OpenGL Engine with GUI and new Console

[edited by - MofuX on August 25, 2003 4:53:56 AM]

This topic is closed to new replies.

Advertisement