Variables

From FreeSpace Wiki
Revision as of 21:34, 27 September 2007 by Mustang19 (talk | contribs)
Jump to: navigation, search

Variables can be a powerful FRED tool, although are often more of a convenience than an absolute necessity. FREDers use variables for the same two reasons that programmers do. One, they allow you to easily store pieces of information for later use. Two, they let you avoid hard-coded or "magic" numbers for arguments that you use several times in your mission's events.

Using variables is simple. You can use variables in place of any SEXP argument where you would normally use a string or number. Right-click on the argument in the Events list and look at the three options at the bottom of the menu, Add Variable, Modify Variable, and Insert Variable. Click Add Variable to create a new variable. Modify Variable allows you to change a variable that you've already created. Insert Variable replace the currently selected SEXP argument with the variable of your choice.

Three Types of Variables

There are three types of variables.

Player-persistent variables are just what they sound like.

Network-persistent variables are only relevent in a multiplayer game. They affect all players in the current game. Not sure about this. Someone with multiplayer experience, please correct.

Campaign-persistent variables carry through an entire campaign. These require some more explanation:

Say the variable "PlayerHitsLeft" stores the percent hull strength that the player had by the end of Mission 1. Say you play Mission 1 and end up with 50% hull. If you want to set the player's hull to PlayerHitsLeft (which is now 50) in Mission 2 (as if it were a red-alert), create another campaign-persistent variable in Mission 2 of the exact same name. The variable will then initialize as 50 (in our example) when you start the mission, and you can use set-hull-strength to set the player's hull equal to PlayerHitsLeft, or 50, the instant Mission 2 starts. Every time you play these two campaign missions, your hull strength at the beginning of Mission 2 will be the same as the percent hull you had left at the end of Mission 1. You could use this if, for example, you wanted to avoid the unstable red-alert SEXP. You can even manipulate PlayerHitsLeft by adding to it, dividing it, multiplying it, or whatever, to represent the player's hull being repaired or disintegrating between the two missions.

Variables do exist; use them

There are many "hackish" ways to avoid variable use that can be seen in some of the older FRED tutorials, "back then" when people where ignorant of variables (which have existed since FreeSpace 1). Many people are still new to FRED and may suggest overcomplicated solutions to problems that could more easily be solved using variables. For example, some of the older FRED tutorials posit that you can see how many waypoints a player has reached (in any order) by placing a wing of invisible fighters far out of the play area and tie each fighter in the wing to self-destruct as a waypoint is reached. You can then use the percent-ships-destroyed SEXP on the wing to see how many waypoints have been reached, as there's no SEXP to answer this question directly. This is a primitive solution to the problem, and it's an example of where variables could easily be used to get the same effect.

Fred Zone examples of uses for variables

Example 1

Example 2