• 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+ Quick question about addEvent()

Soulster

New Member
Joined
Dec 4, 2023
Messages
11
Reaction score
1
Lua:
local itemIdToEffectId = {
    [365] = 40,
    [370] = 41,
    [375] = 42,
    [380] = 43,
    [385] = 44
}

local itemIdToTime = {
    [365] = 299, -- Time for item with ID 365
    [370] = 299, -- Time for item with ID 370
    [375] = 299, -- Time for item with ID 375
    [380] = 399, -- Time for item with ID 380
    [385] = 399  -- Time for item with ID 385
}

local itemIdToAssignedId = {
    [365] = 367, -- Assigned ID for item with ID 365
    [370] = 372, -- Assigned ID for item with ID 370
    [375] = 377, -- Assigned ID for item with ID 375
    [380] = 382, -- Assigned ID for item with ID 380
    [385] = 387  -- Assigned ID for item with ID 385
}

local itemIdToDissapearEffect = {
    [365] = 45, -- Assigned EffectID for item with ID 365
    [370] = 46, -- Assigned EffectID for item with ID 370
    [375] = 47, -- Assigned EffectID for item with ID 375
    [380] = 48, -- Assigned EffectID for item with ID 380
    [385] = 49  -- Assigned EffectID for item with ID 385
}
local action = Action()

function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    
    local storedBagItems = item:getItems()
    local itemPos = item:getPosition()
    local itemId = item:getId()
    local effectId = itemIdToEffectId[itemId]
    local time = itemIdToTime[itemId]
    local openedBag = itemIdToAssignedId[itemId]
    local dissapearEffect = itemIdToDissapearEffect[itemId]

    item:remove()

    itemPos:sendMagicEffect(effectId)
    addEvent(function()
        local bag = Game.createItem(openedBag, 1, itemPos)
        for i = 1, #storedBagItems do
            bag:addItem(storedBagItems[i]:getId(), storedBagItems[i]:getCount())
        end
        addEvent(function()
            bag:remove()
            itemPos:sendMagicEffect(dissapearEffect)
        end, 120000)
    end, time)
end

action:id(365, 370, 375, 380, 385)
action:register()


My question is how likely this code is to break/crash something
I have a feeling like the addEvent might be a problem since they hold the info about items without passing them as arguments
Also if it happends like its not a problem then its might be a performence problem since this script will hang around for 2 minutes so the last addevent will execute

Could somebody provide me with some information ?
 
Ok, there is no check if that bag exsists so after addEvent is executed and bag not exsists = crash server

with addEvents u must be extreme carefull, for example you're not parsing player here but his cid and doing check if that player still exsists, same for items and literally for everything
 
Ok, there is no check if that bag exsists so after addEvent is executed and bag not exsists = crash server

with addEvents u must be extreme carefull, for example you're not parsing player here but his cid and doing check if that player still exsists, same for items and literally for everything
Ye i was thinking about it but im not sure why those variables even have values without passing them to the function
 
Lua:
local itemIdToEffectId = {
    [365] = 40,
    [370] = 41,
    [375] = 42,
    [380] = 43,
    [385] = 44
}

local itemIdToTime = {
    [365] = 299, -- Time for item with ID 365
    [370] = 299, -- Time for item with ID 370
    [375] = 299, -- Time for item with ID 375
    [380] = 399, -- Time for item with ID 380
    [385] = 399  -- Time for item with ID 385
}

local itemIdToAssignedId = {
    [365] = 367, -- Assigned ID for item with ID 365
    [370] = 372, -- Assigned ID for item with ID 370
    [375] = 377, -- Assigned ID for item with ID 375
    [380] = 382, -- Assigned ID for item with ID 380
    [385] = 387  -- Assigned ID for item with ID 385
}

local itemIdToDissapearEffect = {
    [365] = 45, -- Assigned EffectID for item with ID 365
    [370] = 46, -- Assigned EffectID for item with ID 370
    [375] = 47, -- Assigned EffectID for item with ID 375
    [380] = 48, -- Assigned EffectID for item with ID 380
    [385] = 49  -- Assigned EffectID for item with ID 385
}
local action = Action()

