Page 1 of 3
local/global "persistant" variables, in scripts/functions
Posted: Wed Sep 19, 2012 3:43 pm
by Shroom
I just tried to add some variables into a script and then reference them in a function and I got an upvalue error - wtf is that :p
Code: Select all
local bool1 = false
function test()
if not bool1 then
end
end
Thats literally all i put and then on hitting preview I got the code error
Re: Beta-1.2.9 now available!
Posted: Wed Sep 19, 2012 3:46 pm
by antti
You need to either define the local variable inside the function or use a global variable.
Re: Beta-1.2.9 now available!
Posted: Wed Sep 19, 2012 3:58 pm
by Shroom
antti wrote:You need to either define the local variable inside the function or use a global variable.
oh so there is no such thing as local to that script entity - i see (I was thinking of each script as a class in c#)
Thanks for the heads up
Re: Beta-1.2.9 now available!
Posted: Wed Sep 19, 2012 4:35 pm
by petri
Well, to be precise, there are no "global" variables. E.g.
Script1:
Code: Select all
-- this variable is contained in the script entity, so it is "local" to the script
-- it will be saved/restored when loading a saved game
foo = 1
function doSomething()
-- local variable to function doSomething
-- since the variable will be out of scope when doSomething() returns, it's value won't be saved
local a = 0
end
Script2:
Code: Select all
-- this variable is separate from 'foo' in script1
-- modifying this variable does not have effect on script1's foo
foo = 2
function blah()
-- this will create a new variable in script2's scope
-- it is NOT local to function blah() so it will be saved/restored from save games
bar = "test"
end
Re: Beta-1.2.9 now available!
Posted: Wed Sep 19, 2012 4:51 pm
by Billick
petri wrote:Well, to be precise, there are no "global" variables. E.g.
Script1:
Code: Select all
-- this variable is contained in the script entity, so it is "local" to the script
-- it will be saved/restored when loading a saved game
foo = 1
function doSomething()
-- local variable to function doSomething
-- since the variable will be out of scope when doSomething() returns, it's value won't be saved
local a = 0
end
Script2:
Code: Select all
-- this variable is separate from 'foo' in script1
-- modifying this variable does not have effect on script1's foo
foo = 2
function blah()
-- this will create a new variable in script2's scope
-- it is NOT local to function blah() so it will be saved/restored from save games
bar = "test"
end
So... in other words variables are persistent, but not global.
Re: Beta-1.2.9 now available!
Posted: Wed Sep 19, 2012 5:04 pm
by Shroom
Thanks for clearing that up petri
To summarise
There are no global variables
To make a variable global to the script entity it is in (and persistant through saves) put it outside of the functions and simply declare it ie foo = 1
to make a variable local to a function (and therefore only exist as long as that function instance is being run), put it within the function declaration and use local in front of it
Can I access a variable from a different script ie script1.foo or is that limited to function calls?
Is there any diference then in declaring a variable inside or outside of a function if I do not use local?
Re: Beta-1.2.9 now available!
Posted: Fri Sep 21, 2012 7:33 am
by Lmaoboat
Shroom wrote:Thanks for clearing that up petri
To summarise
There are no global variables
To make a variable global to the script entity it is in (and persistant through saves) put it outside of the functions and simply declare it ie foo = 1
to make a variable local to a function (and therefore only exist as long as that function instance is being run), put it within the function declaration and use local in front of it
Can I access a variable from a different script ie script1.foo or is that limited to function calls?
According to the patch notes, script1.foo should work.
Re: Beta-1.2.9 now available!
Posted: Fri Sep 21, 2012 7:45 am
by cromcrom
So, does it mean I can create a "variable_storage" script entity (SE), define variable YYY in it (not in a function, just in the SE), and access/change it using variable_storage.YYY , and this new YYY variable will be saved through saved games ?
if yes, it's huge, and it's gonna make my numerous skills easier to use and create ^^
Re: Beta-1.2.9 now available!
Posted: Fri Sep 21, 2012 8:24 am
by antti
cromcrom wrote:So, does it mean I can create a "variable_storage" script entity (SE), define variable YYY in it (not in a function, just in the SE), and access/change it using variable_storage.YYY , and this new YYY variable will be saved through saved games ?
if yes, it's huge, and it's gonna make my numerous skills easier to use and create ^^
For the most part, yes. The only thing is that you can't change the variables like this from some other script entity though:
Code: Select all
variable_storage.exampleVariable = 1 -- this doesn't work from outside the variable_storage entity
But instead you can do a setter function for it in the variable_storage like this for instance:
Code: Select all
function setVariable(variable, value)
variable = value
end
And then refer to that function like this:
Code: Select all
variable_storage.setVariable(exampleVariable, 2)
Or you can alternatively build one setter function for each variable instead of a generic one like this if you want to introduce some variable specific functionality right in the setter (like checking if the value is within some specific bounds or error checking and so on).
Re: Beta-1.2.9 now available!
Posted: Fri Sep 21, 2012 9:34 am
by cromcrom
Lol, that's great, it makes a neat trick. The bad side is that I am probably gonna have to redo many functions/items/SE. But it's really some good great news anyways ^^ Thanks a lot