• 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 Remove (boss) monster after time x

Fermantor

Active Member
Joined
Dec 16, 2009
Messages
209
Solutions
4
Reaction score
33
Location
Germany
TFS 1.3 (OTBR)

I wanna add a function that boss monsters (rare spawning creatures like undead cavebear, midnight panther, etc.) only life for like 3h and get removed after that time has passed.
The problem is, I don't know how I can set a starting point for the timer, since monsters that appear in raids don't count under the "onSpawn" function, so I can't start an event at the spawn time.
The "onThink" function for the monster only executed when it is active, so I need a way to get the spawned time or "alive in seconds" parameter to compare it in onThink.
My question now is, can I get the "created time" somehow or start an event with the creation of the monster?

If you're having problem understanding my problem I try to explain it in more detail.
 
Solution
Untitled.png

When the creature spawns, it will activate onThink 1 time, which you could use to create an addEvent to destroy it after x time.

data/creaturescripts/creaturescripts.xml
XML:
<event type="think" name="monster_onThink" interval="1000" script="monster_onThink.lua" />
data/creaturescripts/scripts/monster_onThink.lua
Lua:
function onThink(creature, interval)
    print("I am a " .. creature:getName() .. " and I am thinking!")
    return true
end
And then put this into any data/monster you want.
XML:
<script>
    <event name="monster_onThink" />
</script>

More specific to your scenario, let's destroy this monster after 10 seconds.

Untitled.png

Lua:
local destroyTimer = 10 -- seconds
local creatures =...

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,612
Solutions
395
Reaction score
3,731
Untitled.png

When the creature spawns, it will activate onThink 1 time, which you could use to create an addEvent to destroy it after x time.

data/creaturescripts/creaturescripts.xml
XML:
<event type="think" name="monster_onThink" interval="1000" script="monster_onThink.lua" />
data/creaturescripts/scripts/monster_onThink.lua
Lua:
function onThink(creature, interval)
    print("I am a " .. creature:getName() .. " and I am thinking!")
    return true
end
And then put this into any data/monster you want.
XML:
<script>
    <event name="monster_onThink" />
</script>

More specific to your scenario, let's destroy this monster after 10 seconds.

Untitled.png

Lua:
local destroyTimer = 10 -- seconds
local creatures = {}

local function resetTableIfAllCreaturesDead()
    for v, k in pairs(creatures) do
        local creature = Creature(v)
        if creature then
            return
        end
    end
    creatures = {}
    print("Creature table has been reset! Yay for no memory leak.")
end

local function removeCreatureAfterAwhile(creatureId)
    local creature = Creature(creatureId)
    if creature then
        print("Creature still alive!")
        if creatures[creatureId] < os.time() then
            print("Creature timer expired. Removing!")
            creature:remove()
            resetTableIfAllCreaturesDead()
            return
        end
        addEvent(removeCreatureAfterAwhile, 1000, creatureId)
    end
end

function onThink(creature, interval)
    print("I am a " .. creature:getName() .. " and I am thinking!")
    local creatureId = creature:getId()
    if not creatures[creatureId] then
        print("Creature added to the table!")
        creatures[creatureId] = os.time() + destroyTimer
        addEvent(removeCreatureAfterAwhile, 1000, creatureId)
    end
    return true
end
 
Solution
OP
OP
F

Fermantor

Active Member
Joined
Dec 16, 2009
Messages
209
Solutions
4
Reaction score
33
Location
Germany
When the creature spawns, it will activate onThink 1 time, which you could use to create an addEvent to destroy it after x time.
Thanks a lot for pointing that out man! This helped me so much! I actually thought I tested this but turns out, I didn't. That's all I need. Thank you so much

Also props for writing the whole script, that would not have been necessary but is greatly appreciated.
 
Top