User Tag List

Results 1 to 6 of 6

Thread: Alternative to most "elseif" statements

  1. #1
    Enthusiast Zadax's Avatar
    Join Date
    Aug 2013
    Posts
    28
    XP
    2,582
    Level
    14
    Thanks
    8
    Thanked 24 Times in 7 Posts

    Alternative to most "elseif" statements

    Hiya! As most people know, when you want to create a script that does "X" when "Y" happens but does "W" when "Z" happens. Most people use a LOT of elseif statements, it's handy but it gets pretty messy with a lot of different events

    Example:
    Code:
    function uh(event, what) -- the "0" are just there because it's part of the method itself
    if what:WhatEver() == 1 then 
    what:Say("Whatever", 0)
    elseif what:Whatever() == 2 then
    what:Say("Whatever 2", 0)
    elseif what:Whatever() == 3 then
    what:Say("Whatever 3", 0)
    end
    end
    And so on....

    BUT, we can use locals to have a pretty much clean script. You might ask yourself "dafuq" well, I understand you do. We are going to create a pretty much clean list of variables to dont have a VERY huge script with a LOT of "elseif" statements. Now, you might think "omg shut the hell up already and teach us" Well, here is it:
    Code:
    local Whatever = {
    [1] = "Uh, Whatever",
    [2] = "Uh, Whatever2",
    [3] = "Uh, Whatever3"
    }; -- And so on
    Now, you may think this "okay, now i know how to create that, but how am i going to use it". Well, it's example time!

    Code:
    function uh (event)
    what:Say("Uh, "..Whatever[what:WhatEver()])
    end
    Okay, it's done. At this point, you should have this:
    Code:
    local Whatever = {
    [1] = "Uh, Whatever",
    [2] = "Uh, Whatever2",
    [3] = "Uh, Whatever3"
    };
    
    function uh (event, what)
    what:Say("Uh, "..Whatever[what:WhatEver()])
    end
    Hope this was useful for you.
    Regards, Zadax.
    Achievements:
    1 year registered Member Level X

  2. The Following 6 Users Say Thank You to Zadax For This Useful Post:


  3. #2
    Founder <span style='color: #FF4500'>Tommy</span>'s Avatar
    Join Date
    Apr 2013
    Posts
    7,399
    XP
    3,449,919
    Level
    378
    Thanks
    891
    Thanked 1,860 Times in 1,121 Posts
    "switch" statements are also good to use when you don't want to have a lot of "elseif" statements. Thanks for the tutorial. \o/

  4. #3
    Founder <span style='color: #FF4500'>Foereaper</span>'s Avatar
    Join Date
    Apr 2013
    Posts
    926
    XP
    38,862
    Level
    60
    Thanks
    125
    Thanked 329 Times in 156 Posts
    Quote Originally Posted by Tommy View Post
    "switch" statements are also good to use when you don't want to have a lot of "elseif" statements. Thanks for the tutorial. \o/
    There is no native switch in Lua, see the below;

    http://lua-users.org/wiki/SwitchStatement

  5. #4
    Exalted Member <span style='color: #000000'><span class='glow_4169E1'>Skrbx</span></span>'s Avatar
    Join Date
    Aug 2013
    Posts
    506
    XP
    2,646,164
    Level
    311
    Thanks
    87
    Thanked 72 Times in 63 Posts
    LUA....
    Achievements:
    Got a Sticky! Shopper 1 year registered Member Bad Seed Level XL Level C
    Awards:
    Raffle Winner

  6. #5
    Founder <span style='color: #FF4500'>Tommy</span>'s Avatar
    Join Date
    Apr 2013
    Posts
    7,399
    XP
    3,449,919
    Level
    378
    Thanks
    891
    Thanked 1,860 Times in 1,121 Posts
    Quote Originally Posted by Foereaper View Post
    There is no native switch in Lua, see the below;

    http://lua-users.org/wiki/SwitchStatement
    I thought there was...

    Quote Originally Posted by Skrbx View Post
    LUA....
    It is actually 'Lua', not "LUA". :P

  7. #6
    Founder <span style='color: #FF4500'>Foereaper</span>'s Avatar
    Join Date
    Apr 2013
    Posts
    926
    XP
    38,862
    Level
    60
    Thanks
    125
    Thanked 329 Times in 156 Posts
    Quote Originally Posted by Tommy View Post
    I thought there was...



    It is actually 'Lua', not "LUA". :P
    Nope, but it's something we could possibly implement :P

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •