Signup Now
Results 1 to 3 of 3
  1. #1
    Free User albino's Avatar
    Join Date
    Apr 2014
    Location
    Brazil
    Posts
    183
    Reputation
    27
    Rep Power
    22

    Post Using switch() case in Lua!

    I wanted to use a switch() case: in one of my script and I couldn't find anywhere how to use it in lua.

    After some research, I managed to get it working and I would like to share this in case someone looks for it.

    function switch(n, ...)
    for _,v in ipairs {...} do
    if v[1] == n or v[1] == nil then
    return v[2]()
    end
    end
    end

    function case(n,f)
    return {n,f}
    end

    function default(f)
    return {nil,f}
    end

    switch(var,
    case(1,
    --do something
    end),

    case(2,
    --do something
    end),

    default(
    --do something
    end)
    )
    Last edited by albino; 01-18-2016 at 04:43 PM.

  2. #2
    Free User
    Join Date
    Dec 2015
    Posts
    70
    Reputation
    10
    Rep Power
    18
    Quote Originally Posted by albino View Post
    I wanted to use a switch() case: in one of my script and I couldn't find anywhere how to use it in lua.

    After some research, I managed to get it working and I would like to share this in case someone looks for it.

    function switch(n, ...)
    for _,v in ipairs {...} do
    if v[1] == n or v[1] == nil then
    return v[2]()
    end
    end
    end

    function case(n,f)
    return {n,f}
    end

    function default(f)
    return {nil,f}
    end

    switch(var,
    case(1,
    --do something
    end),

    case(2,
    --do something
    end),

    default(
    --do something
    end)
    )


    Can you show what shoud looks case that will:

    when pressed ctr+1 (or 1) connect('z','z','z')
    when pressed ctr+2 (or 2) connect('y','y','y')

    etc?

  3. #3
    Free User albino's Avatar
    Join Date
    Apr 2014
    Location
    Brazil
    Posts
    183
    Reputation
    27
    Rep Power
    22
    Quote Originally Posted by klabaternik View Post
    Can you show what shoud looks case that will:

    when pressed ctr+1 (or 1) connect('z','z','z')
    when pressed ctr+2 (or 2) connect('y','y','y')

    etc?
    For that case it's better if you use the built-in hotkey feature:


 

 

Posting Permissions

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