@Laura,
Is it possible for a timer event to get in between these 2 lines of code?
TL;DR - The short answer is, “No.”
As long as Corona is not multi-threaded (and I’m 99.99% sure it is not), then no script or (non-system) event may preempt an executing script.
Both enterFrame listeners and timers are queued for processing on the next available frame.
For enterFrame listeners this is every frame, for timers it depends on when the timer duration has ‘expired’.
Event Processing Queue(s)
I have not seen the internals of Corona, but I have coded a written two game engines (long ago) and if I were to hazard a guess, the way enterFrame, other event listeners, and timers are being handled can occur in one of three basic ways.
First, let’s assume the engine/sdk has a game loop that does its best to execute tasks on a frame by frame basis, while maintaining forward progress and fairly consistent frame durations. i.e. At 30 FPS, the average frame duration will be 33.33 ms.
Second (and this may be a simplification), let’s assume that each frame is broken into two major phases:
A. Rendering, Physics, and all other ‘internal’ stuff.
B. User defined event (listeners), timers, and other script handling.
Third, lets agree that Corona is single-threaded and if either phase A or phase B takes ‘too long’, then the frame duration average will be breached. i.e. If there is too much work, a frame will take longer than 33.33 ms to complete.
Having assumed all of the above, there are three ways that phase B can be implemented:
-
Each listener, timer, … category is bundled.
-
All listeners, timers, handlers are homogeneously queued.
-
A hybrid of 1 + 2
In case #1 (the most common implementation I’ve seen and the easiest to code), the engine/sdk iterates over a fixed and immutably ordered list of tasks each frame. That is, it handles all queued event listener, timers, etc by groups.
-
First it may handle all enterFrame listeners.
-
Second it may handle all collision handlers.
-
Third all touch handlers.
-
Fourth all timers,
-
etc…
In case #2, all event listeners, timers, etc are queued for handling based on when they are detected ‘ready to execute’ and the engine/sdk then iterates over a list like this:
-
timer 1
-
enterframe 1
-
enterframe 2
-
touch 1
-
timer 2
-
…
Some Things I Know For Sure
If the above is a little hand-wavy for you I apologize, but again I haven’t seen the internals of the engine. That said I’ve done extensive exploratory work trying to answer this question in the past. Things I do know:
- Timers are ordered against each other. i.e. If you schedule timer A then timer B with the same duration starting in the same frame, timer A will execute before timer B.
- enterFrame - Similarly enterFrame listeners are ordered and consistent in the same way.
- As far as I can tell, events are grouped, but I’m not 100% sure of the execution/handling order.
Your Example
In the example you gave, your:
- enterFrame listener will execute every frame.
- The timer will execute every 3rd or 4th frame if FPS is set to 30FPS.
- The timer will NEVER suspend executing code.
Special Timer Times
Please note: There is are some things you need to be aware of with regards to timers and times used for them:
- time == 100 - Your example, will execute approximately every 3rd or 4th frame when FPS set to 30.
- time == 1 - This is effectively saying, “execute this in the next frame”.
- time == 0 - This is effectively saying, “execute this right now”. (I have to double check this, but IIRC this was the outcome of my prior tests.)