lua-users home
lua-l archive

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


> From my reading of what you wrote above, you basically want every
variable in every function to be, for want of a better word, a "public
variable."

Well, I have a lot of local vars.  The Lua fns have "local foo=..." all
over the place, so in that sense it's just like the out of the box Lua
behavior.  Local vars are local, and once you don't declare local
to go _G, or in my case, the particular environment I'm trying to use
at the time.  I'm not trying to change the basic way Lua works
in that sense, just trying to steer things over to a different table, but
being able to chose that table on a callstack basis.

CM



From: Steve Litt <slitt@troubleshooters.com>
To: lua-l@lists.lua.org
Sent: Saturday, March 24, 2012 7:11 PM
Subject: Re: callstack recursive environment

On Sat, 24 Mar 2012 17:38:11 -0700 (PDT)
codemonkey Monkey <thecodemonkey@rocketmail.com> wrote:

> > So if I understand you correctly, you want to be able to look at
> variables and have them be the value they were in the caller, but
> change them without affecting the caller. Do I have it right so far?
> Almost: changes should impact the caller too (hence, I set both
> __index and __newindex right now).
>
> Basically, my C++ code maintains a bunch of separate Lua tables,
> one per game object.  When it's about to call a Lua fn for one of
> these, it sets up the module's metatable.  Then, any variables either
> referenced or created by the Lua functions being called go into
> the right table.  This is what i want, except that I also want it to
> follow the entire Lua call stack.
>
> function a()
>   some_var = true
>   b()
>   print(some_var)
> end
>
> function b()
>   print(some_var)
>   some_var = 5
> end
>
> Should print true and 5, even if b is in another module somewhere.

CM, let me play devil's advocate here...

From my reading of what you wrote above, you basically want every
variable in every function to be, for want of a better word, a "public
variable." Is that REALLY what you want? That sounds a little dangerous to me.

SteveT