• 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 Help converting into meta-table

E

Evil Puncker

Guest
Using tfs 1.3, can you guys please help me converting those 3 scripts into its meta-table counterpart? thanks:

Lua:
if getContainerSize(getPlayerSlotItem(player, CONST_SLOT_BACKPACK).uid) then
            for i = 0, getContainerSize(getPlayerSlotItem(player, CONST_SLOT_BACKPACK).uid) do
                local thing = getContainerItem(getPlayerSlotItem(player, CONST_SLOT_BACKPACK).uid, i)
                container[i] = getContainerItem(getPlayerSlotItem(player, CONST_SLOT_BACKPACK).uid, i)
                if isContainer(container[i].uid) then
                    if not table.contains(names, container[i].itemId) then
                        table.insert(names, container[i].itemId)
                        window:addChoice(sum, ""..string.gsub(" "..string.lower(ItemType(container[i].itemid):getName()), "%W%l", string.upper):sub(2).."")
                    end
                    sum = sum + 1
                end
            end
            else
            player:sendCancelMessage("Not find main backpack.")
            return false
        end

Lua:
            local container, bp, sequencer = {}, {}, 1
            local bp2 = {}
            for i = 0, getContainerSize(getPlayerSlotItem(player, CONST_SLOT_BACKPACK).uid) do
                local thing = getContainerItem(getPlayerSlotItem(player, CONST_SLOT_BACKPACK).uid, i)
                container[i] = getContainerItem(getPlayerSlotItem(player, CONST_SLOT_BACKPACK).uid, i)
                if isContainer(container[i].uid) then
                    itemtester = Item(container[i].uid)
                    bp[sequencer] = itemtester
                    bp2[sequencer] = itemtester:getId()
                    sequencer = sequencer + 1
                end
            end

Lua:
            local container, bp, sequencer = {}, {}, 1
            local bp2 = {}
            for i = 0, getContainerSize(getPlayerSlotItem(player, CONST_SLOT_BACKPACK).uid) do
                local thing = getContainerItem(getPlayerSlotItem(player, CONST_SLOT_BACKPACK).uid, i)
                container[i] = getContainerItem(getPlayerSlotItem(player, CONST_SLOT_BACKPACK).uid, i)
                if isContainer(container[i].uid) then
                    itemtester = Item(container[i].uid)
                    bp[sequencer] = itemtester
                    bp2[sequencer] = itemtester:getId()
                    sequencer = sequencer + 1
                end
            end

I tried doing it by using the compat.lua file but my head just went boom lol (full script in case its matter)

and second question: is it safe to change db.query into db.storeQuery? because I saw nothing related to it into the compat.lua
 
Solution
getContainerItem(uid, index) -> container:getItem(index)
getPlayerSlotItem(uid, slot) -> player:getSlotItem(slot)
getContainerSize(uid) -> container:getSize()

if isContainer(uid) then -> if container then

Make sure you create a variable for conatiner (should be player:getSlotItem(CONST_SLOT_BACKPACK)) and check if it exists before using it.

You should be using db.query for queries that don't generate any results, and storeQuery for things that do such as SELECT. Swapping from query to storeQuery won't cause problems but the other way around will, that's why it's important to know the difference between the two.
getContainerItem(uid, index) -> container:getItem(index)
getPlayerSlotItem(uid, slot) -> player:getSlotItem(slot)
getContainerSize(uid) -> container:getSize()

if isContainer(uid) then -> if container then

Make sure you create a variable for conatiner (should be player:getSlotItem(CONST_SLOT_BACKPACK)) and check if it exists before using it.

You should be using db.query for queries that don't generate any results, and storeQuery for things that do such as SELECT. Swapping from query to storeQuery won't cause problems but the other way around will, that's why it's important to know the difference between the two.
 
Solution
getContainerItem(uid, index) -> container:getItem(index)
getPlayerSlotItem(uid, slot) -> player:getSlotItem(slot)
getContainerSize(uid) -> container:getSize()

if isContainer(uid) then -> if container then

Make sure you create a variable for conatiner (should be player:getSlotItem(CONST_SLOT_BACKPACK)) and check if it exists before using it.

You should be using db.query for queries that don't generate any results, and storeQuery for things that do such as SELECT. Swapping from query to storeQuery won't cause problems but the other way around will, that's why it's important to know the difference between the two.

