• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Broken Down

Codex NG

Recurrent Flamer
Joined
Jul 24, 2015
Messages
2,994
Solutions
12
Reaction score
1,657
Before we start building anything in any language,
It is important to have a clear understanding of the language itself


Comments, Naming Convention, Structure:
Comments in every programming language allow the programmer and the non-programmers to have a general idea of what is going on in the code, especially when that code becomes excessively large and or complex.

There are 2 types of comments in lua
Single Line Comment:
-- This is a single line comment, everything til the end of the line will be commented out & ignored by the lua interpreter
Multi-Line Comment:
--[[ This is a multi-line comment, everything that is contained within the --[[ ]] is ignored by the lua interpreter ]]

Technically speaking there is an unwritten 3rd type of comment in every language, which is called the naming convention although naming convention falls under a consistent style of program (e.g. nameOfPlayer, nam_of_player etc..) it can also be used as a comment style of programming.

Lets look at an example:

This piece of code, will run and execute as expected.
Although its functional, it isn't very readable which is a problem because we have no idea what is going on in this script.
Code:
local s = 18000 local x = { p = 0.50, f = 0.10}
function onEquip(cid, item, slot)
local b = (isPremium(cid)) and x['p'] or x['f'] local r = g(cid, true) + b
if getPlayerStorageValue(cid, s) == -1 then setPlayerStorageValue(cid, s, 1) doPlayerSetExperiencer(cid, r) doPlayerSendTextMessage(cid, 22, "You have gained "..(b * 100).."% b exp!")
if not isPremium(cid) then doPlayerSendTextMessage(cid,21,"If you were a premium player you would receive "..(x['p'] * 100).."% instead of "..(x['f'] * 100).."% as a free account while using this ring.") end
doTransformItem(item.uid, 7697, 1) end return true end
function onDeEquip(cid, item, slot) local b = (isPremium(cid)) and x['p'] or x['f'] local r = ((g(cid, true) - b) <= 1) and 1 or g(cid, true) - b
if getPlayerStorageValue(cid, s) == 1 then doPlayerSetExperiencer(cid, r) setPlayerStorageValue(cid, s, -1) doTransformItem(item.uid, 7708, 1) doPlayerSendTextMessage(cid, 22, "The bounus xp has ended.") end return true end
function g(cid, s) if s then return getExperienceStage(getPlayerLevel(cid), getVocationInfo(getPlayerVocation(cid)).experienceMultiplier)end return getConfigInfo('rateExperience') end
As I was saying the naming convention can be used not only as a consistent style of programming but also a comment, lets look at an example using the script above
Code:
local s = 18000 local x = { p = 0.50, f = 0.10}
What the heck is s, x, p & f? Nevermind what the values they hold but what are s, x, p & f used for?
It is not very clear and it would take a bit time to go through the code to see what these things are used for.

So lets rename these letters so that they describe their purpose
Code:
local xpStorage = 18000 local xpToAdd = { premacc = 0.50, freeacc = 0.10}
Now we have a general idea of what these things will be used for even if we aren't a programmer

Structure is also an important part of readability it allows us to see the level of execution, find bugs and make our less then savy code seem a bit sexy :) When we talk about structure we are referring to proper spacing & indentation of the code.

So with the above in mind I will show the original code using comments, a naming convention & structure.
Code:
local xpStorage = 18000 -- change this to a storage value not used
local xpToAdd = {
    premacc = 0.50, -- premium account bonus xp
    freeacc = 0.10  -- free account bonus xp
}

function onEquip(cid, item, slot)
    local bonus = (isPremium(cid)) and xpToAdd['premacc'] or xpToAdd['freeacc']
    local rate = getPlayerExpRate(cid, true) + bonus
    if getPlayerStorageValue(cid, xpStorage) == -1 then
        setPlayerStorageValue(cid, xpStorage, 1)
        doPlayerSetExperienceRate(cid, rate)
        doPlayerSendTextMessage(cid, 22, "You have gained "..(bonus * 100).."% bonus exp!")
        if not isPremium(cid) then
            doPlayerSendTextMessage(cid,21,"If you were a premium player you would receive "..(xpToAdd['premacc'] * 100).."% instead of "..(xpToAdd['freeacc'] * 100).."% as a free account while using this ring.")
        end
        doTransformItem(item.uid, 7697, 1)
    end
    return true
