Lua Coding.

Discussion in 'Science, Technology & Car Chat' started by Hairbear, Sep 14, 2011.

  1. Hairbear

    Hairbear Well-Known Member

    224
    41
    0
    Trying to learn lua and coding. Any tips? I think I've got some of the basics down, such as some functions. Not a whole lot, maybe like the easy ones like "print", "msg" and some tables.

    The tuts that i've found are kind of confusing, can anyone translate into layman terms? Or is trying to figure out Lua, more trial and error?
     
  2. I would suggest learning the basics of programming in general first, and tutorials don't particularly explain to you programming concepts. One thing to note is that programming concepts can be applied to any programming language, lua being one of them. So get familiar with the basic programming concepts first.

    Methods such as "print" and "msg" are some of the methods that you can use to help writing your code, but that's not particularly programming. Are you familiar with the basics, such as what a variable is, or conditional statements, operators, loops, recursion and such?

    Coding, whether in Lua or any other languages, has similar, if not the same structure. Pick up any introductory book in any programming language, doesn't need to be Lua, and once you learn those basic concepts, it can be transferred to use in Lua.

    Python would be a good programming language to start off with. It's easy to use and powerful.

    But to get you started off here's a very simple Lua example, which I'll explain the structure of a function:

    Code:
    -- This function is a simple function that prints "foo" when the input is 0, and "bar" when the input is anything else.
    
    -- Definition of the function begins here.
    function switch(n)
      if n == 0 then
        return "foo"
      else
        return "bar"
      end
    end
    -- Definition of the function ends here.
    
    print(switch(0))  -- Prints "foo"
    print(switch(1))  -- Prints "bar"
    print(switch(2))  -- Prints "bar"
    print(switch(0))  -- Prints "foo"
    
    
    Some explanations:

    The double dash "--" means that you are commenting a line of code out. This means that the code will not be run. It's like inserting a comment so other coders (or yourself) can understand what the code does. You can comment multiple lines by doing "-- [[ insert multiline comment here ]]".

    So firstly, you need to define a function, so I defined a function called "switch" which takes in one input "n" (or the correct term is parameter). If you've taken math, it's like having a mathematical function: f(x) = 2x. That means define a function f which takes an x, and multiply x by 2. Just in programming, it's done with a different syntax.

    "If" is a conditional statement, that means if the condition defined after "if" is true, run the next line. Otherwise, go to the "else" statement and run the lines of code for the "else" part. We see that the condition is "n == 0". The double equal sign generally means to compare. So if the value that is stored in the variable "n" is equal to 0, then do the following code. Else, do the other code.

    "Return" means that the function switch returns a value once it finishes running. So in the previous case, if you input 0, in returns the value of "foo". Any other inputs return "bar".

    In Lua, it requires you to write "end" to denote the end of a statement. So the first "end" ends the if statement, and the second "end" closes the function.

    Once you've defined your function, we use what you're familiar with, the "print" method. You know that "print" accepts a parameter, so it prints out the value. So we'll pass in our function "switch", and an input of your choice. You'll see I inputted 0, 1, 2, 0, and that will output "foo", "bar", "bar", "foo".

    Good luck
     
  3. Hairbear

    Hairbear Well-Known Member

    224
    41
    0
    This was immensely helpful. Thanks.

    Uhh. I know "variables" "if" "else "if else" "tables" but loops kind of confuse me.
     
  4. kdotc

    kdotc 안녕하세요빅뱅K-Dragon입니다

    hire dan...how much does he charge an hour as a tutor? $10? lol
     
  5. reno

    reno Well-Known Member

    i'm sure he might accept other special favours :p
     
  6. [N]

    [N] RATED [ ]

    ^reno haz exchanged favors before :trollface2:
     
  7. brown_bear

    brown_bear ☆‧° ☆﹒﹒‧ ☆ ﹒﹒‧☆‧° ☆

    8,870
    467
    21
    ^ and kung kung watcheddd......-nono
     
  8. mr_evolution

    mr_evolution ( • )( •ԅ(ˆ⌣ˆԅ)

    9,967
    590
    57
    ^ and bb recorded
     
  9. [N]

    [N] RATED [ ]

    ^and evo distributed :trollface2:
     
  10. spider-man

    spider-man Well-Known Member

    466
    55
    1
    Learn the syntax first, then go for the algorithm.
    Sorry, I haven't heard of Lua before.
     
  11. reno

    reno Well-Known Member

    stop distributing videos unless I get my % cut!!!
     
  12. Wiki it, it has the exact same programming structure as any other programming language. I've never programmed Lua before, but if you truly know how to program, it doesn't matter what the syntax is.

    And as for OP, here's a quick explanation of loops, specifically "for" and "while" loops.

    Say you have a list (or more correctly, arrays) of 10 apples (pretend the O's are apples):

    array_of_apples = [ O, O, O, O, O, O, O, O, O, O ]

    a for loop means that you go through each of the apples in the array, and you do things with it. So for example:

    Code:
    
    for apple in array_of_apples
       do
           slice(apple)
       end
    end
    
    -- slice is a function that we will assume is defined. Obviously you'll have to define what you want to do with it.
    
    So that means for each of the items in the list (apples), do something (slice them).

    Now while loops are similar, but only go up to a certain condition. So if you have the same array of apples, and the following code:

    Code:
    
    while n < 5
       do
          slice(array_of_apples[n])
          n = n + 1
       end
    end
    
    
    So while n is less than 5, slice the apple that's at the index "n" of the array of apples, and when done, increment n by 1. So the next "iteration" is 2, then 3, then 4 and then 5.

    So instead of doing all 10 apples in the list, it only goes up to the condition that's defined.
     
  13. Kaitou_Kid

    Kaitou_Kid Well-Known Member

    1,400
    86
    0
    there is a lua book, they sell it on amazon. its the same as the documentation on the website i believe...