• 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 1.X+ Issue with Item Description not being Recognized in Script

Curb

Active Member
Joined
Sep 7, 2014
Messages
82
Solutions
5
Reaction score
34
TFS 1.4.2

I have a script that involves modifying item descriptions and interacting with specific items (a bucket of water and a sack of feed, with IDs 26415 and 26414, respectively. The script is intended to read the item description, extract a quantity value from it, and then perform certain actions based on this value.

The strange part is that the script works perfectly for the bucket of water, but not for the feed. When I change the description of the sack of feed to match that of the bucket of water, the script starts working correctly for the sack of feed as well. I wonder if there is any cache I'm unaware of.

items.xml:
XML:
        <item id="26414" article="a" name="sack of feed">
            <attribute key="weight" value="50000" />
            <attribute key="description" value="Contains 500 oz of feed." />
        </item>  

        <item id="26415" article="a" name="bucket of water">          
            <attribute key="weight" value="25000" />
            <attribute key="description" value="Contains 250 oz of water." />
        </item>


trough_of_feed.lua:
Lua:
local config = {
    keyItem = 26414,
    maxAmount = 500,
    transformItems = {
        [26406] = 26408,
        [26409] = 26411
    }
}

function getAmountFromDescription(description)  
    local pattern = "Contains (%d+) oz of feed."
    local amount = description:match(pattern)
    return tonumber(amount) or 0
end

function getAmountFromItem(item)  
    local description = item:getAttribute(ITEM_ATTRIBUTE_DESCRIPTION) or ""  
    if description == "" then
        description = ItemType(item:getId()):getDescription() or ""      
    end  
    return getAmountFromDescription(description)
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)  
    if item.itemid ~= config.keyItem or not item then      
        return false
    end

    if not target or not target:isItem() then
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You cannot use this item here.")
        return true
    end

    local sackAmount = getAmountFromItem(item)
    print('Sack Amount: ', sackAmount)

    local transformTo = config.transformItems[target.itemid]
    local currentDescription = target:getAttribute(ITEM_ATTRIBUTE_DESCRIPTION) or ""
    local currentAmount = getAmountFromDescription(currentDescription)
    local amountToAdd

    if transformTo then      
        amountToAdd = sackAmount
        target:transform(transformTo)
    else      
        amountToAdd = math.min(config.maxAmount - currentAmount, sackAmount)
        if amountToAdd <= 0 then
            player:sendTextMessage(MESSAGE_STATUS_SMALL, "The trough is already full.")
            return true
        end
    end

    target:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "Contains " .. (currentAmount + amountToAdd) .. " oz of feed.")

    local newSackAmount = sackAmount - amountToAdd
    if newSackAmount > 0 then
        item:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "Contains " .. newSackAmount .. " oz of feed.")
    else
        item:remove(1)
    end

    player:sendTextMessage(MESSAGE_STATUS_SMALL, "You added feed to the trough.")
    toPosition:sendMagicEffect(CONST_ME_YELLOW_RINGS)
    return true
end

trough_of_water.lua:
Lua:
local config = {
    keyItem = 26415,
    maxAmount = 500,
    transformItems = {
        [26406] = 26407,
        [26409] = 26410
    }
}

function getAmountFromDescription(description)  
    local pattern = "Contains (%d+) oz of water."
    local amount = description:match(pattern)
    return tonumber(amount) or 0
end

function getAmountFromItem(item)  
    local description = item:getAttribute(ITEM_ATTRIBUTE_DESCRIPTION) or ""
    if description == "" then
        description = ItemType(item:getId()):getDescription() or ""
    end
    return getAmountFromDescription(description)
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid ~= config.keyItem or not item then
        return false
    end

    if not target or not target:isItem() then
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You cannot use this item here.")
        return true
    end

    local bucketAmount = getAmountFromItem(item)
    print('Bucket Amount: ', bucketAmount)

    local transformTo = config.transformItems[target.itemid]
    local currentDescription = target:getAttribute(ITEM_ATTRIBUTE_DESCRIPTION) or ""
    local currentAmount = getAmountFromDescription(currentDescription)
    local amountToAdd

    if transformTo then      
        amountToAdd = bucketAmount
        target:transform(transformTo)
    else      
        amountToAdd = math.min(config.maxAmount - currentAmount, bucketAmount)
        if amountToAdd <= 0 then
            player:sendTextMessage(MESSAGE_STATUS_SMALL, "The trough is already full.")
            return true
        end
    end

    target:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "Contains " .. (currentAmount + amountToAdd) .. " oz of water.")

    local newBucketAmount = bucketAmount - amountToAdd
    if newBucketAmount > 0 then
        item:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "Contains " .. newBucketAmount .. " oz of water.")
    else
        item:remove(1)
    end

    player:sendTextMessage(MESSAGE_STATUS_SMALL, "You added water to the trough.")
    toPosition:sendMagicEffect(CONST_ME_LOSEENERGY)
    return true
end

edit:
I converted it to revscript and it worked. 🤷‍♂️
Lua:
local config = {
    keyItem = 26414,
    maxAmount = 500,
    transformItems = {
        [26406] = 26408,
        [26409] = 26411
    }
}

local function getAmountFromDescription(description)
    local pattern = "Contains (%d+) oz of feed."
    local amount = description:match(pattern)
    return tonumber(amount) or 0
end

local function getAmountFromItem(item)
    local description = item:getAttribute(ITEM_ATTRIBUTE_DESCRIPTION) or ""
    if description == "" then
        description = ItemType(item:getId()):getDescription() or ""
    end   
    return getAmountFromDescription(description)
end

local feedTrough = Action()

function feedTrough.onUse(player, item, fromPosition, target, toPosition, isHotkey)   
    if item.itemid ~= config.keyItem or not item then       
        return false
    end

    if not target or not target:isItem() then
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You cannot use this item here.")
        return true
    end

    local sackAmount = getAmountFromItem(item)
    print('Sack Amount: ', sackAmount)

    local transformTo = config.transformItems[target.itemid]
    local currentDescription = target:getAttribute(ITEM_ATTRIBUTE_DESCRIPTION) or ""
    local currentAmount = getAmountFromDescription(currentDescription)
    local amountToAdd

    if transformTo then
        amountToAdd = sackAmount
        target:transform(transformTo)
    else
        amountToAdd = math.min(config.maxAmount - currentAmount, sackAmount)
        if amountToAdd <= 0 then
            player:sendTextMessage(MESSAGE_STATUS_SMALL, "The trough is already full.")
            return true
        end
    end

    target:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "Contains " .. (currentAmount + amountToAdd) .. " oz of feed.")

    local newSackAmount = sackAmount - amountToAdd
    if newSackAmount > 0 then
        item:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "Contains " .. newSackAmount .. " oz of feed.")
    else
        item:remove(1)
    end

    player:sendTextMessage(MESSAGE_STATUS_SMALL, "You added feed to the trough.")
    toPosition:sendMagicEffect(CONST_ME_YELLOW_RINGS)
    return true
end

feedTrough:id(config.keyItem)
feedTrough:register()

I'm still curious to know why that happened, if anyone knows, please.
 
Last edited:
Back
Top