end

function onDeEquip(cid, item, slot)
    local bonus = (isPremium(cid)) and xpToAdd['premacc'] or xpToAdd['freeacc']
    local rate = ((getPlayerExpRate(cid, true) - bonus) <= 1) and 1 or getPlayerExpRate(cid, true) - bonus
    if getPlayerStorageValue(cid, xpStorage) == 1 then
        doPlayerSetExperienceRate(cid, rate)
        setPlayerStorageValue(cid, xpStorage, -1)
        doTransformItem(item.uid, 7708, 1)
        doPlayerSendTextMessage(cid, 22, "Bonus xp has ended.")
    end
    return true
end

function getPlayerExpRate(cid, stages)
    if stages then
        return getExperienceStage(getPlayerLevel(cid), getVocationInfo(getPlayerVocation(cid)).experienceMultiplier)
    end
    return getConfigInfo('rateExperience')
end
Don't worry what all this means at the moment we will get to breaking all this down at a later date, what is more important is that you have clear understanding of the language itself before we build anything meaningful. :)

Declaration:
Variable:
A variable is simply a container used to hold a value, think of a variable as a box where you can only hold 1 thing in that box.
A variable must start with a letter or underscore they can be 1 character to the entire alphabet in length, variables can contain numbers, letters or underscores but absolutely no spaces, variables are case sensative meaning wow, WoW & wOw are all different variables.
The way we declare a variable is by typing the variable's name followed by an equals sign ( = ) and the value you want to store in the variable, the equal sign is known as the assignment operator.
Code:
     local myVariable = 5 -- a variable with a default value of 5
The reason it is called the assignment operator is because we are taking the thing on the right and assigning it to the thing on the left.

If we wanted to access the variables value and assign it to a new variable or just print it to the console we could write
Code:
     local myNewVariable = myVariable -- store the value of myVariable in myNewVariable now both variables hold the value of 5
       or
     print(myVariable) -- this will print 5 in the console window
Tables:
A table is similar to a variable but instead of just holding 1 value it can hold multiple values & even other tables.
Code:
     local myEmptyTable = {} -- this is an empty table
Tables can also be assigned properties at any time, either during its declaration or as we progress through the script.
Code:
     local myTable = {
       name = 'Codex'
     }
The tables property value can then be accessed by simply using the name of the table followed by the dot operator ( . ) and then the property's name
Example:
Code:
       local myName = myTable.name -- assign myTable.name to myName
Using myEmptyTable example we can assign a property to it at a later time simply by using the table name followed by the dot operator ( . ) and then the property followed by an equals sign ( = )
Code:
     myEmptyTable.name = myName
Now both myEmptyTable & myTable both contain a property called name with the value of Codex

Tables can also be assigned a property based on its index value, think of a table as a bookshelf and its index value as one of its shelves.

Lets create a new table
Code:
     local myNewEmptyTable = {}

     myNewEmptyTable[1] = 10
Your probably wondering what the number enclosed within square brackets is, we are telling lua we want to assign the value of 10 to myNewEmptyTable's index of 1

This also could have been done during its declaration
Code:
     local myNewEmptyTable = {
       10 -- index 1
     }
The index value of a table normally starts with 1, although you can force lua to start the index count at 0 or any index you'd like simply by writing
Code:
local table_ = {
    [0] = 23, -- index 0
    27, -- index 1
    89  -- index 2
}
local table_ = {
    [0] = 23, -- index 0
    [5] = 27, -- index 5
    [100] = 89  -- index 100
}

Tables can also be assigned tables as properties
It is important to note that if you have more than 1 property in a table that you place a comma ( , ) prior to the last property listed.
Code:
     local myNewEmptyTable = {
       10, -- index 1
       innerEmptyTable = {} -- index 2
     }
Now we'll add and access a property of the innerEmptyTable

Add a property
Code:
         myNewEmptyTable.innerEmptyTable.someProperty = 34
Access the property
Code:
         local x = myNewEmptyTable.innerEmptyTable.someProperty
 
