Jump to content

  • Log In with Google      Sign In   
  • Create Account


Trying to move to 5.2 - replacement for LUA_GLOBALSINDEX?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Servant of the Lord   Crossbones+   -  Reputation: 17972

Like
0Likes
Like

Posted 29 January 2012 - 09:12 PM

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:
lua_type(luaState, LUA_GLOBALSINDEX);

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

Would:
lua_pushglobaltable(luaState);
int globalIndex = lua_tointeger(luaState, -1);

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

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


Sponsor:

#2 Matias Goldberg   Crossbones+   -  Reputation: 3048

Like
0Likes
Like

Posted 29 January 2012 - 11:12 PM

I don't use Lua 5.2, but try lua_getglobal
It is my understanding that macro is still available and has been redefined to behave the same way as it did in 5.1

#3 Servant of the Lord   Crossbones+   -  Reputation: 17972

Like
0Likes
Like

Posted 30 January 2012 - 10:21 AM

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?

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#4 Matias Goldberg   Crossbones+   -  Reputation: 3048

Like
2Likes
Like

Posted 30 January 2012 - 11:50 AM

'_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.

More info
Some more info

#5 Servant of the Lord   Crossbones+   -  Reputation: 17972

Like
0Likes
Like

Posted 30 January 2012 - 12:15 PM

What confused me about _G, is this comment:

http://www.lua.org/m...ual.html#pdf-_G
A global variable (not a function) that holds the global environment (see §2.2). Lua itself does not use this variable; changing its value does not affect any environment, nor vice-versa.

Does this mean when I add values to the global environment, _G is not updated to reflect the changes?

Also, "LUA_RIDX_GLOBALS: At this index the registry has the global environment."

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

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

(globalIndex == LUA_RIDX_GLOBALS) //<------  (?)

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#6 Matias Goldberg   Crossbones+   -  Reputation: 3048

Like
1Likes
Like

Posted 30 January 2012 - 12:47 PM

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):

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

To create a simple bidimensional table (i.e. like myTable[1][7] = 3):
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" );
Note I do "i+1" & "j+1" because Lua uses the convention that indexes start at 1, not 0




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS