Sign in to follow this  
Servant of the Lord

Trying to move to 5.2 - replacement for LUA_GLOBALSINDEX?

Recommended Posts

I haven't used Lua in about 8 months (or more), so I'm trying to refresh my mind, while at the same time migrating to 5.2 from 5.1.
In a function I wrote way back then, I iterate over a table recursively descending into sub-tables, starting at the global index.

I used the global 'LUA_GLOBALSINDEX' which is no longer available in 5.2. What can I use instead?

For example, if I want to call:
[code]lua_type(luaState, LUA_GLOBALSINDEX);[/code]

...to get the type of the global table itself ([b]LUA_TTABLE[/b], right?), how can I do that with Lua 5.2?

Would:
[code]lua_pushglobaltable(luaState);
int globalIndex = lua_tointeger(luaState, -1);
[/code]

...get me the index of the globals table?

Share this post


Link to post
Share on other sites
Doesn't that get a specific global, like:
lua_getglobal(lua, "MyGlobalVariable")

I want to get the table that all the global variables are stored in. That is to say, I want to get the table that is the parent of "MyGlobalVariable", not "MyGlobalVariable" itself.How can I do that?

Share this post


Link to post
Share on other sites
Matias Goldberg    9574
'_G' is the global table that holds everything. lua_getglobal(lua, "_G") should do the trick.
Apparently Lua 5.2 introduces "environments", so you should check the variable '_ENV' as well.

Beware _G is recursive, therefore _G contains _G as well.

[url="http://www.lua.org/manual/5.2/manual.html#2.2"]More info[/url]
[url="http://www.lua.org/manual/5.2/manual.html#4.5"]Some more info[/url]

Share this post


Link to post
Share on other sites
What confused me about _G, is this comment:
[quote][size=2][url="http://www.lua.org/manual/5.2/manual.html#pdf-_G"]http://www.lua.org/m...ual.html#pdf-_G[/url][/size]
[size=3][color=#000000][font=Helvetica, Arial, sans-serif]A global variable (not a function) that holds the global environment (see [/font][/color][url="http://www.lua.org/manual/5.2/manual.html#2.2"]§2.2[/url][color=#000000][font=Helvetica, Arial, sans-serif]). Lua itself does not use this variable; changing its value does not affect any environment, [u]nor vice-versa[/u].[/font][/color][/size]
[/quote]
Does this mean when I add values to the global environment, _G is not updated to reflect the changes?

Also, "[i]LUA_RIDX_GLOBALS: At this index the registry has the global environment.[/i]"

Would that mean LUA_RIDX_GLOBALS functions the same as LUA_GLOBALSINDEX used to?

I mean, does this hold true: ([size=2]I'm not capable of testing it at the moment[/size])
[CODE]
lua_pushglobaltable(luaState);
int globalIndex = lua_tointeger(luaState, -1);

(globalIndex == LUA_RIDX_GLOBALS) //<------ (?)
[/CODE]

Share this post


Link to post
Share on other sites
Matias Goldberg    9574
I think it means you have to treat it as read only (it's confusing for me as well).

To create new variables, I do (in 5.1):

[code]lua_pushnumber( m_luaVM, 1 );
lua_setglobal( m_luaVM, "myVar" );[/code]
That is the same as declaring 'myVar = 1'

To create a simple bidimensional table (i.e. like myTable[1][7] = 3):
[code]
std::vector<int> keyMessages[numElements];
lua_createtable( m_luaVM, numElements, 0 );
for( int i=0; i<numElements; i++ )
{
lua_createtable( m_luaVM, (int)keyMessages[i].size(), 0 );

for( size_t j=0; j<keyMessages[i].size(); j++ )
{
lua_pushinteger( m_luaVM, keyMessages[i][j] ); // value
lua_rawseti( m_luaVM, -2, (int)(j+1) ); //set table at key 'j+1'
}

lua_rawseti( m_luaVM, -2, (int)(i+1) ); //set table at key 'i+1'
}
lua_setglobal( m_luaVM, "myTable" );
[/code]
Note I do "i+1" & "j+1" because Lua uses the convention that indexes start at 1, not 0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this