lua-users home
lua-l archive

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


To get a named global use the macro:

lua_getglobal( L, var_name );		//declared in lua.h

As for accessing data as a hierarchy:

getValue( "a/b/c/d" )

I do something very similar (except mine looks like getRef( "a.b.c.d" ))

I use lua_ref, lua_getref, lua_unref (formerly functions, but now macros
in lua5)

lua_ref returns an integer identifier that you can later use to retrieve
a reference to the value that's currently on the top of the stack.

Call lua_getref( L, id ) to restore the value to the top of the stack.

To prevent lua from gc'ing the data, pass a non-zero as the 2nd arg to
lua_ref.  Then, when you're finished with the ref, allow it to be gc'd
by calling lua_unref.

(Regarding the lock value - it looks like it's been deprecated.  Anyone
know if this is this the case?)

Here's an example:

getRef( "a.b.c" ); is equivalent to:

Ref ref = getGlobalRef( "a" );
ref = ref.getRef( "b" );
ref = ref.getRef( "c" );

Using lua functions/macros:

lua_getglobal( L, "a" );

int i_ref_a = lua_ref( L, 1 );	//lua_ref pops top of stack

lua_getref( L, i_ref_a );		//restore value at "a" to top of
stack
lua_pushstring( L, "b" );
lua_gettable( L, -1 );
int i_ref_b = lua_ref( L, 1 );

lua_getref( L, i_ref_b );		//restore value at "b" to top of
stack
lua_pushstring( L, "c" );
lua_gettable( L, -1 );			//value stored at "a.b.c" is now
on top

I hope I got that right.

-Kevin

> -----Original Message-----
> From: owner-lua-l@tecgraf.puc-rio.br 
> [mailto:owner-lua-l@tecgraf.puc-rio.br] On Behalf Of Brian Hook
> Sent: Thursday, March 06, 2003 12:21 PM
> To: Multiple recipients of list
> Subject: Re: 5.0 changes question
> 
> 
> Actually what I'm doing is (I think) really simple, but since I have 
> yet to seriously use Lua, I'm probably doing this all wrong, like 
> bitflags =).  
> 
> I have an API call in my framework to "set the current table" so that 
> I can then iterate over its members or find a member.  Since I wasn't 
> aware of a special table name for the globals, I would simply call 
> lua_getglobals() if I wanted to select the global table.
> 
> I had a fairly simple scanner that would then allow me to reference 
> items in C code as if it were a directory structure, e.g. if I wanted 
> to access foo.x.y I could specify it as:
> 
> getValue( "/foo/x/y" );
> 
> but if "foo" was already selected, I could also do:
> 
> selectTable( "x" );
> getValue( "y" );
> 
> etc.
> 
> So the important bit is that sometimes I wanted to set the global 
> table as current, and other times I wanted to select a named table, 
> depending on where I was trying to search the name space.
> 
> Does this make sense?
> 
> Brian
> 
>