local/global "persistant" variables, in scripts/functions

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Shroom
Posts: 98
Joined: Tue Mar 27, 2012 6:37 pm
Location: UK

local/global "persistant" variables, in scripts/functions

Post 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
User avatar
antti
Posts: 688
Joined: Thu Feb 23, 2012 1:43 pm
Location: Espoo, Finland
Contact:

Re: Beta-1.2.9 now available!

Post by antti »

You need to either define the local variable inside the function or use a global variable.
Steven Seagal of gaming industry
User avatar
Shroom
Posts: 98
Joined: Tue Mar 27, 2012 6:37 pm
Location: UK

Re: Beta-1.2.9 now available!

Post 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 :)
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: Beta-1.2.9 now available!

Post 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  
User avatar
Billick
Posts: 201
Joined: Thu Apr 19, 2012 9:28 pm

Re: Beta-1.2.9 now available!

Post 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.
Halls of Barrian - current version 1.0.3 - Steam Nexus
Me Grimrock no bozo!
User avatar
Shroom
Posts: 98
Joined: Tue Mar 27, 2012 6:37 pm
Location: UK

Re: Beta-1.2.9 now available!

Post 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?
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Beta-1.2.9 now available!

Post 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.
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Beta-1.2.9 now available!

Post 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 ^^
A trip of a thousand leagues starts with a step.
User avatar
antti
Posts: 688
Joined: Thu Feb 23, 2012 1:43 pm
Location: Espoo, Finland
Contact:

Re: Beta-1.2.9 now available!

Post 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).
Steven Seagal of gaming industry
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Beta-1.2.9 now available!

Post 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 :-)
A trip of a thousand leagues starts with a step.
Post Reply