Last edited:
If tables have property names then they can be accessed either using the dot operator (table.property) or by using the the name of the property enclosed in single or double quotes (known as a string) enclosed in square brackets (known as a string index).

Example:
Using the dot operator ( . )
Code:
         local x = myNewEmptyTable.innerEmptyTable.someProperty
Using a string index
Code:
         local x = myNewEmptyTable['innerEmptyTable'].someProperty
           or
         local x = myNewEmptyTable['innerEmptyTable']['someProperty']
           or
         local x = myNewEmptyTable.innerEmptyTable['someProperty']
Tables can also be assigned tables with no property names
Code:
         local tableOfTables = {
           {2, 4, 6}, -- index 1
           {1, 3, 9}, -- index 2
           {5, 8, 7}  -- index 3
         }
Lets say we wanted to access the value of 9 from tableOfTables, we would use its table index value followed by that tables index
Code:
         local x = tableOfTables[2][3] -- this will store the value of 9 in x

Anytime you see text enclosed in double ("") or single ('') quotes this type of data is known as a string, a string can be 1 character like "A" or can be many characters like "Hi my name is Codex :)"

A table can also use a string as a property name in lua but it must be surrounded by single / double quotes and surrounded by square brackets ( [] ).
Example:
Code:
local myStringTable = {
    ['a short sentence with spaces :)'] = 56
}
print(myStringTable['a short sentence with spaces :)']) -- prints 56 to the console

Multiple Declarations:

Tables:
Code:
       local myTable1, myTable2, myTable3 = {}, {}, {}
Variables:
Code:
       local myVariable1, myVariable2, myVariable = 0, '', nil
Mixed:
Code:
       local myVariable1, myTable1, myVariable2, myTable2 = "hi :)", {}, 53, {a = 1}
Scope:
Scope is the life of a variable or table, and how long it exists in memory, more about this as we get into functions :)
Global:
To declare a global we just use the variable or table name
Code:
           myGlobalVariable = 100
           myGlobalTable = {}
Local:
To declare a local we use the keyword local followed by the variable or table name
Code:
           local myLocalVariable = 100
           local myLocalTable = {}


Functions:
Functions in lua and in many other programming languages allow us write generic code, code that can handle all types of values passed to it.
The important thing to remember when writing a function is not to squeeze everything into one, there is nothing wrong with having multiple
functions that perform different tasks even if those tasks are simplistic in nature.

So how do we create a function in lua?
Functions in lua are declared using the keyword function followed by the name of the function, right after the function name you place an open and closed parentheses () and then finally closing the function using the end keyword
The function keyword at the begining tells lua this is the start of the function declaration and the end keywords tells lua this is the end of the function declaration

So lets go ahead and make our 1st function
Code:
   function myFunction()
   end

Our new function isn't all that impressive, because at the moment if we were to call this it wouldn't do anything :p

Arguments, Pass by value, Parameters, Return statements:

The parentheses in our function myFunction serve a purpose, they allow us to pass data to that function so that the function can process it.

Lets expand upon myFunction and give it a purpose
Code:
   local name = "Codex"
   function myFunction(name) -- myFunction declaration
    print("Hi my name is "..name .. " :)")
   end

   myFunction(name) -- call to myFunction

Woah, there is a lot going on in this script here.. we have a variable called name that is assigned the value of Codex with a local keyword in front of it, myFunction has something in between its parentheses also called name, there is this thing called print which also has some text within double quotes and 2 dots followed by name followed by 2 more dots and what looks like a happy face in double quotes.
And then there is myFunction again all by its lonesome with a word called name in its parentheses.

Lets break this code apart, at this stage we know that name is a variable and it has been assigned the value of Codex, but what is this local thing in front of it?

The local keyword is telling lua that this variable will only exist in memory for the duration that the script's execution, because of where it is sitting in the script, more on this later.

Ok, so what is this name thingy in between the parentheses of the myFunction declaration? This is what is known as a parameter its just a place holder it's scope only exist inside myFunction, or we can look at it as an entry point allowing us to send in information to myFunction also known as Pass By Value.

Alright well then what is this print thing, the print function is a pre-defined function that exist in the lua language, this allows us to print data to the console window, any information that is enclosed within the parentheses will be printed.

