User Tools

Site Tools


modding:function:switch

switch

switch

switch

Syntax (switch [exp1 exp2] … [expn expn1] [defaultexp]) → value of evaluated expression
Arguments [exp1 exp2] … [expn expn1]: A group of two expressions following each other. It the first expression evaluates to non-Nil, then the second expression is evaluated. Else the switch moves on to test the next group. There can be as many of these groups as neccessary.
[defaultexp]: an expression which will be evaluated if no group was successfully evaluated. This expression can be considered the “default” of the switch.
Returns Whatever the expression that gets evaluated returns.
Category control structure
Description Allows branching of the code by more than 1 condition. For just 1 condition, 'if' is good enough. It just evaluates all the conditions (every even argument; odd arguments are corresponding functions) in left to right order, until the first one that evaluates to non-Nil. It then invokes the corresponding function, which is the argument that directly follows that condition. If all the conditions evaluate as Nil, then the last, optional function is invoked.

Example

(setq num 3)
 
(switch
    (eq num 1)
        (plyMessage gPlayer "num = 1")
 
    (eq num 2)
        (plyMessage gPlayer "num = 2")
 
    (eq num 3)
        (plyMessage gPlayer "num = 3")
 
    (eq num 4)
        (plyMessage gPlayer "num = 4")
 
    (plyMessage gPlayer "num not in (1,2,3,4)")
)

Return to Functions list

modding/function/switch.txt · Last modified: 2014/12/27 04:40 by 127.0.0.1