Author Topic: Variables, Need Explanations  (Read 1540 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobra

  • 212
  • Snake on a Cain
    • Skype
    • Steam
    • Twitter
Variables, Need Explanations
...and before anyone says anything, I've read Karajorma's FAQ, still can't understand, really.

Anyway, I'm trying to figure out the usage of variables and how to set them up.

What are the various instances that I can use variables in (such as the Sleipner tutorial mission, which didn't teach me anything, really :P) and what numbers do I put "in the box?"
To consider the Earth as the only populated world in infinite space is as absurd as to assert that in an entire field of millet, only one grain will grow. - Metrodorus of Chios
I wept. Mysterious forces beyond my ken had reached into my beautiful mission and energized its pilots with inhuman bomb-firing abilities. I could only imagine the GTVA warriors giving a mighty KIAAIIIIIII shout as they worked their triggers, their biceps bulging with sinew after years of Ivan Drago-esque steroid therapy and weight training. - General Battuta

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Variables, Need Explanations
The FRED Walkthrough actually contains probably one of the more simply uses of a variable so I suppose it's a good place to start.

In the walkthrough you have 4 transports which will attempt to dock with the Orion. At any point during the mission the player can blow up one or more of the transports. The mission designer has decided that if two transports successfully dock, offload their marines and then disengage before they are killed then the transports are captured. So how do you FRED this little senario?

If you don't understand variables what you would do is this. You know that there is an event "Sleipner 1 done" which comes true when the transport disengages and corresponding events for the other 3. So you make a huge event using OR and test that two of those events have come true. That's the pain in the arse way of doing it. Variables is better.

Instead you create a variable called DockCount. DockCount is the number of transports who have successfully docked with the Orion. At the start of the mission nothing has docked successfully so you set the initial value to 0.

What you then want to do is add 1 to this value every time a transport docks successfully with the Orion. Cause if Sleipner 1 makes it then 1 transport has done it successfully.

So how do you do this? Well if you look at the walkthrough you can see they did this.

modify-variable
-DockCount
- +
-- DockCount
-- 1

That's actually pretty simple. All it says is modify the number in the box to this new number. To work out the new number open the box, see what's in there and add 1 to it.


So now that you know that it should be pretty easy to figure out what is going on in the walkthrough mission. Every time a transport docks successfully you add 1 to the DockCount. So now all you need is an event that checks when the DockCount is 2 or more. As you can see the "Relentless Captured" event does that. It checks to see that DockCount is more than 1. As soon as it is the NTF have captured the ship.


A counter is about the easiest way of using variables there are others but you'll have to have a handle on this one to get much further with them :)
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Shade

  • 211
Re: Variables, Need Explanations
They can be somewhat confusing, so lets see if I can clear it up. Might as well start from the beginning I guess.

1) Adding variables to a mission: To do this, you enter the events editor and right-click on an event, then select Add Variable. Any event, it doesn't matter, as variables are not tied to the events you click on to add them, that's simply how you access the command to do it.

Once you have done that, a window pops up where you can select the type (string or number), the name (how you access it later from events), the default value (this is the value of the variable before any operations are carried out on it. Also, it's what the variable's value is displayed as in FRED - This never changes unless the default does, so don't fret), and where you will also find checkboxes for making the variable player- or campaign-persistent (we'll ignore those here).

The type has to fit what you intend to use it for of course (string for a ship name for example, number if you want to count the number of times a ship has been disabled in the mission), and the name obviously should be descriptive of that as well. The default value is something you will need to set depending on how you use it, but it can be modified later once you know what you need it to be.

Once all field are filled out, click ok and the window will disappear as if nothing had happened. Now right click an event again, and a new option has shown up: Modify Variable. Select this and you go back to the window you had before, only the name field is now a dropdown box where you can select any variable in the mission. Here, you can alter things like the default value mentioned before, so this is where you will go once you know what you need it to be.

2) Accessing variables with events: Once you have your variables laid out ready for use, there are two ways to access them from within the events system. A) Using the variable's value directly in an evaluation or operation, by right-click on the item you want to replace with a variable and select the now-available Replace Variable option, and B) Using the modify-variable SEXP found under change->special. The latter of those can also involve the former, as you can use an arithmetic operation as part of that SEXP.

Now, what can we use this for?

The first and most obvious use is counting - In the example the walkthrough uses you have a number of transports docking with an Orion, and variables are used to keep track of how many of them survive. It's actually a very good example, but just not very well explained, so I'll try to explain it properly in the context of what I've already said earlier:

The first thing we see is the window that comes up when you add a variable. We can see that it's initialized to 0, which is because we want to count up from 0 until we've reached the number of transports required for a capture, in this case two.

Now look at the first event of the Sleipner chain ("Sleipner 1 done"). All the stuff that actually happens in unimportant, what matters is the bit at the end. Here, the variable created before is incremented by one, by using the modify-variable SEXP combined with a "+" arithmetic SEXP to add 1 to the existing value of the variable. This is then done for each transport, with the result that each transport that successfully docks raises the value of the variable by one.

The next part of the Sleipner chain is two events ("Relentless boarded" and "Relentless captured") that rely on the value of this variable to determine when they go off. Here you see the variable used in an evaluation without being modified in any way, and you also start to see why variables were the best solution here: There's no need for a complicated evaluation or chained events to make sure they go off in the right sequence, and no need to keep track of every single transport individually. It's very simple, when the counter variable hits 1, one of the transports (could be any one of them) has docked and the first event should go off, and when it hits two, two of them have docked and the second event should go off, and that's all you need to worry about since variables were used.

I hope that explains it a bit better than the walkthrough did. Another example of using a number variable to count can be found on This Wiki Page, where variables are used to effect a delay in an event chain by counting seconds.

Now, string variables are esentially no different, except you can't count with them: They can substitute text just like number variables can substitute numbers. For example, if you want a wingman to send a message, but are worried he might be dead, you could use a string variable in place of the ship name in the send-message SEXP, and modify that variable to a different ship name in case your first choice got killed prematurely.

That'll do for now, as I'm hungry and need food. Hope it helps :)
Report FS_Open bugs with Mantis  |  Find the latest FS_Open builds Here  |  Interested in FRED? Check out the Wiki's FRED Portal | Diaspora: Website / Forums
"Oooooooooooooooooooooooooooooooooooooooh ****ing great. 2200 references to entry->index and no idea which is the one that ****ed up" - Karajorma
"We are all agreed that your theory is crazy. The question that divides us is whether it is crazy enough to have a chance of being correct." - Niels Bohr
<Cobra|> You play this mission too intelligently.

 
Re: Variables, Need Explanations
read it a couple more times really carefully; thats what it took to get me to understand it.
Sig nuked! New one coming soon!

  

Offline Mobius

  • Back where he started
  • 213
  • Porto l'azzurro Dolce Stil Novo nella fantascienza
    • Skype
    • Twitter
    • The Lightblue Ribbon | Cultural Project
Re: Variables, Need Explanations
Copy and write the structure of events which use variables in a paper and read it.
The Lightblue Ribbon

Inferno: Nostos - Alliance
Series Resurrecta: {{FS Wiki Portal}} -  Gehenna's Gate - The Spirit of Ptah - Serendipity (WIP) - <REDACTED> (WIP)
FreeSpace Campaign Restoration Project
A tribute to FreeSpace in my book: Riflessioni dall'Infinito