mission_ and local_ variables

General discussion for players of Oolite.

Moderators: winston, another_commander

Post Reply
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

mission_ and local_ variables

Post 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?
User avatar
Arexack_Heretic
Dangerous Subversive Element
Dangerous Subversive Element
Posts: 1876
Joined: Tue Jun 07, 2005 7:32 pm
Location: [%H] = Earth surface, Lattitude 52°10'58.19"N, longtitude 4°30'0.25"E.
Contact:

Post 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)
Riding the Rocket!
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Post 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.
dajt
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 364
Joined: Tue Aug 17, 2004 7:05 am
Location: Orange, NSW, Australia

Post 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.
Regards,
David Taylor.
User avatar
Arexack_Heretic
Dangerous Subversive Element
Dangerous Subversive Element
Posts: 1876
Joined: Tue Jun 07, 2005 7:32 pm
Location: [%H] = Earth surface, Lattitude 52°10'58.19"N, longtitude 4°30'0.25"E.
Contact:

Post 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)
Riding the Rocket!
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Post 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. :-)
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Post by Commander McLane »

Thanks, all, for the answers!

I am completely satisfied. :D

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.
Post Reply