CastorFlynn
Member
- Joined
- Aug 29, 2021
- Messages
- 89
- Reaction score
- 8
In this creaturescript code, when a creature is killed, a green vortex is created. If the same creature dies in the position of the green vortex, it turns purple and, with the same logic, turns red. I would like each vortex created/transformed to have 30 seconds before being removed. So if the green vortex was created, if 20sec passes and I transform it into purple it would have another 30sec until it is removed if it is not transformed into a red vortex. If it turns red, it would take another 30s until it is removed. In the current script the removal time is not "renewed/incremented".
Lua:
local scheduledEvents = {}
local function removeVortex(pos)
local vortex = Tile(pos):getItemById(26394) or Tile(pos):getItemById(26395) or Tile(pos):getItemById(26396)
if vortex then
vortex:remove()
end
scheduledEvents[pos] = nil -- Clears the scheduled event by removing the vortex
end
local function scheduleVortexRemoval(pos)
if scheduledEvents[pos] then
stopEvent(scheduledEvents[pos]) -- Cancels the previous event if it exists
end
scheduledEvents[pos] = addEvent(removeVortex, 30 * 1000, pos) -- Schedule a new removal event
end
local corruptedSoul = CreatureEvent("CorruptedSoul")
function corruptedSoul.onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
local targetMonster = creature:getMonster()
if not targetMonster then
return true
end
local pos = targetMonster:getPosition()
local vortex = Tile(pos):getItemById(26394) or Tile(pos):getItemById(26395) or Tile(pos):getItemById(26396)
if not vortex then
Game.createItem(26394, 1, pos)
scheduleVortexRemoval(pos)
else
if vortex:getId() == 26394 then
vortex:transform(26395)
elseif vortex:getId() == 26395 then
vortex:transform(26396)
end
scheduleVortexRemoval(pos)
end
return true
end
corruptedSoul:register()