Difference between revisions of "If-then-else"

From FreeSpace Wiki
Jump to: navigation, search
(new section)
m (if-then-else with multiple possibilities: some oversights)
Line 21: Line 21:
 
In this example mission, we'll use ''percent-ships-destroyed'' on Beta, four fighters. If any of Beta jumps out or all are destroyed, Command will inform you how many of them were killed. Here are the logically possible outcomes:
 
In this example mission, we'll use ''percent-ships-destroyed'' on Beta, four fighters. If any of Beta jumps out or all are destroyed, Command will inform you how many of them were killed. Here are the logically possible outcomes:
  
*All survives
+
*All survived
 
*1 killed
 
*1 killed
 
*2 killed
 
*2 killed
Line 27: Line 27:
 
*All killed
 
*All killed
  
In retail, you'd need a separate events for each message, accumulating to five events altogether. With embedded ''if-then-else'', you need only one. The event looks like this:
+
In retail, you need one event for each message, accumulating to five events altogether. With embedded ''if-then-else'', you need only one. The event looks like this:
  
 
  $Formula: ( when  
 
  $Formula: ( when  
Line 70: Line 70:
 
  )
 
  )
  
The event will trigger if there is 0 of Beta in the mission ( = ( num-ships-in-wing "Beta" ) 0 ). This event would fire immediately if Beta weren't present on startup, so watch out if you want to apply this to a ship that arrives later. At the moment, we aren't interested in how many of Beta survived. We just want to tell FRED when to "look for survivors." Now comes the first "main" ''if-then-else'', below which the rest are subordinated. If 100 percent of Beta is destroyed, Command will say "All destroyed." Now, if this scenario becomes false, so at least one survives, FRED will look below to see what to do if sending "All destroyed" is out of the question. The trick here is to use another ''if-then-else'', not a send-message. ''If-then-else'' has two possible outcomes, while send message has one: it sends a message and that's it.
+
The event will trigger if there is 0 of Beta in the mission ( = ( num-ships-in-wing "Beta" ) 0 ). This event would fire immediately if Beta weren't present on startup, so watch out if you want to apply this to a wing that arrives later. At the moment, we aren't interested in how many of Beta survives. We just want to tell FRED when to "look for survivors." Now comes the first "main" ''if-then-else'', below which the rest are subordinated. If 100 percent of Beta is destroyed, Command will say "All destroyed." Now, if this scenario becomes false, so at least one survives, FRED will look below to see what to do if sending "All destroyed" is out of the question. The trick here is to use another ''if-then-else'', not a ''send-message''. ''If-then-else'' has two possible outcomes, while ''send-message'' has one: it sends a message and that's it.
  
