Announcement

Collapse
No announcement yet.

Is there any "select case" or "switch case" structure in ams script?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is there any "select case" or "switch case" structure in ams script?

    I have many conditions should have be checked, is there "switch case" or "select case" structure in ams script I could use instead of a single if and many many elseif ?

  • #2
    No, there is no "switch" statement in Lua, but most likely (if inside each case you only perform assignments) you can get the results you need using a table, indexing it using the "case" value.

    Ulrich

    Comment


    • #3
      Originally posted by Ulrich View Post
      No, there is no "switch" statement in Lua, but most likely (if inside each case you only perform assignments) you can get the results you need using a table, indexing it using the "case" value.

      Ulrich
      I think this is something what Ulrich is aiming at, but with functions. It isn't really that great though, but occasionally this might help.

      Code:
      -- globals
      function switch(cases, value)
          if(type(cases[value]) == "function")then
              cases[value]();
          else
              if(type(cases.default))then
                  cases.default();
              end    
          end
      end
      
      -- anywhere
      local cases = {
          [10] = function()
              Dialog.Message("10", "10");
          end;
          
          [100] = function()
              Dialog.Message("100!", "100!");
          end;
          
          default = function()
              Dialog.Message("Ehh", "I don't know");
          end;
      };
      
      switch(cases, 100);
      Bas Groothedde
      Imagine Programming :: Blog

      AMS8 Plugins
      IMXLH Compiler

      Comment


      • #4
        Yes, I was thinking something like this:
        Code:
        [COLOR="#008000"]--[[
        switch(a) {
          case 1: x = 5;
              y = 7;
              z = 9;
              break;
              
          case 2: x = 3;
              y = 3;
              z = 0;
              break;
              
          case 3: x = 5;
              y = 2;
              z = 3;
              break;
              
          default: x = 1;
              y = 2;
              z = 3;
        }
        --]][/COLOR]
        
        [COLOR="#0000FF"]local[/COLOR] tSwitch [COLOR="#FF0000"]=[/COLOR] [COLOR="#FF0000"]{};[/COLOR]
        tSwitch[COLOR="#FF0000"][[/COLOR][COLOR="#000000"]1[/COLOR][COLOR="#FF0000"]][/COLOR] [COLOR="#FF0000"]=[/COLOR] [COLOR="#FF0000"]{[/COLOR] x [COLOR="#FF0000"]=[/COLOR] [COLOR="#000000"]5[/COLOR][COLOR="#FF0000"];[/COLOR] y [COLOR="#FF0000"]=[/COLOR] [COLOR="#000000"]7[/COLOR][COLOR="#FF0000"];[/COLOR] z [COLOR="#FF0000"]=[/COLOR] [COLOR="#000000"]9[/COLOR] [COLOR="#FF0000"]};[/COLOR]
        tSwitch[COLOR="#FF0000"][[/COLOR][COLOR="#000000"]2[/COLOR][COLOR="#FF0000"]][/COLOR] [COLOR="#FF0000"]=[/COLOR] [COLOR="#FF0000"]{[/COLOR] x [COLOR="#FF0000"]=[/COLOR] [COLOR="#000000"]3[/COLOR][COLOR="#FF0000"];[/COLOR] y [COLOR="#FF0000"]=[/COLOR] [COLOR="#000000"]3[/COLOR][COLOR="#FF0000"];[/COLOR] z [COLOR="#FF0000"]=[/COLOR] [COLOR="#000000"]0[/COLOR] [COLOR="#FF0000"]};[/COLOR]
        tSwitch[COLOR="#FF0000"][[/COLOR][COLOR="#000000"]3[/COLOR][COLOR="#FF0000"]][/COLOR] [COLOR="#FF0000"]=[/COLOR] [COLOR="#FF0000"]{[/COLOR] x [COLOR="#FF0000"]=[/COLOR] [COLOR="#000000"]5[/COLOR][COLOR="#FF0000"];[/COLOR] y [COLOR="#FF0000"]=[/COLOR] [COLOR="#000000"]2[/COLOR][COLOR="#FF0000"];[/COLOR] z [COLOR="#FF0000"]=[/COLOR] [COLOR="#000000"]3[/COLOR] [COLOR="#FF0000"]};[/COLOR]
        
        [COLOR="#0000FF"]function[/COLOR] switch(a) 
          [COLOR="#0000FF"]if[/COLOR] (tSwitch[COLOR="#FF0000"][[/COLOR]a[COLOR="#FF0000"]][/COLOR] [COLOR="#FF0000"]~=[/COLOR] [COLOR="#0000FF"]nil[/COLOR]) [COLOR="#0000FF"]then[/COLOR]
            [COLOR="#008000"]-- case[/COLOR]
            [COLOR="#0000FF"]return[/COLOR] tSwitch[COLOR="#FF0000"][[/COLOR]a[COLOR="#FF0000"]].[/COLOR]x[COLOR="#FF0000"],[/COLOR] tSwitch[COLOR="#FF0000"][[/COLOR]a[COLOR="#FF0000"]].[/COLOR]y[COLOR="#FF0000"],[/COLOR] tSwitch[COLOR="#FF0000"][[/COLOR]a[COLOR="#FF0000"]].[/COLOR]z[COLOR="#FF0000"];[/COLOR]
          [COLOR="#0000FF"]else[/COLOR]
            [COLOR="#008000"]-- default[/COLOR]
            [COLOR="#0000FF"]return[/COLOR] [COLOR="#000000"]1[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#000000"]2[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#000000"]3[/COLOR][COLOR="#FF0000"];[/COLOR]
          [COLOR="#0000FF"]end[/COLOR]
        [COLOR="#0000FF"]end[/COLOR]
        
        a [COLOR="#FF0000"]=[/COLOR] [COLOR="#000000"]2[/COLOR][COLOR="#FF0000"];[/COLOR]
        x[COLOR="#FF0000"],[/COLOR] y[COLOR="#FF0000"],[/COLOR] z [COLOR="#FF0000"]=[/COLOR] switch(a)[COLOR="#FF0000"];[/COLOR]
        Paragraph[COLOR="#FF0000"].[/COLOR]SetText([COLOR="#800080"]"Paragraph1"[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"x="[/COLOR][COLOR="#FF0000"]..[/COLOR]x[COLOR="#FF0000"]..[/COLOR][COLOR="#800080"]", y="[/COLOR][COLOR="#FF0000"]..[/COLOR]y[COLOR="#FF0000"]..[/COLOR][COLOR="#800080"]", z="[/COLOR][COLOR="#FF0000"]..[/COLOR]z)[COLOR="#FF0000"];[/COLOR]
        Really not the best, but a possible approach, depending on the situation.

        Ulrich

        Comment

        Working...
        X