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

TFS 0.3.X -- get item count from position

Xikini

I whore myself out for likes
Senator
Premium User
Joined
Nov 17, 2010
Messages
6,756
Solutions
578
Reaction score
5,305
Add into lib -> 050-function, or into specific scripts using 'local function bah blah'
Code:
function getItemCountFromPosition(item, pos)
    local item_pos = {x = pos.x, y = pos.y, z = pos.z}
    local item_count = 0
    for i = 1, 255 do
        local check_pos = {x = item_pos.x, y = item_pos.y, z = item_pos.z, stackpos = i}
        if getThingFromPos(check_pos).itemid <= 0 then
            break
        elseif getThingFromPos(check_pos).itemid == item then
            if isItemStackable(getThingFromPos(check_pos).itemid) == true then
                item_count = item_count + getThingFromPos(check_pos).type
            else
                item_count = item_count + 1
            end
        end
    end
    return item_count
end
Simple example to show functionality
Code:
local item_id = 2544 -- standard arrow
local item_pos = {x = 1000, y = 1000, z = 7}

if getItemCountFromPosition(item_id, item_pos) >= 500 then
    print(">= 500 or more items on position!")
else
    print("< 500 items on position.")
end
 
Add into lib -> 050-function, or into specific scripts using 'local function bah blah'
Code:
function getItemCountFromPosition(item, pos)
    local item_pos = {x = pos.x, y = pos.y, z = pos.z}
    local item_count = 0
    for i = 1, 255 do
        local check_pos = {x = item_pos.x, y = item_pos.y, z = item_pos.z, stackpos = i}
        if getThingFromPos(check_pos).itemid <= 0 then
            break
        elseif getThingFromPos(check_pos).itemid == item then
            if isItemStackable(getThingFromPos(check_pos).itemid) == true then
                item_count = item_count + getThingFromPos(check_pos).type
            else
                item_count = item_count + 1
            end
        end
    end
    return item_count
end
Simple example to show functionality
Code:
local item_id = 2544 -- standard arrow
local item_pos = {x = 1000, y = 1000, z = 7}

if getItemCountFromPosition(item_id, item_pos) >= 500 then
    print(">= 500 or more items on position!")
else
    print("< 500 items on position.")
end

I know this is an old topic, but would it be possible to add this function to combat.lua in TFS 1.4.2?
I tried to use the above, but when there is no quantity of a given item on an item, I get errors, only when it is above 1 does the function work correctly [this function: Tile(POS):getItemById(ITEMID):getCount()].
 
Back
Top