Lua:
            local container, bp, sequencer = {}, {}, 1
            local container2 = player:getSlotItem(CONST_SLOT_BACKPACK)
            local bp2 = {}
            for i = 0, container2:getSize() do
                local thing = container2:getItem(CONST_SLOT_BACKPACK)
                container[i] = container2:getItem(CONST_SLOT_BACKPACK)
                if container2:isContainer(container[i].uid) then
                    itemtester = Item(container[i].uid)
                    bp[sequencer] = itemtester
                    bp2[sequencer] = itemtester:getId()
                    sequencer = sequencer + 1
                end
            end

am I doing it right? 🤔
 
container:getItem takes an index parameter, not a slot type. You should be passing i as an argument since you're iterating the container (the loop should also be 0, container2:getSize() - 1) and you shouldn't be using container2:isContainer, it should just be container[i]:isContainer(), and the entire itemtester variable is absolutely useless and unnecessary, replace all references to itemtester to container[i].
 
container:getItem takes an index parameter, not a slot type. You should be passing i as an argument since you're iterating the container (the loop should also be 0, container2:getSize() - 1) and you shouldn't be using container2:isContainer, it should just be container[i]:isContainer(), and the entire itemtester variable is absolutely useless and unnecessary, replace all references to itemtester to container[i].

thanks, final codes (gonna test then now):

first one:
Lua:
        local container2 = player:getSlotItem(CONST_SLOT_BACKPACK)
        local window = ModalWindow(modalcode, title, message)
        local sum = 1
        local container, names = {}, {}
        if container2:getSize() then
            for i = 0, container2:getSize() - 1 do
                local thing = container:getItem(i)
                container[i] = container:getItem(i)
                if container[i] then
                    if not table.contains(names, container[i].itemId) then
                        table.insert(names, container[i].itemId)
                        window:addChoice(sum, ""..string.gsub(" "..string.lower(ItemType(container[i].itemid):getName()), "%W%l", string.upper):sub(2).."")
                    end
                    sum = sum + 1
                end
            end

second one (same as third one):
Lua:
            local container, bp, sequencer = {}, {}, 1
            local container2 = player:getSlotItem(CONST_SLOT_BACKPACK)
            local bp2 = {}
            for i = 0, container2:getSize() - 1 do
                local thing = container2:getItem(i)
                container[i] = container2:getItem(i)
                if container[i]:isContainer() then
                    container[i] = Item(container[i].uid)
                    bp[sequencer] = container[i]
                    bp2[sequencer] = container[i]:getId()
                    sequencer = sequencer + 1
                end
            end
 
thanks, final codes (gonna test then now):

first one:
Lua:
        local container2 = player:getSlotItem(CONST_SLOT_BACKPACK)
        local window = ModalWindow(modalcode, title, message)
        local sum = 1
        local container, names = {}, {}
        if container2:getSize() then
            for i = 0, container2:getSize() - 1 do
                local thing = container:getItem(i)
                container[i] = container:getItem(i)
                if container[i] then
                    if not table.contains(names, container[i].itemId) then
                        table.insert(names, container[i].itemId)
                        window:addChoice(sum, ""..string.gsub(" "..string.lower(ItemType(container[i].itemid):getName()), "%W%l", string.upper):sub(2).."")
                    end
                    sum = sum + 1
                end
            end

second one (same as third one):
Lua:
            local container, bp, sequencer = {}, {}, 1
            local container2 = player:getSlotItem(CONST_SLOT_BACKPACK)
            local bp2 = {}
            for i = 0, container2:getSize() - 1 do
                local thing = container2:getItem(i)
                container[i] = container2:getItem(i)
                if container[i]:isContainer() then
                    container[i] = Item(container[i].uid)
                    bp[sequencer] = container[i]
                    bp2[sequencer] = container[i]:getId()
                    sequencer = sequencer + 1
                end
            end
I see no more errors, but you should be using thing to reference container and container:getItem(i) since you've already evaluated that value, there's no need to re-call the value or re-index the container table for that object, you already have a reference to it (stored in thing var).
 
I see no more errors, but you should be using thing to reference container and container:getItem(i) since you've already evaluated that value, there's no need to re-call the value or re-index the container table for that object, you already have a reference to it (stored in thing var).
indeed everything is working fine except the part to choose a backpack (the first code), it is not listing only container items, and when I add:

if container:isContainer then

instead of:

if container then

it gives me an error and I don't know why:

autoloot.lua:80: function arguments expected near 'then'


2019-11-04 20_50_45-Window.png
 
Back
Top