Code: Select all
local bool1 = false
function test()
if not bool1 then
end
end
Code: Select all
local bool1 = false
function test()
if not bool1 then
end
end
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#)antti wrote:You need to either define the local variable inside the function or use a global variable.
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
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.petri wrote:Well, to be precise, there are no "global" variables. E.g.
Script1:Script2: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
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
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?
For the most part, yes. The only thing is that you can't change the variables like this from some other script entity though: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 ^^
Code: Select all
variable_storage.exampleVariable = 1 -- this doesn't work from outside the variable_storage entity
Code: Select all
function setVariable(variable, value)
variable = value
end
Code: Select all
variable_storage.setVariable(exampleVariable, 2)