variable for all script in the editor

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
montagneyaya
Posts: 103
Joined: Tue May 01, 2012 11:08 pm

variable for all script in the editor

Post by montagneyaya »

Is there a way for create a variable usable in all script in the editor ?
User avatar
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: variable for all script in the editor

Post by Xanathar »

You can refer to a variable in another script entity without problems, prefixing it with the name of the entity followed by a dot ".".
If you can't write it (I haven't tested) you can put a couple of methods in that entity to wrap the variable (this is a good practice anyway and is the reason I didn't test the variable access really).
Waking Violet (Steam, PS4, PSVita, Switch) : http://www.wakingviolet.com

The Sunset Gate [MOD]: viewtopic.php?f=14&t=5563

My preciousss: http://www.moonsharp.org
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: variable for all script in the editor

Post by Komag »

I'm not positive but I seemed to not be able to affect the variable from another script entity, only read it. So if I had
badLunchScript which contained:

Code: Select all

hotDogs = 4
applePie = 8

function eatSomeLunch()
  hotDogs = hotDogs - 1
  applePie = applePie - 1
end
Then I can have another script entity in the dungeon called goodDinnerScript which contains:

Code: Select all

peptoBismolSpoons = 0

function setSpoons()
  peptoBismolSpoons = badLunchScript.hotDogs
end
but the only way to change the variable hotDogs is to do it inside badLunchScript. So if I'm still in goodDinnerScript, I can write this:

Code: Select all

badLunchScript.eatSomeLunch()
Finished Dungeons - complete mods to play
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: variable for all script in the editor

Post by Grimwold »

I'm pretty sure you're right Komag as I remember reading that you can only modify a variable from the script entity it is in.

Should be pretty easy to set up a few function to do it though.

Code: Select all

my_variable = 0

function incrementMyVariable()
  my_variable = my_variable + 1
end

function decrementMyVariable()
  my_variable = my_variable -1
end

function setMyVariable(x)
  my_variable = x
end

then call something like:

Code: Select all

my_script.setMyVariable(5)
User avatar
montagneyaya
Posts: 103
Joined: Tue May 01, 2012 11:08 pm

Re: variable for all script in the editor

Post by montagneyaya »

Ok thank you all for this info :)
It will be a great help
User avatar
antti
Posts: 688
Joined: Thu Feb 23, 2012 1:43 pm
Location: Espoo, Finland
Contact:

Re: variable for all script in the editor

Post by antti »

Yup! Coincidentally, using "getters" and "setters" like these is often regarded as a good programming practice anyway even if you would be working in an environment where you could directly modify pretty much any variable. This way you can, for example, expand and build additional logic into the getter and setter functions later on so that you could build a check that a variable stays within a certain range and so on. :geek:
Steven Seagal of gaming industry
Post Reply