I have a problem with locals, so currently I'm using globals instead, but of course that isn't really the solution.
[lua]
// code which makes SOMETHING 0 or 1
if SOMETHING == 1 then
local getal = 10
else
local getal = math.random( 1, 9 )
end
// code which uses 'getal'
[/lua]
Why doesn't the local work outside the if function? Does it need to be a global? And is it possible to change a local variable -which has been declared before- to another variable inside an if-function and then use it afterwards?
This didn't really help:
[url]http://wiki.garrysmod.com/?title=LUA:Global_and_Local[/url]
a local is removed after the scope its defined in is exited, do it like this:
[lua]// code which makes SOMETHING 0 or 1
local getal = 10
if SOMETHING != 1 then
getal = math.random( 1, 9 )
end
// code which uses 'getal'[/lua]
[editline]21st October 2010[/editline]
Example that should explain it:
[lua]if true then -- create new scope simply
local var = "stuff"
print(var)
end
print(var)[/lua]
First print it will print stuff as the variable is defined in the current scope (or a parent scope)
Second print will print nil as its out of the scope
Ok, thank you, I think I understand this now. The wiki said however, and I quote:
"...It's the same for an if statement, if you create a variable inside an if statement. It will still be available to the next scope, such as a function."
If you mean this:
[lua]if ( true ) then
local x = 3
end
print( x ) -- Prints 3[/lua]
Then that is false. Where was that specified?
On the wiki page that didn't really help:
[url]http://wiki.garrysmod.com/?title=LUA:Global_and_Local[/url]
I think it meant this:
[lua]if true then
local newscope = true
function stuff()
print(newscope)
end
stuff()
end[/lua]
It is a bit of a cryptic description though.
Edit: Just found the example. It says this:
[lua]if something == something then
var = true
end
function whatisvar()
if var == true then
print("Var is true!")
end
end[/lua]
Which is completely incorrect obviously - that's a global.
To create a new scope you can use 'do':
[lua]
local x = 10
local z = 123
do
local y = 20
print(x) --10
print(y) --20
print(z) --123
z = 456
end
print(x) --10
print(y) --nil
print(z) -- 456
[/lua]
I know, i just didnt want to confuse him, if true then is more straight-forward than do, which he probably hasent seen used in anything else than loops
[QUOTE=Tobba;25553348]I know, i just didnt want to confuse him, if true then is more straight-forward than do, which he probably hasent seen used in anything else than loops[/QUOTE]
Then explain what do is and how and when it should be used instead of teaching bad habits.
[QUOTE=MakeR;25553956]Then explain what do is and how and when it should be used instead of teaching bad habits.[/QUOTE]
Or explain it yourself instead of teaching him a lesson.
/offtopic
Ok, it's working now, thanks all. I used self.variable by declaring them first (as locals?) and changing their value inside the if-statement. Using self.var was necessary because of the fact that there could be more than one versions of the object.
Is self.var a local?
[QUOTE=mysticyx;25555236]Or explain it yourself instead of teaching him a lesson.
/offtopic
Ok, it's working now, thanks all. I used self.variable by declaring them first (as locals?) and changing their value inside the if-statement. Using self.var was necessary because of the fact that there could be more than one versions of the object.
Is self.var a local?[/QUOTE]
self.var is not a local, it is table index. It is syntactic sugar (the same thing as) self["var"].
Sorry, you need to Log In to post a reply to this thread.