Scheduler
The scheduler is the part of the game engine that decides which thread runs at a certain point in time. It has the ability to pause a running thread, move it to the back of the running queue and start a new thread.
Threads
A new thread can be started in a script by using spawn, execVM and exec commands. A script or function started this way will run in a scheduled environment while a function which is started by call will run unscheduled within the executing instance.
Scheduled Environment
Running code in a scheduled environment starts a new thread. The executing instance will not wait for the result of scheduled code. It will just continue with its strand of execution and there is no way to predict when those scheduled code will have been finished it's excecution (no prediction but you can measure it of course). The advantage of scheduled code is that it runs parallel (fire and forget) and that you are able to run code with a huge performance impact (nearly) without slowing down the game.
Where code starts scheduled
- init.sqf, initServer.sqf, initPlayerLocal.sqf, initPlayerServer.sqf
- functions with postInit attribute
Unscheduled Environment
Often also called non-scheduled environment but means the same and you will find both terms used in this wiki. The unscheduled environment runs (as described above) in the executing instance. The executing instance waits until the called function is finished. This ensures the execution order and is the fastest way for scripters to execute their code. The disadvantage is that a called function can halt or slow down the game if the function has a high performance consumption. Therefore those functions should be spawned and thereby run in a scheduled environment.
A scheduled script runs for exactly 3ms before it is put in suspension to be resumed on the next frame, again for another 3ms and so on until the script is finished. The amount of suspension depends on FPS. At 20 FPS the duration of suspension for example is 50ms. This means that if scheduled script cannot be completed under 3ms, the execution can stretch for undefined amount of time, subject to engine load, FPS and other non scheduled scripts running at the same time. A while true loop with sleep started in scheduled environment therefore has little chance to follow with exact interval.
Scheduled scripts always start with slight delay, subject to engine load.
Where code starts unscheduled
- triggers
- functions with preInit attribute
- FSM-conditions
- object Init Event Handlers
- object initialization fields
Loops
A while do loop will be limited to 10,000 iteration in non-scheduled environment. In scheduled environment such limit does not apply.
Suspension
Suspension like with sleep or uiSleep is forbidden in unscheduled environment. If you need to use this then do it in scheduled environment.