Ok so what are these 2 dots in between the strings and name, The 2 dots (..) are called the concatenation operator, it's job is pretty simple, it joins or attaches data together.

Return statement:
Sometimes when we call a function we want that function to process information and send us the results.
Lets take a look at our add function
Code:
   local a, b = 5, 7 -- assign the value of 5 to a and the value of 7 to b
   function add(c, d) -- c & d are parameters of add and will hold the values of whatever data is passed to them
     return c + d  -- return the value of c + d
   end
   local x = add(a, b) -- send the values of a and b as arguments to add and store the return value in x
   print(x) -- prints 12 to the console window
Using the example above if we were to attempt to print the values of c & d outside of the add function we would generate an error or the values would return nil
Code:
   local a, b = 5, 7 -- assign the value of 5 to a and the value of 7 to b
   function add(c, d) -- c & d are parameters of add and will hold the values of whatever data is passed to them
     return c + d  -- return the value of c + d
   end
   local x = add(a, b) -- send the values of a and b as arguments to add and store the return value in x
   print(c, d) -- prints nil nil to the console window
This is because of c & d has a scope that is only local to the add function outside of add they don't exist.

You can also return more then 1 value from a function this includes tables.
Code:
local a, b, c = 1, 2, 3

function returnValues(e, f, g)
    local r = { e, f, g }
    return e + f, g + e, r -- return everything
end
local x, z, t = returnValues(a, b, c) -- store the return values in x, z & t

print(x, z, table.concat(t)) -- prints  3   4   123
 
Last edited:
Loops:
There are a few types of loops in lua, also known as control structures,
conditional statements also fall under the category of a control structure we will get into this a bit later.
There is 2 types of for, 1 type of while, 1 type of repeat

Lets take a look at the most commonly used loop or iterator
The for loop
As stated above the for loop has two different versions to it

The Numeric For
Code:
     for variable = start, stop, step do
       print(variable)
     end

Lets break down the numeric for loop and show exactly how it works.

Using the for keyword we initiate the loop, variable can be named anything but its scope only exist inside of the for loop, start is the value we would like to assign to variable so that it can be compared to the stop value per iteration and step is the amount we want to increase per iteration although if your increasing the step by 1 this can be omitted.

That was a mouthful.. lets look at another example of a numeric for loop this time with some values.
Code:
   -- 1st example
     for i = 1, 5 do
       print(i)
     end
     -- prints
     1
     2
     3
     4
     5
   -- 2nd example
     -- using a step value of 2
     for i = 1, 10, 2 do
       print(i)
     end
     -- prints
     1
     3
     5
     7
     9

In the 1st example the numeric for loop prints the value of i 5 times, by omitting the step value the for loop used the default step value of 1 and printed every iteration of i to screen.

In the 2nd example the for loop does pretty much everything as the 1st example did except that we manipulated the i's value based on the step value.
Using 2 as our step value we tricked the for loop into increasing the value of i by 2 rather than 1 per iteration this is why the results of i are 1,3,5,7,9 rather than 1,2,3,4,5,6,7,8,9,10, because we are adding step to i per iteration

Using this formula per iteration for step being 2
Code:
     i = 1
     step = 2
     max = 10
  -- compare i to max
     -- iteration 1
     print(i) -- 1
     i = (i + step)
  -- compare i to max
     -- iteration 2
     print(i) -- 3
     i = (i + step)
  -- compare i to max
     -- iteration 3
     print(i) -- 5
     i = (i + step)
  -- compare i to max
     -- iteration 4
     -- etc...

So how does the numeric for loop know when to stop?
Simple it initially evaluates all 3 expressions, the value of i, the max value to meet and the step value if any

And then compares the value of i to the max value to meet based on the step value.. this sounds confusing I bet

We might need another example to show what I am referring to when i say based on the step value

Code:
   -- 1st example
     for i = 5, 1, -1 do
       print(i)
     end
     -- prints
     5
     4
     3
     2
     1
   -- 2nd example
     for i = 10, 1, -2 do
       print(i)
     end
     -- prints
     10
     8
     6
     4
     2

In the 1st example we are using a negative number (-1) as our step value and assigning i a value of 10 while making the max value to meet 1
Since the step value + i controls the value to increment per iteration we can still use this same formula as we did for a positive step value

