When leaving an application state, the contained running timer is stopped. After returning to the state again, the timer continues counting at the place it was stopped. However, it is not possible (or at least I do not know how) to restart timer counting from the beginning when repeatedly switched to an application state. I could not achieve the desired behavior even with setting Timer.Interval = 0 on state's OnLeave and setting Timer.Interval = X on state's OnStart.
I tried to demonstrate the current behavior in the attached example. There are two application states A and B; A is the initial state. By pressing the key B, the application is switched to the state B running for approx. 10 seconds. After 10 seconds, the application is switched back to the state A automatically. Pressing the key A in state B returns application to the state A immediately. Now try the following:
1. Run application.
2. Press B to activate the state B.
3. Wait - after 10 seconds it is switched back to state A.
4. Press B again.
3. At any number less than 10 press A - application returns back to the state A.
4. Press B again.
5. Do not press anything and see counting - it never reaches 10, but only 10 - <previously interrupted counter>. This is the undesired behavior.
For now I use counting of App.Time instead of timers to overcome this problem. This workaround is shown in another attached example.
Is there some trick how to start counting of previously stopped timers from beginning? If not, would it be possible to add this feature? For instance, setting Timer.Interval or Timer.RepeatCount to some value should reset the timer counter.
Restarting interrupted timers
Moderator: Moderators
Restarting interrupted timers
- Attachments
-
- TimerWorkaround.zgeproj
- Timer problem workaround
- (1.94 KiB) Downloaded 545 times
-
- TimerProblem.zgeproj
- Timer problem
- (1.84 KiB) Downloaded 536 times
Hi Rado1,
I didn't envisage timers to be used this way but then again there wasn't any AppState components when I first wrote the Timer component
Your workaround works well and is a nice alternate solution.
I did think of an easy way to fix this with the Timer component too.
Internally the Timer component keeps track of the current relative time. Every frame this value is increased with App.DeltaTime. When it reaches Interval, the timer triggers. So I have now exposed this value with the new "Timer.CurrentRelativeTime" property. By setting this to zero in OnLeave like this "CounterTimer.CurrentRelativeTime = 0;" your project will work.
http://www.zgameeditor.org/files/ZGameEditor_beta.zip
See if it works for you. Btw, any chance of a screenshot or video preview of current zge projects of yours?
I didn't envisage timers to be used this way but then again there wasn't any AppState components when I first wrote the Timer component

Your workaround works well and is a nice alternate solution.
I did think of an easy way to fix this with the Timer component too.
Internally the Timer component keeps track of the current relative time. Every frame this value is increased with App.DeltaTime. When it reaches Interval, the timer triggers. So I have now exposed this value with the new "Timer.CurrentRelativeTime" property. By setting this to zero in OnLeave like this "CounterTimer.CurrentRelativeTime = 0;" your project will work.
http://www.zgameeditor.org/files/ZGameEditor_beta.zip
See if it works for you. Btw, any chance of a screenshot or video preview of current zge projects of yours?