Please log in to view the forum threads.

Be warned, NEVER give your account details to anyone!
Double accounts are unwanted and strictly prohibited!




If your account got hacked please immediately report this to the following Discord id's: Pr0Master#7146 or Snake#0582

You can join our DISCORD support group by using the following link: https://discordapp.com/invite/fgbMxZG


Your Infinity Team

    Lua scripting tutorial

    Share
    avatar
    Starkkz
    Developer
    Developer

    Posts : 55
    Points : 75
    Likes : 6
    Age : 20
    Country : Chile/Santiago
    Join date : 2013-05-04

    Lua scripting tutorial

    Post by Starkkz on Sat Feb 01, 2014 2:11 am

    This is my Lua scripting tutorial, I'll be posting as much as I can but I promise nothing. Before you keep reading, I would recommend you to download Lua for windows if you have a windows operating system. After the installation, run Lua scite which can be found at C:\Program Files\Lua\5.1\SciTE\SciTE.exe

    VARIABLES
    Lua variables are used for different kinds of things, there are two types of variables.

    Global variables
    Global variables are variables that could be used from anywhere at your code, at any time. Modern programmers use the global variables to increase the RAM usage and reduce the CPU usage, so their scripts become faster and they can reduce the CPU time. There are different types of variables, but I'll teach you the string and the number.

    You can simply define a sentence by using quotes at the begining and at the end.
    Code:

    myGlobalVariableName = "A sentence in quotes"

    However, numbers don't use quotes.
    Code:

    myNumberVariable = 123

    String concatenation
    We say "concatenation" when we are trying to get two strings together and make another string (sentence). You can concatenate numbers inside strings aswell, and they will be converted to strings.
    Code:

    StringA = "I like"
    StringB = "pie"
    StringC = StringA.." "..StringB
    This will result "I like pie".

    If you don't really need a space between those two strings to make a new one, just take the " "
    Code:

    StringC = StringA .. StringB
    And it will result into "I likepie".

    Mathematical operations
    You can do any mathematical operations to get new variables when you're using Lua.
    Code:

    NumberA = 3
    NumberB = 6
    NumberC = NumberA + NumberB
    And so 3 + 6 should result 9.

    The parenthesis is always important on this kinds of operations, but just use them if they're necessary.

    Case 1: A plus B splitted by C.
    Code:

    Result = A + (B / C)
    The parenthesis will set B / C as priority operation, so it will split B with C and then add A. (The parenthesis in this case is not necessary, because division and multiplication operations are set to priority as default.)

    Case 2: A plus B, splitted by C.
    Code:

    Result = (A + B) / C
    The parenthesis will set A + B as priority operation, so it will add A with B and after that it will be splitted by C. (The parenthesis in this case IS necessary, for the same reason as I told you on the previous case, adding and substracting aren't priority operations).

    Available operators
    C = A + B (Add two numbers)
    C = A - B (Substract two numbers)
    C = A * B (Multiply two numbers)
    C = A / B (Divide two numbers)
    C = A ^ B (Elevate A by B)
    C = A % B (Modulus operator, what remains from a division)
    avatar
    Starkkz
    Developer
    Developer

    Posts : 55
    Points : 75
    Likes : 6
    Age : 20
    Country : Chile/Santiago
    Join date : 2013-05-04

    Re: Lua scripting tutorial

    Post by Starkkz on Sat Feb 01, 2014 4:49 am

    CONDITIONALS
    Conditionals are one of the most important things in Lua, they are used to know if the elements are equal, different, lower or higher (In case of numbers).

    This is how you write your conditons
    Code:

    if (condition) then
       -- Some script goes here
    end
    The previous code will attempt to check if a condition is possible.

    In the following example I'll show you a comparation of the result from adding two values.
    Code:

    A = 2
    B = 3
    if (A + B == 5) then
       C = 6
    end
    The previous code WILL work, because A + B is the same as 2 + 3, and thus 2 + 3 equals to 5.

    ELSEIF
    This is like a secondary conditional keyword that could be used if the primary conditional is not possible, it executes a separate code. Otherwise, if the primary conditional is possible, the code inside the elseif won't be possible.

    Code:

    A = 3
    B = 2
    if (A - B == 5) then
       -- The code inside this condition will not be executed because
       -- A - B is the same as 3 - 2, and 3 - 2 is the same as 1, thus 1 is NOT equal to 5
    elseif (A + B == 5) then
       -- The code inside this conditional will be executed because the primary conditional "if"
       -- is not possible, and because the condition inside this "elseif" is possible
    elseif (A * B == 6) then
       -- The code inside this conditional WON'T be executed even though the condition is possible
       -- Because the condition in the previous "elseif" was possible
    end

    ELSE
    The else keyword works almost like the elseif keyword, but only if none of the previous conditionals was possible.
    Code:

    A = 1
    B = 2
    if (B - A == 0) then
       -- This condition is not possible
    else
       -- This condition is possible, set C to zero
       C = 0
    end

    Conditional operators
    A >= B (A is higher or equal to B)
    A <= B (A is lower or equal to B)
    A == B (A is equal to B)
    A < B (A is lower than B)
    A > B (A is higher than B)
    A ~= B (A is NOT equal to B)

    Extra conditional keywords
    and checks if two conditionals or more are possible at the same time
    Code:

    if (conditionalA and conditionalB) then
    or  checks if any of the conditionals is possible
    Code:

    if (conditionalA or conditionalB) then
    avatar
    Starkkz
    Developer
    Developer

    Posts : 55
    Points : 75
    Likes : 6
    Age : 20
    Country : Chile/Santiago
    Join date : 2013-05-04

    Re: Lua scripting tutorial

    Post by Starkkz on Sat Feb 01, 2014 3:51 pm

    FUNCTIONS
    Lua functions are objects that can execute a certain action or return something.
    Let's say that we'll make a mathematical function that will add two numbers.

    Code:

    function add()
    end
    The previous function is empty, it has only it's name. To make it functional, we have to add arguments, so those arguments will be used to do it's functionality.

    Code:

    function add(X, Y)
    end
    Those two arguments are X and Y, two values, however the function is still empty because it doesn't execute any action nor does return anything.

    Let's make our function return X + Y.
    Code:

    function add(X, Y)
       return X + Y
    end
    Pretty simple, isn't it? Our function is not empty anymore because it's action is to add two numbers and then push it back as a result from calling our function. Remember that any code that remains after the "return", won't be executed.

    Now we are ready to call our function in the Lua code.
    Code:

    Value = add(10, 5)
    In this case, when we call the function "add", inside the arguments X equals 10 and Y equals 5, so  the function will return 10 + 5 and Value will equal to 15.

    Debugging / outputting
    We say "debugging" when we can see in a text line what our script is doing. There is a basic function called "print", with it you can write some sentences in your console.

    Code:

    print("Hello world")
    This is one of the easiest things that most of the programmers know, debugging "Hello world" to the scripting console.

    But you can still use it for more things, if you want to see the result of the call in our previous function.
    Code:

    Value = add(10, 5)
    print(Value)
    You must remember that the quotes will ONLY be used when you are going to make a sentence (a string).

    You can now use conditionals to make an absolute value function.
    Code:

    function absolute(X)
       if X < 0 then
          -- If X is lower than zero, return minus X
          return -X
       else
          -- Otherwise, that means that X is zero or higher than zero
          return X
       end

       -- Both conditionals up there are possible, so that means that we can't print anything after it.
       print("TEST")
       -- "TEST" will not be displayed
    end

    print( absolute(-10) )
    print( absolute(10) )
    avatar
    Starkkz
    Developer
    Developer

    Posts : 55
    Points : 75
    Likes : 6
    Age : 20
    Country : Chile/Santiago
    Join date : 2013-05-04

    Re: Lua scripting tutorial

    Post by Starkkz on Sat Feb 01, 2014 7:12 pm

    LOCAL VARIABLES
    Local variables are similar to the global variables, the only difference is that they can be accessed from certain point, while the global variable can be accessed from anywhere. You usually use local variables because they help you to save some RAM memory, as after calling a conditional or a function with a local variable, this local variable will be removed from the memory and you won't be able to use it later.

    Code:

    function testLocalVariable()
       local StringVariable = "This variable is local"
       print("Variable = "..StringVariable)
    end

    testLocalVariable()
    print( type(StringVariable) )
    As you see, after you call the function "testLocalVariable" the script defines a new variable named "StringVariable". The difference, is that you can only access to this variable from inside the function but you can't do it from out side.

    When you debug the previous script, it should output the following.

    This variable is local
    nil
    It outputs nil because this variable wasn't found in the list of the global variables.

    It's quite different if you add the local prefix to the variable, this is what it would output if you took the local keyword.

    This variable is local
    string
    Because you took the local keyword, the function's priority will be setting this variable as a global variable.
    avatar
    Starkkz
    Developer
    Developer

    Posts : 55
    Points : 75
    Likes : 6
    Age : 20
    Country : Chile/Santiago
    Join date : 2013-05-04

    Re: Lua scripting tutorial

    Post by Starkkz on Sun Feb 02, 2014 3:15 pm

    TABLES
    Tables are like lists that store elements inside, tables are the most advanced things that are used in Lua. It's like the combination of classes and arrays (You might have heard of them). I'll teach you the basics of it.

    Imagine a simple list of the results in a competition.
    Code:

    -- Make a new table with the results
    CompetitionResults = {}

    -- In first place
    CompetitionResults[1] = "Winner name"

    -- Second place
    CompetitionResults[2] = "Second one"

    -- Third place
    CompetitionResults[3] = "Third one"

    -- The rest of the losers
    CompetitionResults[4] = "I"
    CompetitionResults[5] = "like"
    CompetitionResults[6] = "pie"
    What I used in the previous code is called key and value, where the key is the index of the element (in this case, the number) and the value is what it contains (the string).

    There are different ways to do this faster.
    Code:

    Test = {"FirstElement", 45, "test"}
    Right there we have shorten this.

    Code:

    Test = {}
    Test[1] = "FirstElement"
    Test[2] = 45
    Test[3] = "test"

    Now take a note, the key could be ANY type of value; strings, numbers, tables, functions. In the case of the strings, there are four ways to define a string key in a table.
    Code:

    Test = {Key = 123}

    Test = { ["Key"] = 123}

    Test = {}
    Test.Key = 123

    Test = {}
    Test["Key"] = 123
    I usually prefer the first and the fourth, the fourth because you can use disallowed characters such as space.

    You can also define functions inside Lua tables.
    Code:

    ScriptTable = {}

    function ScriptTable.DoSomething()
       print("Hello world")
    end

    ScriptTable.DoSomething()

    Later, you can implement this tables to make tables with player data stored in it.
    Code:

    Player = {}
    Player.Name = "Starkkz"
    Player.Health = 250
    Player.MaxHealth = 250
    Player.Speed = 10
    Player.Team = 2

    Sponsored content

    Re: Lua scripting tutorial

    Post by Sponsored content


      Current date/time is Sun Jan 21, 2018 1:41 am