Lua : Controlling scope

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua : Controlling scope

Luc Van den Borre
Hi all,

I suppose there's no way of having some static variables local to one file ?
I.e. I would have two files that both have a variable called 'name' at the
top, which can be used by all the functions in that file,
but the variable has different values in different files...
Just like in C...

Thanks,

--Luc

Reply | Threaded
Open this post in threaded view
|

Re: Lua : Controlling scope

Luiz Henrique de Figueiredo
>From: Luc Van Den Borre <[hidden email]>
>
>I suppose there's no way of having some static variables local to one file ?

Not exactly.

>I.e. I would have two files that both have a variable called 'name' at the
>top, which can be used by all the functions in that file,
>but the variable has different values in different files...

You can declare local variables in to "top level":

	local a=102.3
	function f() ... end

but you cannot use 'a' inside function bodies.
What you can do is to to use the *value* that 'a' has when 'f' is defined:

	local a=102.3
	function f(x) return %a+x end

'%a' is called an upvalue.

So the value of 'a' is frozen to 102.3.
It's equivalent to having
	function f(x) return 102.3+x end
except that it might be more readble.

Changes to 'a' after 'f' is defined do not affect 'f'.

>Just like in C...

Lua is not C :-)
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Lua : Controlling scope

Roberto Ierusalimschy
In reply to this post by Luc Van den Borre
> I suppose there's no way of having some static variables local to one file ?
> I.e. I would have two files that both have a variable called 'name' at the
> top, which can be used by all the functions in that file,
> but the variable has different values in different files...
> Just like in C...

I think the nearest you can get of this is with *upvalues* (version 3.1).
You can declare a local variable in a file, and functions in these file
can use the value of this local variable:

      local temp = ...
      
      function foo ()
         ... %temp ...              -- use upvalue temp
      end

If another file uses "temp", it is completely independent of this one.

One drawback of this solution is that upvalues are *values*, not *variables*,
so you cannot assign to them. You can go around that by declaring your value
to be a table, and storing the variable inside the table. For instance:

      local temp = {var = ...}

      function foo ()
        %temp.var = ...
        ...  %temp.var ...
      end

In this way, you never have to change the value of "temp" (always the same
table).

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Lua 3.1 in multithreaded win32 enviroment

Erik Hougaard-2
In reply to this post by Luiz Henrique de Figueiredo
Have anyone worked with Lua 3.1 in a win32 multithreaded enviroment. I was trying
to have a lua_state for each thread (By using the __declspec(thread) on
lua_state) but it seems to crash on me. Without anything I can get Lua to run
great in two thread but with a common Global scope - I want the global variables
to be thread specific (Actually trigger-code section specific). I'm compiling
with Watcom 11 - have not have time to try with VC++ 5.0 yet.

Thanks,

Erik Hougaard
[hidden email]