• 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 Get item count/fluid type on a specific tile

Unknown Soldier

Mapping a map
Joined
Oct 30, 2010
Messages
294
Solutions
11
Reaction score
665
Hello,

I thought it would be pretty simple to get the position and amount of items on the ground, but got no results. So the thing is, that I want to check if a fluid container (a vase for instance) at certain position is filled with certain fluid like water or blood or whatever fluid type it is. Can somebody give me a hint? TFS 1.4.2

Lua:
local item1pos = Position(100, 100, 7)
local item1 = Tile(item1pos):getItemById(12345)
if item1:getCount() == 2 then --2 is for blood
...something...
end
That's where I am stuck...

Thanks in advance
 
Lua:
local itemId = 12345
local position = Position(100, 100, 7)
local tile = Tile(position)
if not tile then
    return
end

local items = tile:getItems()

for i = 1, #items do
    local item = items[i]
    if item:getId() == itemId then
        if ItemType(itemId):isFluidContainer() then -- don't really need this, but added for extra good measure. xP
            local fluidType = item:getFluidType()
            if fluidType == FLUID_WATER or fluidType == FLUID_BLOOD then
                -- do something
                break
            end
        end
    end
end
 
Thanks for quick reply! It's almost working! ^^
Made a lever script using your code, but it doesn't go inside last "if", so after checking fluid type, as a result "cc" is not printed. Where could be an error?

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

local itemId = 33883
local position = Position(2098, 1238, 10)
local tile = Tile(position)
if not tile then
    return
end
 
if item.itemid == 1945 then
item:transform(item.itemid == 1946 and 1945 or 1946)
print("aa")
local items = tile:getItems()
for i = 1, #items do
    local checkitem = items[i]
    if checkitem:getId() == itemId then
        if ItemType(itemId):isFluidContainer() then
            local fluidType = checkitem:getFluidType()
            print("bb")
            if fluidType == FLUID_BLOOD then
                print("cc")
                break
            end
        end
    end
end

elseif item.itemid == 1946 then
item:transform(item.itemid == 1945 and 1946 or 1945)
end

end
 
Thanks for quick reply! It's almost working! ^^
Made a lever script using your code, but it doesn't go inside last "if", so after checking fluid type, as a result "cc" is not printed. Where could be an error?

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

local itemId = 33883
local position = Position(2098, 1238, 10)
local tile = Tile(position)
if not tile then
    return
end
 
if item.itemid == 1945 then
item:transform(item.itemid == 1946 and 1945 or 1946)
print("aa")
local items = tile:getItems()
for i = 1, #items do
    local checkitem = items[i]
    if checkitem:getId() == itemId then
        if ItemType(itemId):isFluidContainer() then
            local fluidType = checkitem:getFluidType()
            print("bb")
            if fluidType == FLUID_BLOOD then
                print("cc")
                break
            end
        end
    end
end

elseif item.itemid == 1946 then
item:transform(item.itemid == 1945 and 1946 or 1945)
end

end
not sure.

I added a few more prints after bb to help figure out the issue.
Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local itemId = 33883
    local position = Position(2098, 1238, 10)
    local tile = Tile(position)
    if not tile then
        return
    end
    
    if item.itemid == 1945 then
        item:transform(item.itemid == 1946 and 1945 or 1946)
        print("aa")
        local items = tile:getItems()
        for i = 1, #items do
            local checkitem = items[i]
            if checkitem:getId() == itemId then
                if ItemType(itemId):isFluidContainer() then
                    local fluidType = checkitem:getFluidType()
                    print("bb")
                    print(fluidType .. " is fluid in container")
                    print(FLUID_BLOOD .. " is fluid blood")
                    if fluidType == FLUID_BLOOD then
                        print("cc")
                        break
                    end
                end
            end
        end
    
    elseif item.itemid == 1946 then
        item:transform(item.itemid == 1945 and 1946 or 1945)
    end
    return true
end
 
Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

local itemId = 33883
local position = Position(2098, 1238, 10)
local tile = Tile(position)
if not tile then
    return
end
 
if item.itemid == 1945 then
item:transform(item.itemid == 1946 and 1945 or 1946)
local items = tile:getItems()
for i = 1, #items do
    local checkitem = items[i]
        if checkitem:getId() == itemId then
            if ItemType(itemId):isFluidContainer() then
                local fluidType = checkitem:getFluidType()
                print("bb")
                print(fluidType .. " is fluid in container")
                if fluidType == 2 then
                    print("Blood is in the vase.")
                break
            end
        end
    end
end

elseif item.itemid == 1946 then
item:transform(item.itemid == 1945 and 1946 or 1945)
end

end

Those prints helped, thank you Xikini, once again :) it didn't want to use FLUID_BLOOD (error message below), when used a number instead, it works fine.

Code:
data/actions/scripts/other/vase_of_blood_lever.lua:20: attempt to concatenate global 'FLUID_BLOOD' (a nil value)
stack traceback:
        [C]: in function '__concat'
        data/actions/scripts/other/vase_of_blood_lever.lua:20: in function <data/actions/scripts/other/vase_of_blood_lever.lua:1>
 
Back
Top