Tutorial - Time
Sometimes you need something to happen with a delay or to keep happening continuously in a loop. First option is to use the Delay instruction, but you can as well use Time Game Logic.
This object allows you to perform something after a give amount of time (you can specify whether real time or game time). Because you can set how many times the waiting and event execution should repeat you can make a loop (an infinite one, if you specify -1) as number of repeats.
Another important thing to know is that the Time Game Logic doesn’t run when it is disabled. So when you want something to happen five seconds after,say , entering a Trigger Zone, you create Time Game Logic, set the time to 5s and disable it. Then you enable it when the player enters the trigger zone… and that’s it.
Here you can see an infinite loop that always waits few seconds, then executes On Trigger (which makes the lamp go on or off) and then waits again.
There are two Time Game Logic objects located in the corner of the wooden railing. One is set to execute in 10 seconds. Because it is enabled by default it will execute 10 seconds after that game starts. Then it enables the second Time Game Logic, which will perform five “ticks”, each 0.5s apart. On every execute we show a countdown variable's current value and reduce it by 1.
When a player steps in front of the armor we use Delay script tile to gradually change players equipment.
When you set something to trigger On game start or right away by having the Time Game Logic enabled with no delay set, please bear in mind that game start doesn’t equal the moment when you see your scene after loading. The game starts in the background and objects are being initialized first before the camera is enabled and this can take anything from few seconds to several dozens of seconds (when starting a dedicated server session).
One of the worst things you can do when it comes to game performance is to perform operations within a looped Time Game Logic with a very short wait time. The shorter the time between each execute and more heavy the script you perform, the more significant impact on game's performance it has. If you are not sure whether your game runs slowly because of your scripting, be sure to check the performance profiler.