• 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!

Why Learning to code is fun and useful :)

Codex NG

Recurrent Flamer
Joined
Jul 24, 2015
Messages
2,994
Solutions
12
Reaction score
1,657
The goal of this thread is to show you that learning the core language lua is important which allows you to write additional code which can make your life easier as a developer.​

Range Chance - Function
Code:
function rangeChance(n, m)
    local t = {}
    for x = 1, m do
        t[x] = {}
        if x == 1 then
            t[x][1] = x
            t[x][2] = n
        else
            t[x][1] = x * n - n
            t[x][2] = (x * n)
        end
    end
    return t
end

It could also be written like this
Code:
function rangeChance(n, m)
    local t = {}
    for x = 1, m do
        t[x] = {}
        t[x][1] = x == 1 and x or x * n - n
        t[x][2] = x == 1 and n or (x * n)
    end
    return t
end

So what does this do?
I am glad you asked!

It returns a table of both min and max values

For instances lets say you wanted to generate a series of conditions like if else etc to give someone an item or buff
Code:
local range = 10
local amount = 7
local x = rangeChance(range, amount)
If we run this through a for loop
Code:
for k, v in pairs(x) do
    print(v[1], v[2])
end
We get this as a result
Code:
1    10
10    20
20    30
30    40
40    50
50    60
60    70

Its a simple way to speed up the long drawn out range values.
 
Last edited:
Create Item By Name
tfs 1.2, can be placed in player.lua
Code:
-- player:createItemByName(name[, amount, pos])
function Player:createItemByName(name, amount, pos)
    local itemid = ItemType(name):getId()
    if itemid == 0 then
        return false
    end
    return Game.createItem(itemid, amount or 1, pos or self:getPosition())
end
 
Last edited:
Return an Associative Array as a String
Long name I know :p
This function returns a string representation of all the index values of a table (not nested), the 2nd parameter is optional.
Code:
-- returnAssociativeArrayAsString(associativeArray[, delimiter])
function returnAssociativeArrayAsString(associativeArray, delimiter)
    local table_ = {}
    for index, _ in pairs(associativeArray) do
        table.insert(table_, delimiter and index .. delimiter or index )
    end
    string_ = table.concat(table_)
    return delimiter and string_:sub(1, #string_ - #delimiter) or string_
end

What is an associative array?
An associative array is an array that can be indexed not only with numbers, but also with strings or any other value of the language, except nil.

A delimiter is a sequence of one or more characters used to specify the boundary between the words, an example of a delimiter is the comma character ( , ).

Example:
Code:
local assocArrayExample = {
    ['troll'] = {},
    5,
    33,
    nine = 9
}
print( returnAssociativeArrayAsString(assocArrayExample, ', ') )
-- output
-- 1, 2, troll, nine

You are probably wondering why 5 or 33 were not printed, this is because 5 and 33 are not index values.

However if we wanted to see what index values were for 1 & 2 we could do this.
Code:
print(assocArrayExample[1], assocArrayExample[2])
-- output
-- 5 -- index 1
-- 33 -- index 2
 
Last edited:
Get Table Index or Content
This function returns the table of a table that has an index value which is a table,
or it will return the content of the table if the 3rd argument is set to true.
Code:
-- getTableArrayIndexOrContent(table_, searchFor[, returnContent])
function getTableArrayIndexOrContent(table_, searchFor, returnContent)
    for array, content in pairs(table_) do
        if type(array) == 'table' then
            if isInArray(array, searchFor) then
                return returnContent and content or array
            end
        end
    end
    return {}
end

Example:
Code:
local t = {
    [{123, 456, 789}] = {text = 'this is coming from the 1st index'},
    [{43, 45, 89}] = {text = 'this is coming from the 2nd index'}
}

With the 3rd argument being omitted.
Code:
local num = getTableArrayIndexOrContent(t, 45)

If we were to unpack num we would see this as the result
Code:
print( unpack(num) )
-- 43    45    89

Which we could then reference as an index value of t
Code:
print( t[num].text )
-- this is coming from the 2nd index

Or we could skip all the bs and just set the 3rd parameter to true and store the contents of the
table's table index inside of num and reference the properties directly
Code:
local num = getTableArrayIndexOrContent(t, 123, true)
print( num.text )
-- this is coming from the 1st index
 
Game.createItem already accepts item name as a parameter
 
Create Item By Name
tfs 1.2, can be placed in player.lua
Code:
-- player:createItemByName(name[, amount, pos])
function Player:createItemByName(name, amount, pos)
    local itemid = ItemType(name):getId()
    if itemid == 0 then
        return false
    end
    return Game.createItem(itemid, amount or 1, pos or self:getPosition())
end
Doesn't it return nil if ItemType(name) does not exist?
 
Now i sent you guys 1 for once :p
iab.png
 
Back
Top