LUA locals

BlitzMax Forums/BlitzMax Programming/LUA locals

Hezkore(Posted 2011) [#1]
I'm a complete LUA noob so you'll have to excuse me if this is a dumb question...
But I'm having some problems creating a "local" and setting its value.


This does _NOT_ work for me.
It just prints a blank line.
local a = 5
print(a)



But this _DOES_ work.
It does print a 5 this time.
local a
a = 5
print(a)



But this also works...
sdf = 5
print(sdf)



So really what confuses me is...
Why would I ever write "local a" when I can instantly just write "a = 5" and it'll be created by itself?
And why can't I set "a" to 5 with "local a = 5"?

Last edited 2011


Zeke(Posted 2011) [#2]
local a = 5
print(a)

works here.


Htbaa(Posted 2011) [#3]
It's Lua, not LUA.


kfprimm(Posted 2011) [#4]
Interesting.

I'm Lua 5.1.4 and when I run it from a file, I get 5. However, when I run it directly from the interpreter, I get nil.

If I enclose it in a function, and then run the function, I get 5.

It appears that you cannot declare a local in the 'global' scope.

This makes sense because when dofile/lua_dofile is called, as the interpreter does, it is enclosed in a function before being run.

Last edited 2011


Zeke(Posted 2011) [#5]
ah. i was using just lual_dostring/lual_dofile.

http://lua-users.org/wiki/ScopeTutorial