• Tutorials

  • Home
  • GPDL $SWITCH Keyword

    DungeonCraft Help Home

       $SWITCH ( expression )
       {
           $CASE  exp1 :  statements
           $CASE  exp2 :  statements
           .
           .
           $CASE  expN :  statements
           $DEFAULT : statements
       };

    The expression in parentheses is evaluated once and its value is compared with exp1, exp2, etc. in turn. For each expN that is equal to expression the associated statements are executed. You can use the $BREAK statement to exit from the $SWITCH without further comparisons or statement executions. In fact, this is most common.

    Example:

       $SWITCH ( $GET_CHAR_ATTR(0,"HairStyle"))
       {
           $CASE "flattop": addExperience(20); $BREAK;
           $CASE "bald"   : addHat(); $BREAK;
           $CASE "braided": cutHair(0); $BREAK;
           $DEFAULT: $SET_CHAR_ATTR(0,"HairStyle","bald");
       };

    The $BREAK statements ensure that only one of the four cases gets executed. Without the $BREAK, all the statments after the first match would be executed. The $DEFAULT matches any value of expression.

    You can substitute $GCASE for any of the $CASE options. This uses $GREP instead of a test for equality.