These chains must be set up according to logic. In this case, I don't jump from (percent-ships-destroyed 100) to (percent-ships-destroyed 50) then (percent-ships-destroyed 75) because that's not how math works. In this case, I need (percent-ships-destroyed 75) subordinated below (percent-ships-destroyed 100). If 75% of Beta is destroyed, Command will say "Three destroyed." If (percent-ships-destroyed 75) becomes false, then FRED will look for another possibly, another subordinated ''if-then-else''. The chain continues until all logically possible scenarios are depleted; in this case, (percent-ships-destroyed 25) becoming false, which equates to all of Beta surviving.
+
These chains must be set up according to logic. In this case, I don't jump from (percent-ships-destroyed 100) to (percent-ships-destroyed 50) then (percent-ships-destroyed 75) because that's not how math works. In this case, I need (percent-ships-destroyed 75) subordinated below (percent-ships-destroyed 100). If 75% of Beta is destroyed, Command will say "Three destroyed." If (percent-ships-destroyed 75) becomes false, then FRED will look for another possibility, another subordinated ''if-then-else''. The chain continues until all logically possible scenarios are depleted; in this case, (percent-ships-destroyed 25) becoming false, which equates to all of Beta surviving.
  
 
Get the example mission [http://www.mediafire.com/?cdzc1by29c6dqlv here]. Destroying the container is Beta's departure cue. The No Traitor flag is set, so you can kill as many Beta as you want to test each possibility.
 
Get the example mission [http://www.mediafire.com/?cdzc1by29c6dqlv here]. Destroying the container is Beta's departure cue. The No Traitor flag is set, so you can kill as many Beta as you want to test each possibility.
  
 
[[Category:SCP SEXPs]]
 
[[Category:SCP SEXPs]]

Revision as of 09:27, 13 July 2011

If-then-else (Conditional operator)
	Performs one action if a condition is true (like "when"), or another action (or set of actions) if the condition is false.  Note that this sexp only completes one of its branches once the condition has been determined; it does not come back later and evaluate the other branch if the condition happens to switch truth values.

Takes 3 or more arguments...
	1:	Boolean expression to evaluate.
	2:	Actions to take if that expression becomes true.
	Rest:	Actions to take if that expression becomes false.

Example SEXP tree

If-then-else.jpg

In this experimental mission, Omicron departs 20 seconds into the mission.

If-then-else is equivalent to a when X paired with a when not-X, for the same X. Thus if if-then-else appears in an event by itself, one of its branches will immediately fire. Therefore, this sexp will probably be more useful when chained after, or used as a subordinate to, a controlling sexp. In the example above, the destroyed-or-departed-delay sexp controls when if-then-else should check if its condition is true or not.

If you substitute destroyed-or-departed-delay with true, then the player's shield icon will flash at mission start. If you substitute it with (has-time-elapsed 15), then the player has 15 seconds to destroy Omicron. If he manages to kill Omicron in 15 seconds, then his ETS will flash. If not, his shield icon will. If the player kills Omicron in the 16th second, his ETS gauge will not flash.

if-then-else with multiple possibilities

A single if-then-else is sufficient to check an either/or duality. Omicron is either destroyed or not, there's nothing in between. By using SEXP embedding, if-then-else can be used to take multiple, not only two, possibilities into account for scenarios that have more than two logical outcomes.

In this example mission, we'll use percent-ships-destroyed on Beta, four fighters. If any of Beta jumps out or all are destroyed, Command will inform you how many of them were killed. Here are the logically possible outcomes:

  • All survived
  • 1 killed
  • 2 killed
  • 3 killed
  • All killed

In retail, you need one event for each message, accumulating to five events altogether. With embedded if-then-else, you need only one. The event looks like this:

$Formula: ( when 
   ( = ( num-ships-in-wing "Beta" ) 0 ) 
   ( if-then-else 
      ( percent-ships-destroyed 100 "Beta" ) 
      ( send-message 
         "#Command" 
         "High" 
         "All destroyed" 
      )
      ( if-then-else 
         ( percent-ships-destroyed 75 "Beta" ) 
         ( send-message 
            "#Command" 
            "High" 
            "Three destroyed" 
         )
         ( if-then-else 
            ( percent-ships-destroyed 50 "Beta" ) 
            ( send-message 
               "#Command" 
               "High" 
               "Two destroyed" 
            )
            ( if-then-else 
               ( percent-ships-destroyed 25 "Beta" ) 
               ( send-message 
                  "#Command" 
                  "High" 
                  "One destroyed" 
               )
               ( send-message 
                  "#Command" 
                  "High" 
                  "All survived" 
               )
            )
         )
      )
   )
)

The event will trigger if there is 0 of Beta in the mission ( = ( num-ships-in-wing "Beta" ) 0 ). This event would fire immediately if Beta weren't present on startup, so watch out if you want to apply this to a wing that arrives later. At the moment, we aren't interested in how many of Beta survives. We just want to tell FRED when to "look for survivors." Now comes the first "main" if-then-else, below which the rest are subordinated. If 100 percent of Beta is destroyed, Command will say "All destroyed." Now, if this scenario becomes false, so at least one survives, FRED will look below to see what to do if sending "All destroyed" is out of the question. The trick here is to use another if-then-else, not a send-message. If-then-else has two possible outcomes, while send-message has one: it sends a message and that's it.

These chains must be set up according to logic. In this case, I don't jump from (percent-ships-destroyed 100) to (percent-ships-destroyed 50) then (percent-ships-destroyed 75) because that's not how math works. In this case, I need (percent-ships-destroyed 75) subordinated below (percent-ships-destroyed 100). If 75% of Beta is destroyed, Command will say "Three destroyed." If (percent-ships-destroyed 75) becomes false, then FRED will look for another possibility, another subordinated if-then-else. The chain continues until all logically possible scenarios are depleted; in this case, (percent-ships-destroyed 25) becoming false, which equates to all of Beta surviving.

Get the example mission here. Destroying the container is Beta's departure cue. The No Traitor flag is set, so you can kill as many Beta as you want to test each possibility.