Page 1 of 2
can timer code be used in scripts?
Posted: Tue Sep 18, 2012 1:31 pm
by Komag
I haven't seen any examples of timer use in scripts (other than the scripting reference list: activate, deactivate, toggle, setTimerinterval)
I want to spawn 10 items over the course of 1 second, so each pops into view in 0.1 seconds. Currently I can do this using 10 timers, each set for 0.1 seconds, and each connected both to a spawner and to the next timer. Thus the full sequence occurs. But that's a lot of crap on the editor screen. I'd prefer a single script that can manage it. Doable?
Re: can timer code be used in scripts?
Posted: Tue Sep 18, 2012 2:12 pm
by antti
You can handle it all with one timer. Here's an untested sample code on how it could be implemented:
Code: Select all
time = 0
function tick()
if time == 0 then
-- spawn object 1 here
elseif time == 1 then
-- spawn object 2 here
elseif time == 2 then
-- spawn object 3 here, and for the sake of example let's say it's the last object on our list)
-- so then we can even loop back to the beginning here by uncommenting the next two lines if we want to:
-- time = 0
-- return
end
time = time + 1
end
Connect the timer to the tick-function.
The same can also be done with one timer and multiple counter entities too but I suppose that's no more clean than your original approach of using a bunch of timers.
Re: can timer code be used in scripts?
Posted: Tue Sep 18, 2012 3:45 pm
by Komag
It works beautifully, thanks. The only thing I added was another line to deactivate the timer after the last item spawns.
Now it works and does one full round of spawning each time I press the button, but I can't spam the button, which is good.
Interestingly, this is still not really using a timer within a script, it's just utilizing a timer's constant signals to control a counter within the script. So there is no code for scripts which waits a certain time, then does something, then waits a different amount of time, then does something else, etc?
Re: can timer code be used in scripts?
Posted: Tue Sep 18, 2012 4:22 pm
by petri
What's wrong with using a timer that triggers the script?
Re: can timer code be used in scripts?
Posted: Tue Sep 18, 2012 4:31 pm
by Komag
it's certainly fine in this case, I'm just thinking of a situation where I want a sequence of events that all have different amounts of time inbetween them, such as a spawn of something after 1 sec, then a teleporter opens after 3 seconds, then two fireballs fly after 4 seconds but both are .5 seconds apart, etc etc etc.
I suppose it could all still work with the one timer so long as the tick interval is small enough to measure out various segments of time. I could have the script do something after the tick counter reaches 5, then 15, then 35, then 36, etc, so that way I get the varying amounts of time I want.
Re: can timer code be used in scripts?
Posted: Tue Sep 18, 2012 5:47 pm
by Lilltiger
If you check my contribution to the Sequence lock:
viewtopic.php?f=14&t=3199
There you see code that creates a timer and uses it in script alone.
Although I don't know if this causes issues with saves etc, also timers probably eat quite a bit resources, so the fewer the better.
Re: can timer code be used in scripts?
Posted: Sun Oct 14, 2012 6:19 am
by akroma222
Hey Guys,
I have used the script (as above) to open and close a series of pit/trap doors (similar to the trapdoor run on level 5 of Mount Grimrock)... script works great (THANK YOU!).
However, the script will only run through once. There is a hidden pressure plate which is connected to a timer which is then connected to the script...
The script finishes off as such...
.......................
.......................
elseif timecount == 28 then
timePit26: open()
end
timecount = timecount +1
end
Wondering what I need to change to make sure the script will run through each time I walk onto the hidden plate (sorry if this is bleedingly obvious!)
Re: can timer code be used in scripts?
Posted: Sun Oct 14, 2012 11:48 am
by Komag
Re: can timer code be used in scripts?
Posted: Sun Oct 14, 2012 12:42 pm
by akroma222
Thanks Komag
I read through the thread but I wanted to stick with Antti's code (as I kind of understand it lol)
I did nut out a solution though...
Script ends with...
......................
elseif timecount == 29 then
timecount = 0
cooltimer2: deactivate()
end
timecount = timecount +1
end
Thanks again to both of you!!!
Re: can timer code be used in scripts?
Posted: Sun Oct 14, 2012 6:17 pm
by Szragodesca
Would it be too CPU intensive to run some form of for/while loop for the purpose you requested Komag?
I mean, so long as you're only running it while one event is in process, and not for the duration of the game, it shouldn't get too resource hungry, right?
Have a local variable that is created/set when the event starts, and as long as that variable == 1 the For loop keeps running. Once the sequence is complete, set it back to 0 and break/end the script. If it's for some puzzle thing (like the pit room with the path you have to follow) then I'm sure you could clear the data used in the script before ending it. Next time you push the button or pull the lever it restarts the script.
Would that work?
(partially asking so I can learn the answers
)