Howl3r
Howl3r wrote:Is there a way to fiddle with timers and stuff so that I can make an enemy respawn in case it is not killed in given time?
If you need it as part of a custom monster, add this component:
Code: Select all
{
class = "Timer",
name = "respawnTimer",
timerInterval = 10,
disableSelf = true,
onActivate = function(self)
end,
},
And this to the monster component:
Code: Select all
onDie = function(self)
if not self.go.respawnTimer:isEnabled() then
self:setExp(0) --Preventing infinite xp (or don't)
spawn(self.go.name, self.go.level, self.go.x, self.go.y, self.go.facing, self.go.elevation)
end
end,
The monster will not respawn if it is killed within 10 seconds
after it spawned, so be sure to spawn it (rather than placing it) when the party should encounter it. (or activate via script)
Since the monster object has a short delay before destroying itself, you could add another initially disabled timer and set its interval to something like 0.5 or 1, have it activate when the monster dies, and upon it activating, it'll spawn the new monster. Don't forget to add a fancy particle effect
If you need it on a single monster, do the following:
Add a timer, name it "respawn_timer_1", make it disable self, and have the interval set to the time for the party to kill the monster.
Add a monster, name it "myMonster_1", and have it trigger the scripts "respawn" function on death.
Add a script entity, name it "respawn_script_1", and add this to it:
Code: Select all
function respawn(monster)
if not respawn_timer_1.timer:isEnabled() then
if monster.go.id == "myMonster_1" then
if not myMonster_2 then
local m = spawn(monster.go.name, monster.go.level, monster.go.x, monster.go.y, monster.go.facing, monster.go.elevation, "myMonster_2")
m.monster:addConnector("onDie", "respawn_script_1", "respawn")
respawn_timer_1.timer:enable()
else
print("error")
end
elseif monster.go.id == "myMonster_2" then
if not myMonster_1 then
local m = spawn(monster.go.name, monster.go.level, monster.go.x, monster.go.y, monster.go.facing, monster.go.elevation, "myMonster_1")
m.monster:addConnector("onDie", "respawn_script_1", "respawn")
respawn_timer_1.timer:enable()
else
print("error")
end
else
print("wrong ID")
end
else
print("The monster died")
end
end
Make sure the interval is not too small(higher than 5), so the monster object will be deleted by the time he is ready to respawn.
If you still want to the interval to be lower than 5, you could make then "myMonster_2" checker spawn a ""myMonster_3", and have its checker spawn a "myMonster_1".