Using this formula per iteration for step being -1
Code:
     i = 10
     step = -1
     max = 1
  -- compare i to max
    -- iteration 1
     print(i) -- 10
     i = (i + step)
  -- compare i to max
     -- iteration 2
     print(i) -- 9
     i = (i + step)
  -- compare i to max
     -- iteration 3
     print(i) -- 8
     i = (i + step)
  -- compare i to max
     -- iteration 4
     -- etc...

Using this formula per iteration for step being -2
Code:
     i = 10
     step = -2
     max = 1
  -- compare i to max
    -- iteration 1
     print(i) -- 10
     i = (i + step)
  -- compare i to max
     -- iteration 2
     print(i) -- 8
     i = (i + step)
  -- compare i to max
     -- iteration 3
     print(i) -- 6
     i = (i + step)
  -- compare i to max
     -- iteration 4
     -- etc...

If we add a negative number to a positive number we are essentially subtracting e.g. 10 + (-2) = 8

Generic For Loop
The generic for loop is always used on a table, it is define as
Code:
     for key, value in pairs(table_name) do
  
     end
     -- or
     for key, value in ipairs(table_name) do
  
     end
What a generic for loop does it allows us to easily traverse through a table.
Early on we referred to tables having properties and indexes well the index of a table can be referred to as its key and a property as its value.

Lets break apart the generic for loop and explain how it works.
The generic for loop is initialized using the for keyword just as you did with the numeric for loop, it is then assigned a variable to be used to hold the table's key value this variable can be named anything even an underscore ( _ ) as long as it follows lua's naming convention.
We then place a comma after the key's variable name and create another variable this variable will hold the value of the table's key.
We then place a space and type the in keyword and then another space and use either pairs or ipairs followed by an open parentheses followed by the table name followed by a closing parentheses and then another space followed by the do keyword and the end keyword to close the generic for loop.

The difference between pairs and ipairs is pairs will iterate over everything in the table regardless if it has a numerical key or not in no particular order. Where as ipairs will only iterate through a table if the table has keys that are numerical.

Lets take a look at both pairs & ipairs to see how they are different.

Using pairs
Code:
     local t = {
       ["one"] = 1,
       [2] = 23,
       codex = "my name",
       50
     }

     for k, v in pairs(t) do
       print(k, v)
     end
   -- output
   1   50
   one   1
   2   23
   codex   my name
Using ipairs
Code:
     local t = {
       ["one"] = 1,
       [2] = 23,
       codex = "my name",
       50
     }

     for k, v in ipairs(t) do
       print(k, v)
     end
   -- output
   1   50
   2   23

As you can see from the 2 examples pairs printed everything to the screen where as ipairs only printed non-text based keys
 
Last edited:
Great initiative, I think you should explain a bit more on the tables section, as you did in the last part. Other than that, it looks pretty neat.
A suggestion, you might want to get into some ot-related stuff maybe after explaining everything extensively.
 
Great initiative, I think you should explain a bit more on the tables section, as you did in the last part. Other than that, it looks pretty neat.
A suggestion, you might want to get into some ot-related stuff maybe after explaining everything extensively.
Its a work in progress :p
Thanks for the tips :)
 
Last edited:
I like this, great to see some new nice contributations to this board.
Keep it up (Y) :)
 
@Codex NG
Pretty good tutorial, but I have some suggestions. You should explain value and reference semantics when you're explaining assignment and elaborate on Pass by Value semantics, because the target reader most likely has very little experience with languages that exhibit such behavior (like C++). Your explanation of local is also a little imprecise and might lead to confusion - local binds the lifetime of a variable to the scope of the function it is declared in. A "script" (more commonly known in lua as a "chunk") is an invisible, unnamed function that is executed when the script is loaded. I would also suggest mentioning that functions are first class objects in lua (which is quite useful for things like callbacks).
 
All subject matter of the tutorial are no where near complete including the tutorial itself so I do appreciate the suggestions :)

This is just a side project like you all have your servers, maps or scripts that you work on this is my project, by trying the explain the language to others its helping me better understand the language myself :)
 
