[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Help getting local variables from Lua
- From: Warren Merrifield <wmerrifield@...>
- Date: Mon, 20 Jun 2005 17:45:18 +0100
Do you mean object syntax? Where you might have
local foo = MyObject();
foo.var1 = "A";
foo.var2 = "B";
foo.var3 = "C";
foo:DoSomething();
where MyObject() returns an appropriately set up table and
DoSomething() is a C function call?
In that case, the 'foo' table will be passed as the 'hidden' first
parameter, which you can access in the usual way inside the C function
as the 1st element on the Lua stack. i.e.
int DoSomething(lua_State *L)
{
if(!lua_istable(L, 1))
error(L, "self is not a valid table!");
lua_pushstring(L, "var1");
lua_gettable(L, 1); // Get self["var1"]
value = lua_tostring(L, -1); // value points to "A" if you
assume the above script.
lua_pop(L, 1) // Remove number from the stack
// etc.
}
On 6/19/05, Wim Couwenberg <w.couwenberg@chello.nl> wrote:
> > -- in lua script i have --------
> > local SELF = {}
> >
> > function SELF.onDamage( )
> > -- do some stuff
> > end
>
> Locals are not stored in a table. The SELF in your example will simply
> get collected some time after the script has run since there are no
> references to SELF left.
>
> There are basically two approaches:
>
> 1) Drop the "local" to make SELF a global and you can access that
> easily. Globals are stored in the script's environment.
>
> 2) Explicitly "return SELF" at the end of the script to obtain a
> reference to it.
>
> --
> Wim
>