Variables

From FreeSpace Wiki
Revision as of 17:53, 10 January 2009 by TopAce (talk | contribs) (Example mission)
Jump to: navigation, search

Variables, implemented in retail FS2, allow you to store pieces of information that you can use later in a mission or a campaign. Normally, SEXP trees use fix values. Consider this basic one:

is-destroyed-delay
 2
 GTT Spomer
send-message
 #Command
 High
 Spomer destroyed

All these values are fixed. If you wanted Command to send a his message five (not two) seconds after the Spomer is destroyed, you would need a second Event with 5 instead of 2. If, however, the "delay" value (2) were a variable, you could manipulate the delay between the Spomer's death and Command sending the message in a more convenient and flexible way in one Event. Variables are dynamic values that can be changed during the course of the mission.

Basics

Right-click on any argument in the Events list and look at the three options at the bottom of the menu, Add Variable, Modify Variable, and Replace Variable. If your Events window is empty, create an Event.

  • Add Variable allows you to create a new variable. If you're using FreeSpace Open, your Add Variable window looks somewhat like this. You can add a number or a string-based variable. Number-based variables must contain numbers only, while string-based values can use letters. From a technical point of view, it's unimportant how you name your variable. Then set your variable's default value.
    • In our example, let's create a number-based variable that is named "MessageDelay" and its default value is 2.
  • Modify Variable allows you to change a variable that you've already created. You can modify only one variable at a time. This feature is not to be confused with the modify-variable one.
  • Replace Variable replaces the currently selected SEXP argument with the variable of your choice.
    • If you clicked on "2" in the example, you could change it to MessageDelay using Replace Variable --> MessageDelay. If the "2" is changed to MessageDelay (which receives a red icon), then you've been successful.

Variable values can be changed using the modify-variable SEXP, which you can find under Add Operator --> Change --> Special. A sample Event that adds 1 to a number-based variable looks like this.

Four Types of Variables

There are four types of variables.

Standard variables This type of variables are used in one mission only. If the player ends the mission in any way (jumping out, dying, restarting, etc.), all the standard variables will be reset to their default values. Whether the player is allowed to progress in the campaign or not is inconclusive (compare campaign-persistent variables).

Player-persistent: This type of persistent variable is stored in the pilot file. Further, they are stored immediately when they are created/changed from within a mission. This means that any change is going to be there regardless of the later outcome of the mission. Player-persistent variables can be accessed and changed from any campaign played with the same pilot.

Campaign-persistent: These persistent variables are stored in the campaign file, and are only stored when a campaign progresses to a new mission. So if the player is killed or quits during a mission, and changes made to a campaign-persistent variable are not stored.

Network variables are only relevant in a multiplayer game. Non-network variables are only ever altered on the server. This means that they can not be used in messages or scripting because all the client machines will only ever have the default value. If you need to have the value of a variable display in a message you will need to mark it as a network variable.

Variables and Messages

Variables can also be used in messages. To do this, use the variable's name preceded by the '$' token (for example "$killcount"), and in-game this will be replaced with the value of the variable. It works for both numeric and string variables.

Example mission

Main Article: Variables example mission

This is a mission in which the player must take down all four of the Sathanas's main beam weapons to win the mission. This is like Bearbaiting, except this mission is solely made as a variables tutorial. The juggernaut will not fire at you or jump out.

Links