• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Some Array/Table Help

Jordan_August

New Member
Joined
Jul 24, 2012
Messages
122
Reaction score
4
I am trying to get a script that "merges" 3 items into 1 as an upgrade area. Now i have a way that works and if continued my way it will be like 99999999999999x lines of code which i know could be solved using arrays and a possible table. i have two arrays with 11 items in each.

array1 = {1234, 1234, 1234, ....}
array2 = {1234, 1234, 1234, ....}

and i place three of array1[1] on coal basins and and i need it to turn into array2[1] and so on and so fourth. all the way down. array1[2] will turn into array2[2]. if anyone could help me understand how to use the arrays and such would be greatly appreciated. Thanks
 
I don't understand what you aim to do. Could you post your script so far or give us a more detailed description of your idea?

Ignazio
 
thats the thing.. ireally dont have a script for it yet. Okay, i have this script.
Code:
local itemPos1 = {x=1172, y=868, z=7, stackpos=1} -- first item position on coal basin
local itemPos2 = {x=1173, y=868, z=7, stackpos=1} -- second item position on coal basin
local itemPos3 = {x=1174, y=868, z=7, stackpos=1} -- third item position on coal basin
local tier1EQ = {5926, 6096, 6095, 5918, 5462, 10123, 2169, 2385, 2380, 2394, 2184, 5947} -- Backpack, Helmet, Armor, Legs, Boots, Shield, Ring, Sword, Axe, Club, Wand, Bow
local tier2EQ = {7342, 7458, 7463, 7464, 7457, 7460, 7967, 2450, 7413, 7381, 7379, 3965} -- Backpack, Helmet, Armor, Legs, Boots, Shield, Ring, Sword, Axe, Club, Wand, Bow

-- Tier 1 Upgrades to  Tier 2 --
function onUse(cid, item, fromPosition, itemEx, toPosition)
local createtier2BP = tier2EQ[1]
local reqtier1BP = tier1EQ[1]
local get1 = getTileItemById(itemPos1, reqtier1BP)
local get2 = getTileItemById(itemPos2, reqtier1BP)
local get3 = getTileItemById(itemPos3, reqtier1BP)
    if get1.uid and get2.uid and get3.uid > 0 then
        doSendMagicEffect(itemPos1, 15)
        doSendMagicEffect(itemPos2, 15)
        doSendMagicEffect(itemPos3, 15)
        doRemoveItem(get1.uid, 1)
        doRemoveItem(get2.uid, 1)
        doRemoveItem(get3.uid, 1)
        doBroadcastMessage(''.. getPlayerName(cid) ..' has successfully upgraded their equipment!', MESSAGE_STATUS_CONSOLE_RED)
        doPlayerAddItem(cid, createtier2BP, 1)
        doTransformItem(item.uid, 1946)
    else
        doCreatureSay(cid, "Merging Unsuccessful!", TALKTYPE_ORANGE_1)
end
return true
end

and instead of copying. pasting this entire thing over and over again. and renaming the variables. i need it so that. i can add more arrays. but also i need it so the arrays will make the item in the next array corresponding with it. so 3 of tier1EQ[1] will "merge" into 1 of tier2EQ[1] and so on and so fourth.
 
Okay, just so I understand it correctly; Do you put three of the same item on these coal basins from tier 1, to get one item from tier 2?

So if I would put three items with id 5947 (last of tier1eq array), I'd get one item with id 3965?

Ignazio
 
What you want to do then are loops, for loops. You should study them, since they are very useful.

You can easily loop through an entire array by typing something like this:

Code:
for key,value in pairs(tier1EQ) do
    get1 = getTileItemById(itemPos1, value)
end

It will then get the tile item id of each entry in that array (tier1EQ) and place it within the get1 variable.

I wrote something quickly that might work

