Page 1 of 1
variable for all script in the editor
Posted: Wed Nov 28, 2012 11:09 am
by montagneyaya
Is there a way for create a variable usable in all script in the editor ?
Re: variable for all script in the editor
Posted: Wed Nov 28, 2012 11:33 am
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).
Re: variable for all script in the editor
Posted: Thu Nov 29, 2012 11:53 am
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:
Re: variable for all script in the editor
Posted: Thu Nov 29, 2012 1:05 pm
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:
Re: variable for all script in the editor
Posted: Thu Nov 29, 2012 1:13 pm
by montagneyaya
Ok thank you all for this info
It will be a great help
Re: variable for all script in the editor
Posted: Thu Nov 29, 2012 2:49 pm
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.
