Ifelse(math) & Cond(math) (Triggers)

Started by Ricepigeon, September 15, 2015, 04:33:10 PM

Previous topic - Next topic

Ricepigeon

Both of these triggers take three arguments. If the first is nonzero, IfElse and Cond will return the value of the second argument. Otherwise, they will return the value of the third argument. How each trigger handles the evaluation of each argument differs depending on which one is used. See section "IfElse vs Cond" for more info on this.

Format:
    IfElse(exp_cond,exp_true,exp_false)
    Cond(exp_cond,exp_true,exp_false)
Arguments:
    exp_cond
        Expression to test.
    exp_true
        Expression specifying value to return if exp_cond is nonzero.
    exp_false
        Expression specifying value to return if exp_cond is zero.
Return type:
    Type of exp_true or exp_false, whichever is returned.
Error conditions:
    Returns bottom if exp_cond evaluates to bottom, or if exp_true or exp_false (whichever is returned) evaluates to bottom. See section "IfElse vs Cond" for more info.

Example:

value = ifelse(var(3)!=0,1,2)
  Sets value to 1 if var(3) is not zero, and sets value to 2 if var(3) is 0.

value = Cond(var(3)!=0,1,2)
  Sets value to 1 if var(3) is not zero, and sets value to 2 if var(3) is 0.


IfElse vs Cond:
While IfElse and Cond appear to be functionally identical to each other on the surface, both triggers actually operate differently in how expressions are evaluated. In the case of IfElse, all three arguments are evaluated prior to the actual execution of the IfElse trigger. Cond, on the other hand, will perform the logical test first before evaluating exp_true and exp_false. Because of this difference, IfElse can sometimes produce unintended debug errors in the mugen console. Consider the following examples;


trigger1 = IfElse(time>0,1/time,1)
trigger2 = Cond(time>0,1/time,1)


In both cases, each statement will return the value of 1/time when time is greater than 0, and will return 1 if time is equal to zero. However, because IfElse will still attempt to evaluate both expressions before the actual trigger is executed, when time is equal to 0, IfElse will still attempt to evaluate the argument 1/time, which will result in division by 0 and thus log a debug error to the console. Cond, on the other hand, will only evaluate the expressions that are actually used, so the argument of 1/time is ignored completely when time=0.

While IfElse may seem depricated or redundant because of this behavior, there are still reasons to use it over Cond. The first and perhaps most obvious reason is that, unlike IfElse, Cond is a trigger that is exclusive to MUGEN 1.0 and later versions, so users of WinMugen will be unable to use Cond. The other reason to use IfElse over Cond is if you have variable assignment operators in either branch that still needs to be evaluated regardless of the result of the logic test. Consider the following example:


trigger1 = IfElse(time>0,var(0):=1,var(0):=var(0)+1)
trigger2 = Cond(time>0,var(0):=1,var(0):=var(0)+1)


In the above example with trigger1, when time=0, var(0) will be set to 1 and then incremented by 1, thus the value of var(0) will be equal to 2. When time>=1, however, var(0) will be set to 1 and then incremented by 1 yet again, always setting var(0) equal to 2 regardless of the value of time.

With trigger2 however, since Cond only evaluates the expressions that are actually used, when time=0, var(0) will be set to 1, however, the expression var(0):=var(0)+1 will never be evaluated, so the value of var(0) will be 1. When time>=1, the expression of var(0):=1 will not be executed, and only the increment to var(0) will occur, thus over time, the value of var(0) will be 2, 3, 4, 5, etc...

Of course the above is only an arbitrary example to illustrate how IfElse and Cond handle variable assignments and not something that has any practical use.