• 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.X Check CAP + free space slots in backpack

fyalhed

Member
Joined
Nov 18, 2017
Messages
156
Reaction score
20
I can check if player got cap to get the items by just check getPlayerFreeCap

Code:
        -- dont have cap
        if getPlayerFreeCap(cid) < TILE.needcap then
            doTeleportThing(cid, fromPosition, false)
            doSendMagicEffect(position, CONST_ME_MAGIC_BLUE)
            doPlayerPopupFYI(cid, 'You need '.. TILE.needcap ..' OZ CAP to recive the items!')
            return false
        end
        for i = 1, #TILE.extraItems do
            doPlayerAddItem(cid, TILE.extraItems[i].itemID, TILE.extraItems[i].count)
        end
        setPlayerStorageValue(cid, item.actionid, 1)

But sometimes players has the cap, but don't have space, slots in backpack to recive the items

How to check if player has the free slots enough?
 
Solution
I don't know if there's an existing function to check that but since i was bored i created a new one 😅

It will search for containers on your equipment slots and then it will check all the free slots on those containers (including containers inside the containers, until there is nothing to check)

It will also check your equipment slots as 'free space' if there is no container on them (RIGHT-LEFT HAND SLOT, AMMO SLOT)

Add this function to your libs/050-functions.lua
Lua:
function getPlayerFreeSlots(cid) -- by Musztang
    local slots = 0
    local containers = {}
    for s = 3, 10 do -- CONST_SLOT_BACKPACK to CONST_SLOT_AMMO
        local item = getPlayerSlotItem(cid, s)
        if item.itemid > 0 then
            if...
I don't know if there's an existing function to check that but since i was bored i created a new one 😅

It will search for containers on your equipment slots and then it will check all the free slots on those containers (including containers inside the containers, until there is nothing to check)

It will also check your equipment slots as 'free space' if there is no container on them (RIGHT-LEFT HAND SLOT, AMMO SLOT)

Add this function to your libs/050-functions.lua
Lua:
function getPlayerFreeSlots(cid) -- by Musztang
    local slots = 0
    local containers = {}
    for s = 3, 10 do -- CONST_SLOT_BACKPACK to CONST_SLOT_AMMO
        local item = getPlayerSlotItem(cid, s)
        if item.itemid > 0 then
            if isContainer(item.uid) then
                table.insert(containers, item.uid)
            end
        else
            if s == 5 or s == 6 or s == 10 then
                slots = slots + 1
            end
        end
    end
    while #containers > 0 do
        for i = (getContainerSize(containers[1]) - 1), 0, -1 do
            local it = getContainerItem(containers[1], i)
            if isContainer(it.uid) then
                table.insert(containers, it.uid)
            end
        end
        slots = slots + (getContainerCap(containers[1]) - getContainerSize(containers[1]))
        table.remove(containers, 1)
    end
    return slots
end

Now on your script, you can simply need to do a check before giving the items:
Lua:
if getPlayerFreeSlots(cid) < #TILE.extraitems then
    return doPlayerPopupFYI(cid, 'You dont have enough space to carry this items.') and false
end

@TOPIC ASIDE
Also if someone is interested in a function to separately check containers, you can use this function.
Lua:
function getFreeSlotsFromContainer(item) -- by Musztang
    local slots = 0
    local containers = {}
    if isContainer(item) then
        table.insert(containers, item)
    end
    while #containers > 0 do
        for i = (getContainerSize(containers[1]) - 1), 0, -1 do
            local it = getContainerItem(containers[1], i)
            if isContainer(it.uid) then
                table.insert(containers, it.uid)
            end
        end
        slots = slots + (getContainerCap(containers[1]) - getContainerSize(containers[1]))
        table.remove(containers, 1)
    end
    return slots
end

Use: getFreeSlotsFromContainer(container.uid)
 
Last edited:
Solution
Why dont you just use simple if doPlayerAddItem... ?
Depending on the situation, it's not a good idea to allow items to drop onto the floor.

Kill boss -> get special loot.. no guarantee that the player has cap or space.
Cash shop -> again, can't let that drop on the floor

Most one-time quests from chests it's fine, but some are too 'important' to let drop.
Or if an npc is giving a quest item, et cetera.

but the most important part..
Is having the functionality to do either or.
 