function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
 
    local storedBagItems = item:getItems()
    local itemPos = item:getPosition()
    local itemId = item:getId()
    local effectId = itemIdToEffectId[itemId]
    local time = itemIdToTime[itemId]
    local openedBag = itemIdToAssignedId[itemId]
    local dissapearEffect = itemIdToDissapearEffect[itemId]

    item:remove()

    itemPos:sendMagicEffect(effectId)
    addEvent(function()
        local bag = Game.createItem(openedBag, 1, itemPos)
        for i = 1, #storedBagItems do
            bag:addItem(storedBagItems[i]:getId(), storedBagItems[i]:getCount())
        end
        addEvent(function()
            bag:remove()
            itemPos:sendMagicEffect(dissapearEffect)
        end, 120000)
    end, time)
end

action:id(365, 370, 375, 380, 385)
action:register()


My question is how likely this code is to break/crash something
I have a feeling like the addEvent might be a problem since they hold the info about items without passing them as arguments
Also if it happends like its not a problem then its might be a performence problem since this script will hang around for 2 minutes so the last addevent will execute

Could somebody provide me with some information ?
Assuming that the item cannot be moved to any player or container, that is, it can only be moved on the map, you can do the following trick:

Lua:
local itemIdToEffectId = {
    [365] = 40,
    [370] = 41,
    [375] = 42,
    [380] = 43,
    [385] = 44
}

local itemIdToTime = {
    [365] = 299, -- Time for item with ID 365
    [370] = 299, -- Time for item with ID 370
    [375] = 299, -- Time for item with ID 375
    [380] = 399, -- Time for item with ID 380
    [385] = 399 -- Time for item with ID 385
}

local itemIdToAssignedId = {
    [365] = 367, -- Assigned ID for item with ID 365
    [370] = 372, -- Assigned ID for item with ID 370
    [375] = 377, -- Assigned ID for item with ID 375
    [380] = 382, -- Assigned ID for item with ID 380
    [385] = 387 -- Assigned ID for item with ID 385
}

local itemIdToDissapearEffect = {
    [365] = 45, -- Assigned EffectID for item with ID 365
    [370] = 46, -- Assigned EffectID for item with ID 370
    [375] = 47, -- Assigned EffectID for item with ID 375
    [380] = 48, -- Assigned EffectID for item with ID 380
    [385] = 49 -- Assigned EffectID for item with ID 385
}

local cacheItems = {}

if not __AUTO__ID then __AUTO__ID = 0 end
local function getAutoId()
    __AUTO__ID = __AUTO__ID + 1
    return __AUTO__ID
end

local function removeBag(autoID, dissapearEffect)
    local cacheItem = cacheItems[autoID]
    if not cacheItem then return end
    local tile = Tile(cacheItem.itemPos)
    if not tile then return end
    local item = tile:getItemById(cacheItem.itemId)
    if not item then return end
    item:remove()
    cacheItem.itemPos:sendMagicEffect(dissapearEffect)
end

local function createBag(openedBag, itemPos, storedBagItems, dissapearEffect)
    local bag = Game.createItem(openedBag, 1, itemPos)
    if not bag then return end
    for _, it in ipairs(storedBagItems) do bag:addItem(it.id, it.count) end
    local autoID = getAutoId()
    bag:setCustomAttribute("autoID", autoID)
    cacheItems[autoID] = {itemId = openedBag, itemPos = itemPos}
    addEvent(removeBag, 20000, autoID, dissapearEffect)
end

local action = Action()

function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local itemPos = item:getPosition()
    local itemId = item:getId()
    local effectId = itemIdToEffectId[itemId]
    local time = itemIdToTime[itemId]
    local openedBag = itemIdToAssignedId[itemId]
    local dissapearEffect = itemIdToDissapearEffect[itemId]
    local items = item:getItems()
    local storedBagItems = {}
    for _, it in ipairs(items) do
        storedBagItems[#storedBagItems + 1] = {id = it:getId(), count = it:getCount()}
    end
    addEvent(createBag, time, openedBag, itemPos, storedBagItems, dissapearEffect)
    item:remove()
    itemPos:sendMagicEffect(effectId)
    return true
end

action:id(365, 370, 375, 380, 385)
action:register()

local event = EventCallback

function event.onItemMoved(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local autoID = item:getCustomAttribute("autoID")
    if not autoID then return end
    local cacheItem = cacheItems[autoID]
    if not cacheItem then return end
    cacheItem.itemPos = toPosition
end

event:register()
Post automatically merged:

If it really is immovable, then you can completely remove the EventCallback and this: bag:setCustomAttribute("autoID", autoID)
 
Back
Top