lua-users home
lua-l archive

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



I generally prefer to stay away from them too, but I'm using them (well, not global variables, but in a special environment table) as persistant storage between invocations of a script.  So everything that can be local, is local, but if you need a piece of state that should be around next time your script runs, you stash it in this table, and it'll be there next time you run - even serialized to disk and reloaded with game saves.

CM


From: Steve Litt <slitt@troubleshooters.com>
To: lua-l@lists.lua.org
Sent: Wednesday, March 28, 2012 5:59 PM
Subject: Re: callstack recursive environment

On Wed, 28 Mar 2012 18:00:50 -0400
David Favro <lua@meta-dynamic.com> wrote:

> On 03/28/2012 02:46 PM, Steve Litt wrote:
>
> > First, a disclaimer. I don't know how global variables work in any
> > language because I never use them,
>
> Really?  By "use" do you mean "set"?  Because Lua is pretty hard to
> use if you never access type, require, pairs, table, string, io,
> etc.  Or do you not consider them "global" because they are in the
> "environment"?
>

The latter. I always considered type, require, pairs, table, string, io
part of the language or Lua libraries.

What I mean is I don't do this:

mycount = 3

function inccount()
    mycount = mycount + 1
end

inccount()

Instead I typically do something like this:

local mycount = 3

function incc(number)
    return number + 1
end

mycount = incc(mycount)

Also, I create and call functions globally. In most languages they're
not data, and I've had few if any problems from using functions
globally instead of passing them as arguments.

SteveT