Depending on the situation, it's not a good idea to allow items to drop onto the floor.

Kill boss -> get special loot.. no guarantee that the player has cap or space.
Cash shop -> again, can't let that drop on the floor

Most one-time quests from chests it's fine, but some are too 'important' to let drop.
Or if an npc is giving a quest item, et cetera.

but the most important part..
Is having the functionality to do either or.
Lua:
doPlayerAddItem(cid, itemid[, count/subtype = 1[, canDropOnMap = true[, slot = 0]]])
Lua:
if doPlayerAddItem(cid, itemid, count, false) == RETURNVALUE_NOERROR then
    -- do something
else
    -- no space or cap
    return true
end
 
Last edited:
Lua:
doPlayerAddItem(cid, itemid[, count/subtype = 1[, canDropOnMap = true[, slot = 0]]])
Lua:
if doPlayerAddItem(cid, itemid, count, false) == RETURNVALUE_NOERROR then
    -- do something
else
    -- no space or cap
    return true
end
Okay, now do that with 23 items.
 
Lua:
doPlayerAddItem(cid, itemid[, count/subtype = 1[, canDropOnMap = true[, slot = 0]]])
Lua:
if doPlayerAddItem(cid, itemid, count, false) == RETURNVALUE_NOERROR then
    -- do something
else
    -- no space or cap
    return true
end
With that if you have 2 slots free and the array has 5 items, you will only get 2 items and u wont get the rest. Thats why i made the function to prevent that.
 
Last edited:
With that if you have 2 slots free and the array has 5 items, you will only get 2 items and u wont get the rest. Thats why i made the function to prevent that.
Im sorry, havent actually realized he wanted to do that in loop :p still you may do the same with adding virtual items to some backpack and gives player backpack with items inside
 
Im sorry, havent actually realized he wanted to do that in loop :p still you may do the same with adding virtual items to some backpack and gives player backpack with items inside
Yeah, i though about that aswell but i was bored 😆 Still, i believe the function can be useful.
 
I don't know if there's an existing function to check that but since i was bored i created a new one 😅

It will search for containers on your equipment slots and then it will check all the free slots on those containers (including containers inside the containers, until there is nothing to check)

It will also check your equipment slots as 'free space' if there is no container on them (RIGHT-LEFT HAND SLOT, AMMO SLOT)

Add this function to your libs/050-functions.lua
Lua:
function getPlayerFreeSlots(cid) -- by Musztang
    local slots = 0
    local containers = {}
    for s = 3, 10 do -- CONST_SLOT_BACKPACK to CONST_SLOT_AMMO
        local item = getPlayerSlotItem(cid, s)
        if item.itemid > 0 then
            if isContainer(item.uid) then
                table.insert(containers, item.uid)
            end
        else
            if s == 5 or s == 6 or s == 10 then
                slots = slots + 1
            end
        end
    end
    while #containers > 0 do
        for i = (getContainerSize(containers[1]) - 1), 0, -1 do
            local it = getContainerItem(containers[1], i)
            if isContainer(it.uid) then
                table.insert(containers, it.uid)
            end
        end
        slots = slots + (getContainerCap(containers[1]) - getContainerSize(containers[1]))
        table.remove(containers, 1)
    end
    return slots
end

Now on your script, you can simply need to do a check before giving the items:
Lua:
if getPlayerFreeSlots(cid) < #TILE.extraitems then
    return doPlayerPopupFYI(cid, 'You dont have enough space to carry this items.') and false
end

@TOPIC ASIDE
Also if someone is interested in a function to separately check containers, you can use this function.
Lua:
function getFreeSlotsFromContainer(item) -- by Musztang
    local slots = 0
    local containers = {}
    if isContainer(item) then
        table.insert(containers, item)
    end
    while #containers > 0 do
        for i = (getContainerSize(containers[1]) - 1), 0, -1 do
            local it = getContainerItem(containers[1], i)
            if isContainer(it.uid) then
                table.insert(containers, it.uid)
            end
        end
        slots = slots + (getContainerCap(containers[1]) - getContainerSize(containers[1]))
        table.remove(containers, 1)
    end
    return slots
end

Use: getFreeSlotsFromContainer(container.uid)
thank you!!!
 
Back
Top