Delay Timer in Script
Posted: Mon Apr 29, 2013 9:36 am
I try to have a delay in a script without using a timer already built into the editor, but I am faced with many difficulties. I have some ideas but I'm not an expert with script.
Initially the script is very simple:
the herderondie.onDieOne function runs when the herder die:
herderondie script:
But I want a delay between when the first herder die and when the other herder spawn.
if I simply put a timer in the editor, the problem is with the "self" in the function, because the herder spawn where the timer is placed in the editor. So not possible.
So my idea was to spawn a timer where the first herder dies:
But I am not satisfied with this code. For example, if I destroy several herders in the same time? The function collapse. And I intend to use in addition, several sequences with such longer period to create visual effects with particle, so this function is not suitable with many herders in the same room. There is probably another method, more efficient and more general, but I don't see.
Initially the script is very simple:
the herderondie.onDieOne function runs when the herder die:
Code: Select all
cloneObject{
name = "herder_dying",
baseObject = "herder",
onDie = function(self)
return herderondie.onDieOne(self)
end,
}herderondie script:
Code: Select all
function onDieOne(self)
spawn("herder",self.level,self.x,self.y,self.facing)
endif I simply put a timer in the editor, the problem is with the "self" in the function, because the herder spawn where the timer is placed in the editor. So not possible.
So my idea was to spawn a timer where the first herder dies:
Code: Select all
herder_ondieone_sequence = 0
function onDieOne(self)
if herder_ondieone_sequence == 0 then
t = spawn("timer", self.level, self.x,self.y,self.facing, "timer_herder_ondie_one"..self.id)
t:setTimerInterval(0.25)
t:addConnector("activate", "herderondie", "onDieOne")
t:activate()
herder_ondieone_sequence = 1
elseif herder_ondieone_sequence == 1 then
spawn("herder",self.level,self.x,self.y,self.facing)
herder_ondieone_sequence = 0
t:deactivate()
t:destroy()
end
end