Page 1 of 1
mission_ and local_ variables
Posted: Wed Mar 14, 2007 6:52 am
by Commander McLane
A question for the code-people:
Is there any difference in how the game-engine handles mission_foo and local_foo variables? E.g. are the local_foo variables perhaps automaticly reset on leaving the current system? Or anything else?
Or can they be used pretty much interchangeably?
Posted: Wed Mar 14, 2007 11:08 am
by Arexack_Heretic
That is exactly how I imagined them to function...
how else would having two variable-types make sense?
(at first I thought I could just take any string as a variable, but I changed all my (erroneous?)code to mission_var_foo/local_var_foo for standardisation purposes)
Posted: Wed Mar 14, 2007 1:42 pm
by JensAyton
The local variables are tied to a specific script name/mission title.¹ That is, they are local to the script, not to the system. (This is a programmerese usage of “local”.) In contrast, mission variables are global, i.e. all scripts can access the same set of mission variables. As I understand it, locals were introduced because people’s copy & pasted scripts were reusing mission variables elsewhere, which caused problems; if you’re not trying to share data between different scripts, you should probably be using locals.
¹ In script.plist, these are the keys of the dictionary containing the scripts, such as “thargoid_plans” and “constrictor_hunt” in the built-in script. I don’t know about other scripting contexts. JavaScripts can set their name explicitly, but don’t need to use local_foo anyway.
Side note: I’m reserving the name “!metadata!” for future use. If you intended to write a script called “!metadata!”, try thinking of a name that makes sense.
Posted: Wed Mar 14, 2007 11:06 pm
by dajt
The other difference is that mission variables are written to the save game files, whereas local ones are not.
Local variables are tied to specific scripts, are all undefined when the game loads, and keep their values only as long as the game is running.
I cannot recall if they are all removed when a saved game is loaded or not.
Posted: Wed Mar 14, 2007 11:18 pm
by Arexack_Heretic
another good reason to switch to local_variables for many of mission_variables.
also mission_variable_proot are still checked at loading even after an oxp has been removed.
(<nil> equals [mission_variable] etc)
Posted: Wed Mar 14, 2007 11:21 pm
by JensAyton
dajt wrote:The other difference is that mission variables are written to the save game files, whereas local ones are not.
Ahh, yes. That one’s worth keeping in mind. :-)
Posted: Thu Mar 15, 2007 5:44 am
by Commander McLane
Thanks, all, for the answers!
I am completely satisfied.
I also find the distiction between writing or not-writing in the save-file very interesting. I think intuitively I have used my variables accordingly when scripting. I use local_variables in instances where there is no need (and I don't want) to write something in the savefile. But as I didn't know that they are not saved anyway I always reset them when I don't need them anymore. Perhaps in future I can spare me that line of code in the script.