This can be good to link to :p
But I dislike that you go for the legacy interface insted of the meta interface.

You should how ever correct things like this;
Code:
function onEquip(cid, item, slot)
    local bonus = (isPremium(cid)) and xpToAdd['premacc'] or xpToAdd['freeacc']
    local rate = getPlayerExpRate(cid, true) + bonus
    if getPlayerStorageValue(cid, xpStorage) == -1 then
        setPlayerStorageValue(cid, xpStorage, 1)
        doPlayerSetExperienceRate(cid, rate)
        doPlayerSendTextMessage(cid, 22, "You have gained "..(bonus * 100).."% bonus exp!")
        if not isPremium(cid) then
            doPlayerSendTextMessage(cid,21,"If you were a premium player you would receive "..(xpToAdd['premacc'] * 100).."% instead of "..(xpToAdd['freeacc'] * 100).."% as a free account while using this ring.")
        end
        doTransformItem(item.uid, 7697, 1)
    end
    return true
end

To this:
Code:
function onEquip(cid, item, slot)
    if getPlayerStorageValue(cid, xpStorage) ~= -1 then
        return true
    end

    local isPremium = isPremium(cid)
    local bonus = isPremium and xpToAdd['premacc'] or xpToAdd['freeacc']
    setPlayerStorageValue(cid, xpStorage, 1)
    doPlayerSetExperienceRate(cid, getPlayerExpRate(cid, true) + bonus)
    doPlayerSendTextMessage(cid, 22, "You have gained " .. (bonus * 100) .. "% bonus exp!")
    doTransformItem(item.uid, 7697, 1)

    if not isPremium then
        doPlayerSendTextMessage(cid,21,"If you were a premium player you would receive " .. (xpToAdd['premacc'] * 100) .. "% instead of " .. (xpToAdd['freeacc'] * 100) .. "% as a free account while using this ring.")
    end
    return true
end

Mainly to make it quicker to load (no reason to load the if statment if we are not gonna use it).
And to also not load "isPremium(cid)" 2 times.

You can also add things like the format function:
Code:
string.format("Hi %s!", player:getName())


