Pausing a schedule command
by Nicolas Olhaberry · in Torque Game Builder · 01/03/2007 (8:05 am) · 10 replies
Hi guys,
I'm pausing my game using setScenePause(true) as explained on the Pause Screen Tutorial (http://tdn.garagegames.com/wiki/Torque_2D/StandardTutorials/Basics/PauseScreen). The problem I'm having is that I rely on schedules in several parts of the game code and they continue to run even after the scene is paused. Is there any way to get the schedules to pause also?
Thanks,
Nicolas.
I'm pausing my game using setScenePause(true) as explained on the Pause Screen Tutorial (http://tdn.garagegames.com/wiki/Torque_2D/StandardTutorials/Basics/PauseScreen). The problem I'm having is that I rely on schedules in several parts of the game code and they continue to run even after the scene is paused. Is there any way to get the schedules to pause also?
Thanks,
Nicolas.
#2
The thing is that it would be rather cumbersome to keep track of every schedule and cancel/resume by myself. Doesn't TGB has some sort of schedule manager that can be paused just like you can pause a scene?
01/03/2007 (12:56 pm)
Thank you Drew for your post, your idea definitely solves my problem.The thing is that it would be rather cumbersome to keep track of every schedule and cancel/resume by myself. Doesn't TGB has some sort of schedule manager that can be paused just like you can pause a scene?
#3
01/03/2007 (9:13 pm)
Not to my knowledge, tho it would be nice :)
#4
01/04/2007 (8:25 am)
It wouldn't be that hard to make a schedule manager in script - you could then use that to schedule things, except that it would store all the schedules for easy cancelling - but if you made a nice one, it could also reschedule them when you resumed.
#5
There is no central "schedule manager" in core Torque. When a schedule is executed, it does a couple of things:
1) Find the current simulation time, and calculates a "maturation time" that is the current time plus the number of system ticks closest to the requested millisecond time interval. In other words:
2) It posts a SimEvent to the event processing queue with an activation time of the calculated maturation time.
The sim continues to process time as normal, and when the current time matches the maturation time of the event just posted, the requested function is executed.
What's important to note here (and why you can't pause schedules in stock directly) is that even when a game is paused, sim time continues to advance--it has to, because sim time is used for all sorts of things other than just your game processing--things like user input tracking (you need them to be able to press something to resume the game!), network processing (even though you may not be using it, it's still happening), and the like. In other words, we are really using what is a very low level functionality to perform game level capabilities.
As an aside, this also describes why schedules are not 100% accurate in duration--and the longer a schedule is, the less accurate the actual interval before the event matures in real time is. Basically, in that case something has caused your number of ticks in the "real time" to deviate.
The "real" solution to something like this is to do exactly what others have said, and write yourself a higher level schedule manager that can take the game pause into account. I'd suggest that if you do write something like this, and real time accurate timing is also important to you, you may want to implement a system that tracks and manages real time passing over a schedule, and update the sim time the scheduled event should mature at based on the real time that has passed since it was scheduled.
01/04/2007 (10:38 am)
An underlying design assumption on schedule that may help clear this up:There is no central "schedule manager" in core Torque. When a schedule is executed, it does a couple of things:
1) Find the current simulation time, and calculates a "maturation time" that is the current time plus the number of system ticks closest to the requested millisecond time interval. In other words:
maturationTime = CurrentSimTime + RequestedMilliseconds / MillisecondsPerTick
2) It posts a SimEvent to the event processing queue with an activation time of the calculated maturation time.
The sim continues to process time as normal, and when the current time matches the maturation time of the event just posted, the requested function is executed.
What's important to note here (and why you can't pause schedules in stock directly) is that even when a game is paused, sim time continues to advance--it has to, because sim time is used for all sorts of things other than just your game processing--things like user input tracking (you need them to be able to press something to resume the game!), network processing (even though you may not be using it, it's still happening), and the like. In other words, we are really using what is a very low level functionality to perform game level capabilities.
As an aside, this also describes why schedules are not 100% accurate in duration--and the longer a schedule is, the less accurate the actual interval before the event matures in real time is. Basically, in that case something has caused your number of ticks in the "real time" to deviate.
The "real" solution to something like this is to do exactly what others have said, and write yourself a higher level schedule manager that can take the game pause into account. I'd suggest that if you do write something like this, and real time accurate timing is also important to you, you may want to implement a system that tracks and manages real time passing over a schedule, and update the sim time the scheduled event should mature at based on the real time that has passed since it was scheduled.
#6
01/04/2007 (5:55 pm)
Depending on your needs, you may just be able to set $timeScale = 0 when you pause and $timeScale = 1 when you unpause. A schedule manager is a worthy investment though since this method won't help you if you need to be selective at all about what stops running.
#7
Thanks again guys.
01/16/2007 (10:49 am)
Hey guys... sorry for not replying earlier, but I was struggling with a tight deadline. Thank you all for your replies. Richard's suggestion seems to work fine for me, so I don't think I'll write an schedule manager. But if I end up doing it, I'll post it here.Thanks again guys.
#8
Thanks again guys.
01/16/2007 (12:52 pm)
Hey guys... sorry for not replying earlier, but I was struggling with a tight deadline. Thank you all for your replies. Richard's suggestion seems to work fine for me, so I don't think I'll write an schedule manager. But if I end up doing it, I'll post it here.Thanks again guys.
#9
Thanks again guys.
01/16/2007 (1:53 pm)
Hey guys... sorry for not replying earlier, but I was struggling with a tight deadline. Thank you all for your replies. Richard's suggestion seems to work fine for me, so I don't think I'll write an schedule manager. But if I end up doing it, I'll post it here.Thanks again guys.
Torque Owner Drew -Gaiiden- Sikora
1) get a handle back from all your scheduled calls
2) upon pausing the scene, get the time remaining and cancel the event
3) upon un-pausing, recreate the event with the time remaining