• 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 How to find the position information I want in an array?

GOD Half

Member
Joined
May 9, 2010
Messages
179
Reaction score
15
I need to find the information based on the position of each array.
Lua:
local arrayVoc = {
   [1] = {1, 2, 3, 4, 5},
   [2] = {6, 7, 8, 9, 10},
   [3] = {11, 12, 13, 14, 15},
   [4] = {16, 17, 18, 19, 20},
   [5] = {21, 22, 23, 24, 25},
   [6] = {26, 27, 28, 29, 30}
}

Currently the search system searches based on the vocation of the player. But that's not enough that I need.
Lua:
if isInArray(arrayVoc[1], player:getVocation():getId()) then
  ...
end

For example: I need to get the information that is in [2] and position 3. Would the result be 8 or 9? If it starts with 0 it would be 9, but if it starts with 1 then it is 8.

How do I build the formula to find the value of the position I want?

Thank's
 
Array [1][2] will give you the first array, third positiin. Arrays are zerobased, so first position is 0.
 
Array [1][2] will give you the first array, third positiin. Arrays are zerobased, so first position is 0.
no they are not, arrays in lua begin at 1
if they were your statement should have been array[0][2], array[1] would be 2nd array if lua started indexing at 0 instead of 1

I need to find the information based on the position of each array.
Lua:
local arrayVoc = {
   [1] = {1, 2, 3, 4, 5},
   [2] = {6, 7, 8, 9, 10},
   [3] = {11, 12, 13, 14, 15},
   [4] = {16, 17, 18, 19, 20},
   [5] = {21, 22, 23, 24, 25},
   [6] = {26, 27, 28, 29, 30}
}

Currently the search system searches based on the vocation of the player. But that's not enough that I need.
Lua:
if isInArray(arrayVoc[1], player:getVocation():getId()) then
  ...
end

For example: I need to get the information that is in [2] and position 3. Would the result be 8 or 9? If it starts with 0 it would be 9, but if it starts with 1 then it is 8.

How do I build the formula to find the value of the position I want?

Thank's
i don't really understand what exactly you want but i threw this together to make it check for ids in each table instead of just 1
Lua:
local arrayVoc = {
   [1] = {1, 2, 3, 4, 5},
   [2] = {6, 7, 8, 9, 10},
   [3] = {11, 12, 13, 14, 15},
   [4] = {16, 17, 18, 19, 20},
   [5] = {21, 22, 23, 24, 25},
   [6] = {26, 27, 28, 29, 30}
}

isInArray = function(t, v, deep)
    for k, val in pairs(t) do
        if type(val) == 'table' and deep then
            return isInArray(t, val, true)
        else
            if val == v then
                return true
            end
        end
    end
    return false
end

print(isInArray(arrayVoc, player:getVocation():getId(), true))
 
no they are not, arrays in lua begin at 1
if they were your statement should have been array[0][2], array[1] would be 2nd array if lua started indexing at 0 instead of 1
Mhm, to be honest, i though they started by Zero. But you are right. And about my statement, it wasn't wrong because in case he declare an array as he is doing, he can access directly by the key, which is "1" for the first array. Thats why i did it that way. :p
 
no they are not, arrays in lua begin at 1
if they were your statement should have been array[0][2], array[1] would be 2nd array if lua started indexing at 0 instead of 1


i don't really understand what exactly you want but i threw this together to make it check for ids in each table instead of just 1
Lua:
local arrayVoc = {
   [1] = {1, 2, 3, 4, 5},
   [2] = {6, 7, 8, 9, 10},
   [3] = {11, 12, 13, 14, 15},
   [4] = {16, 17, 18, 19, 20},
   [5] = {21, 22, 23, 24, 25},
   [6] = {26, 27, 28, 29, 30}
}

isInArray = function(t, v, deep)
    for k, val in pairs(t) do
        if type(val) == 'table' and deep then
            return isInArray(t, val, true)
        else
            if val == v then
                return true
            end
        end
    end
    return false
end

print(isInArray(arrayVoc, player:getVocation():getId(), true))

Thank you. But can you help me build this code below? He's making a mistake, and I think you can understand what I've tried to do.

EDIT.: I find a error, I'll show again, cheking...
EDIT 2: Here is the revised code , but it has an error.

Lua:
for _, v in pairs(arrayVoc) do
    if type(v) == 'table' then
        if isInArray(v, player:getVocation():getId()) then
            player:setVocation(arrayVoc[v][1])
        end
    end
end
 
Last edited:
Thank you. But can you help me build this code below? He's making a mistake, and I think you can understand what I've tried to do.

EDIT.: I find a error, I'll show again, cheking...
EDIT 2: Here is the revised code , but it has an error.

Lua:
for _, v in pairs(arrayVoc) do
    if type(v) == 'table' then
        if isInArray(v, player:getVocation():getId()) then
            player:setVocation(arrayVoc[v][1])
        end
    end
end
If you have an error you have to supply the error not just say it has an error, most especially since its not a complete script.
 
If you have an error you have to supply the error not just say it has an error, most especially since its not a complete script.

Sorry sir. Error in line 41 when addOutfit.
Lua:
function onAdvance(player, skill, oldlevel, newlevel)
    -- OUTFITS --
    local OUTFIT0001  = 905 -- 01
    local OUTFIT0002  = 129 -- 02

    local arrayOutfit = {
    [1] = {OUTFIT0001, OUTFIT0002}
    }

    local arrayVocation = {
    [1] = {1, 2}
    }

    local L01           = 10

    -- Storage_Evolution
    local storage00000  = 70000
    local storage00001  = 70001

    local outfit = player:getOutfit()

    for _, o in pairs(arrayOutfit) do
        if type(o) == 'table' then
            if isInArray(o, outfit.lookType) and (skill == SKILL_LEVEL) then
                for _, v in pairs(arrayVocation) do
                    if type(v) == 'table' then
                        if isInArray(v, player:getVocation():getId()) then
                            if (player:getStorageValue(storage00000) ~= 1) and (player:getLevel() < L01) then
                                player:setStorageValue(storage00000, 1)
                                player:setStorageValue(storage00001, 0)
                                player:addOutfit(arrayOutfit[o][1])
                                player:removeOutfit(arrayOutfit[o][2])
                                outfit.lookType = arrayOutfit[o][1]
                                player:setOutfit(outfit)
                                player:setVocation(arrayVocation[v][1])
                            end
                            if (player:getStorageValue(storage00001) ~= 1) and (player:getLevel() >= L01) then
                                player:setStorageValue(storage00000, 0)
                                player:setStorageValue(storage00001, 1)
                                player:addOutfit(arrayOutfit[o][2])
                                outfit.lookType = arrayOutfit[o][2]
                                player:setOutfit(outfit)
                                player:setVocation(arrayVocation[v][2])
                            end
                        end
                    end
                end
            end
        end
    end
    return true
end

Error:
C++:
Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/outfitVoc_onAdvance.lua:onAdvance
data/creaturescripts/scripts/outfitVoc_onAdvance.lua:41: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/creaturescripts/scripts/outfitVoc_onAdvance.lua:41: in function <data/creaturescripts/scripts/outfitVoc_onAdvance.lua:1>
        [C]: in function 'addExperience'
        data/actions/scripts/other/itemlevel.lua:7: in function <data/actions/scripts/other/itemlevel.lua:6>
 
Last edited:
Back
Top