// Edit
Forgot, always use the constants insted of the integer it resembles.
Ex.
Code:
doPlayerSendTextMessage(cid, 22
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR,

Just look at when 1.1 got updated to 10.76, alot of the integers got changed (most likely due to cipsoft noticing alot of ppl not using the constants = more work to update).
 
This can be good to link to :p
But I dislike that you go for the legacy interface insted of the meta interface.

You should how ever correct things like this;
Code:
function onEquip(cid, item, slot)
    local bonus = (isPremium(cid)) and xpToAdd['premacc'] or xpToAdd['freeacc']
    local rate = getPlayerExpRate(cid, true) + bonus
    if getPlayerStorageValue(cid, xpStorage) == -1 then
        setPlayerStorageValue(cid, xpStorage, 1)
        doPlayerSetExperienceRate(cid, rate)
        doPlayerSendTextMessage(cid, 22, "You have gained "..(bonus * 100).."% bonus exp!")
        if not isPremium(cid) then
            doPlayerSendTextMessage(cid,21,"If you were a premium player you would receive "..(xpToAdd['premacc'] * 100).."% instead of "..(xpToAdd['freeacc'] * 100).."% as a free account while using this ring.")
        end
        doTransformItem(item.uid, 7697, 1)
    end
    return true
end

To this:
Code:
function onEquip(cid, item, slot)
    if getPlayerStorageValue(cid, xpStorage) ~= -1 then
        return true
    end

    local isPremium = isPremium(cid)
    local bonus = isPremium and xpToAdd['premacc'] or xpToAdd['freeacc']
    setPlayerStorageValue(cid, xpStorage, 1)
    doPlayerSetExperienceRate(cid, getPlayerExpRate(cid, true) + bonus)
    doPlayerSendTextMessage(cid, 22, "You have gained " .. (bonus * 100) .. "% bonus exp!")
    doTransformItem(item.uid, 7697, 1)

    if not isPremium then
        doPlayerSendTextMessage(cid,21,"If you were a premium player you would receive " .. (xpToAdd['premacc'] * 100) .. "% instead of " .. (xpToAdd['freeacc'] * 100) .. "% as a free account while using this ring.")
    end
    return true
end

Mainly to make it quicker to load (no reason to load the if statment if we are not gonna use it).
And to also not load "isPremium(cid)" 2 times.

You can also add things like the format function:
Code:
string.format("Hi %s!", player:getName())


// Edit
Forgot, always use the constants insted of the integer it resembles.
Ex.
Code:
doPlayerSendTextMessage(cid, 22
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR,

Just look at when 1.1 got updated to 10.76, alot of the integers got changed (most likely due to cipsoft noticing alot of ppl not using the constants = more work to update).

That script i added to the tutorial is not from a 1.1 server and I wasn't using the script for the purpose of learning the language, the tfs framework is not the only thing lua is used for.

Plus your method uses too many steps :p
 
That script i added to the tutorial is not from a 1.1 server and I wasn't using the script for the purpose of learning the language, the tfs framework is not the only thing lua is used for.

Plus your method uses too many steps :p

That was my point, to use 1.x insted of 0.x.
The only diffrence we have with "main" Lua as with all other projects - the functions.
If you ask me, if you are gonna do something - do it right. :p

How does it have more steps?
It will be easier to understand then long nested if statments.
The string.format might be a bit to much, but still a good thing to point out to those who want to go a step further.

And as I said about the 2 isPremium executions, this is not just a TFS thing, you should always store things if you are gonna check it more then once and you are sure that the value isen't changed during that phase.
 
That was my point, to use 1.x insted of 0.x.
The only diffrence we have with "main" Lua as with all other projects - the functions.
If you ask me, if you are gonna do something - do it right. :p

How does it have more steps?
It will be easier to understand then long nested if statments.
The string.format might be a bit to much, but still a good thing to point out to those who want to go a step further.

And as I said about the 2 isPremium executions, this is not just a TFS thing, you should always store things if you are gonna check it more then once and you are sure that the value isen't changed during that phase.

The point of this tutorial is to teach the lua language not the latest framework, to start from the basics and work upto the complex aspects of the language now please stop being rude...
 
Last edited:
The point of this tutorial is to teach the lua language not the latest framework, to start from the basics and work upto the complex aspects of the language now please stop being rude...

Im not trying to be rude, im just trying to point out things that you should teach insted.
As I said nothing I said is directly linked to TFS, all Lua interfaces works pretty much the same way.
There is no reason to load a function 2 times.

You can write any tutorial and it could work.
But there is no reason teaching things that they will have to re-learn in a couple of months when they try to write better codes.
Ex. breaking an if statment if we don't need to load it (as I posted above).
 
Im not trying to be rude, im just trying to point out things that you should teach insted.
As I said nothing I said is directly linked to TFS, all Lua interfaces works pretty much the same way.
There is no reason to load a function 2 times.

You can write any tutorial and it could work.
But there is no reason teaching things that they will have to re-learn in a couple of months when they try to write better codes.
Ex. breaking an if statment if we don't need to load it (as I posted above).

Apparently you have a tough time understanding what I said in my previous post so I will put it in bold for you.
The point of this tutorial is to teach the lua language and not the latest tfs framework, to start with basic concepts and then eventually move on to more complex aspects of the language.

This is why the title of the tutorial is called Lua Broken Down and not The TFS Framework Broken Down.

If people don't understand the core language then how do you expect people to write efficient scripts in any framework?

Edit:
I'll assume by your attitude towards this thread you have never read 1 book on any programming language.
Similar to mathematics when a language is taught you are always shown the long way to do things 1st and then you are shown the short-cuts

Math Example:
Code:
2 x 2 x 2 x 2 x 2 = 10 -- long way
2 x 5 = 10 -- short way

Anyone can read a tutorial or book on any language but it doesn't guarantee your going to understand the concepts until you test what you have learned.

So that is the point here is to introduce the basic concepts of the language while breaking it apart step by step and using what we just learned in conjunction with what we already know.
 
Last edited:
@WibbenZ isn't saying you need to teach "the latest TFS framework", he's saying your code is inefficient.

Everything I have discussed in this tutorial up to the present is essential to know in order to write efficient scripts.

Now if you have nothing to positive to add please leave this thread and go write your own.
 
Back
Top