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

Lua TFS 0.3.6 pass all array variables as a one param through doRemoveItem TFS function.

waqmaz

Member
Joined
Jun 17, 2015
Messages
203
Reaction score
11
Hello. I've been coding since a few days in lua. I've wrote simple functions:
Code:
local itemsID = {
    [1] = { 2471, 2130},
    -- [2] = { 5456, 777, 1148},
}

local quantity = {
    [1] = { 1, 2},
    -- [2] = { 100, 50, 2},
}

local function getItem(nr)
    for i = 1, #itemsID[nr] do
        return itemsID[nr][i]
    end
end

local function getQuantity(nr)
    for i = 1, #quantity[nr] do
        return quantity[nr][i]
    end
end

local function removeThat(get_items, get_quantity, cid)

    if (getPlayerItemCount(cid, get_items) >= get_quantity) then
        doPlayerRemoveItem(cid, get_items, get_quantity)
    end

    return true
end


function onUse(cid, item, fromPosition, itemEx, toPosition)

    if item.uid == 4767 then
        removeThat(getItem(1), getQuantity(1), cid)
    end

    return true
end

What I want to get is to remove all variables from itemsID array where [1] and their quantity from quantity array where [1], using doPlayerRemoveItem(cid, itemid, count[, subtype]) function from TFS 0.3.6.
So player should have removed 1 * item 2471 and 2 * item 2130.
Something like this:
Code:
doPlayerRemoveItem(cid, {2471, 2130}, {1,2})
Can someone help me? Maybe I should do it another way? Thanks.
 
Last edited:
try print(getCreatureName(target), t[getCreatureName(target)], #t[getCreatureName(target)])
Code:
function onKill(cid, target)

    if t[getCreatureName(target)] then
        for i = 1, #t[getCreatureName(target)] do
           -- print(t[getCreatureName(target)], #t[getCreatureName(target)])
        end
    end
  
    print(t[getCreatureName(target)], #t[getCreatureName(target)])
 
    return true
end

Outside the for loop:
Orc returns 2.
Troll returns 3.
 
ill check it out myself after my dota game is done, be back in 30ish
I will be glad, it is really important for me, because our game bases on object oriented programming.
Here in Europe it's 22:30. I will be back at 20:00 tomorrow. See you, friend.
 
Code:
    ['orc'] = {
        {pos = {x = 950,  y = 1063,  z = 7},  id = 1037},
        {pos = {x = 951,  y = 1063,  z = 7},  id = 1037}
    },
    ['troll'] = {
        {pos = {x = 953,  y = 1070,  z = 7},  id = 5154},
        {pos = {x = 953,  y = 1071,  z = 7},  id = 1036},
        {pos = {x = 953,  y = 1072,  z = 7},  id = 5154}
    }
}

function onKill(cid, target, damage, flags)
    local name = getCreatureName(target):lower()
    local tmp = t[name]
    if tmp then
        for i=1,#tmp,1 do
            print_r(tmp[i].pos)
        end
    end
    return true
end
works fine for me o.o

also
Code:
local t = {
    ['Orc'] = {
        {pos = {x = 950,  y = 1063,  z = 7},  id = 1037},
        {pos = {x = 951,  y = 1063,  z = 7},  id = 1037}
    },
    ['troll'] = {
        {pos = {x = 953,  y = 1070,  z = 7},  id = 5154},
        {pos = {x = 953,  y = 1071,  z = 7},  id = 1036},
        {pos = {x = 953,  y = 1072,  z = 7},  id = 5154}
    }
}

function onKill(cid, target, damage, flags)
    if t[getCreatureName(target)] then
        for i=1, #t[getCreatureName(target)],1 do
           print_r(t[getCreatureName(target)][i].pos)
        end
    end
end
works just fine for me killing an orc o.o

EDIT: i think your problem was you are trying to print a position, which is a table like {x = 1, y = 1, z = 1}, and in 0.x TFS printing a table will just print a blank line, in 1.x printing a table will say table 0xAAAAAAA (hex number)
for printing tables in tfs console you should use print_r function
Code:
function print_r ( t ) 
    local print_r_cache={}
    local function sub_print_r(t,indent)
        if (print_r_cache[tostring(t)]) then
            print(indent.."*"..tostring(t))
        else
            print_r_cache[tostring(t)]=true
            if (type(t)=="table") then
                for pos,val in pairs(t) do
                    if (type(val)=="table") then
                        print(indent.."["..pos.."] => "..tostring(t).." {")
                        sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
                        print(indent..string.rep(" ",string.len(pos)+6).."}")
                    else
                        print(indent.."["..pos.."] => "..tostring(val))
                    end
                end
            else
                print(indent..tostring(t))
            end
        end
    end
    sub_print_r(t,"  ")
end
 
Last edited:
I use TFS 0.3.6 CryingDamson for Tibia version 8.6. It returns null:
[23/06/2016 22:41:25] [Error - CreatureScript Interface]
[23/06/2016 22:41:25] data/creaturescripts/scripts/Boss System/bossTimeBlock.lua:eek:nKill
[23/06/2016 22:41:25] Description:
[23/06/2016 22:41:25] ...ata/creaturescripts/scripts/Boss System/bossTimeBlock.lua:18: attempt to call global 'print_r' (a nil value)
[23/06/2016 22:41:25] stack traceback:
[23/06/2016 22:41:25] ...ata/creaturescripts/scripts/Boss System/bossTimeBlock.lua:18: in function <...ata/creaturescripts/scripts/Boss System/bossTimeBlock.lua:13>
Code:
local t =  {
    ['orc'] = {
        {pos = {x = 950,  y = 1063,  z = 7},  id = 1037},
        {pos = {x = 951,  y = 1063,  z = 7},  id = 1037}
    },
    ['troll'] = {
        {pos = {x = 953,  y = 1070,  z = 7},  id = 5154},
        {pos = {x = 953,  y = 1071,  z = 7},  id = 1036},
        {pos = {x = 953,  y = 1072,  z = 7},  id = 5154}
    }
}

function onKill(cid, target, damage, flags)
    local name = getCreatureName(target):lower()
    local tmp = t[name]
    if tmp then
        for i=1,#tmp,1 do
            print_r(tmp[i].pos)
        end
    end
    return true
end
 
It works, lol. My main language is PHP, there is inbuild print_r function. Completely forgot that it has to be done in lua too.
I can say only one thing: THANK YOU SO MUCH. YOU HAVE SAVED ME A FEW DAYS OF WORK FOR SURE! :)
 
Back
Top