Code:
local itemPos1 = {x=1172, y=868, z=7, stackpos=1} -- first item position on coal basin
local itemPos2 = {x=1173, y=868, z=7, stackpos=1} -- second item position on coal basin
local itemPos3 = {x=1174, y=868, z=7, stackpos=1} -- third item position on coal basin
local tier1EQ = {5926, 6096, 6095, 5918, 5462, 10123, 2169, 2385, 2380, 2394, 2184, 5947} -- Backpack, Helmet, Armor, Legs, Boots, Shield, Ring, Sword, Axe, Club, Wand, Bow
local tier2EQ = {7342, 7458, 7463, 7464, 7457, 7460, 7967, 2450, 7413, 7381, 7379, 3965} -- Backpack, Helmet, Armor, Legs, Boots, Shield, Ring, Sword, Axe, Club, Wand, Bow

-- Tier 1 Upgrades to  Tier 2 --
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local createtier2BP = tier2EQ[1]
    local reqtier1BP = tier1EQ[1]

    for key,value in pairs(tier1EQ) do
        local get1 = getTileItemById(itemPos1, value)
        local get2 = getTileItemById(itemPos2, value)
        local get3 = getTileItemById(itemPos3, value)

        if get1.uid and get2.uid and get3.uid > 0 then
            doSendMagicEffect(itemPos1, 15)
            doSendMagicEffect(itemPos2, 15)
            doSendMagicEffect(itemPos3, 15)
            doRemoveItem(get1.uid, 1)
            doRemoveItem(get2.uid, 1)
            doRemoveItem(get3.uid, 1)
            doBroadcastMessage(''.. getPlayerName(cid) ..' has successfully upgraded their equipment!', MESSAGE_STATUS_CONSOLE_RED)
            doPlayerAddItem(cid, tier2EQ[key], 1)
            doTransformItem(item.uid, 1946)
            break
        else
            doCreatureSay(cid, "Merging Unsuccessful!", TALKTYPE_ORANGE_1)
        end
    end
    return true
end

You can use the key and value variables defined in the for loop for each item in the array,
each item in an array corresponds to a key, if you didn't define a key like in this case, they are automatically assigned as numeric keys (1,2,3) and can be used to identify values from other arrays, but I recommend specifying keys when using different arrays, since LUA randomizes the order in arrays.

You can try something like this, to build arrays

Code:
local tierEq = {
    {
        [1] = 5926,
        [2] = 7342
    },
    {
        [1] = 6096,
        [2] = 7458
    },
        [1] = 6095,
        [2] = 7463
    }
}

-- Tier 1 Upgrades to  Tier 2 --
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local createtier2BP = tier2EQ[1]
    local reqtier1BP = tier1EQ[1]

    for _,val in pairs(tier1EQ) do
        local get1 = getTileItemById(itemPos1, val[1])
        local get2 = getTileItemById(itemPos2, val[1])
        local get3 = getTileItemById(itemPos3, val[1])

        if get1.uid and get2.uid and get3.uid > 0 then
            doSendMagicEffect(itemPos1, 15)
            doSendMagicEffect(itemPos2, 15)
            doSendMagicEffect(itemPos3, 15)
            doRemoveItem(get1.uid, 1)
            doRemoveItem(get2.uid, 1)
            doRemoveItem(get3.uid, 1)
            doBroadcastMessage(''.. getPlayerName(cid) ..' has successfully upgraded their equipment!', MESSAGE_STATUS_CONSOLE_RED)
            doPlayerAddItem(cid, val[2], 1)
            doTransformItem(item.uid, 1946)
            break
        else
            doCreatureSay(cid, "Merging Unsuccessful!", TALKTYPE_ORANGE_1)
        end
    end
    return true
end

This way you don't need the key, only the value, and specify the transformation in the same multidimensional child value in the tierEq array.

I'm sorry if it was unclear, or contained any mistakes, you can read more about loops here; http://www.lua.org/pil/4.3.4.html

Ignazio
 
Back
Top