Timers in UnrealScript
In UnrealScript, timers are handled differently compared to Go. Instead of using channels, UnrealScript provides built-in timer functionality through the SetTimer
function.
Here’s how the example works:
We define two float variables to store the durations for our timers.
In the
Init
function (which is similar tomain
in Go), we set up our timers:SetTimer(Timer1Duration, false, 'OnTimer1Fired')
sets a timer that will call theOnTimer1Fired
function afterTimer1Duration
seconds. Thefalse
parameter means it will only fire once.- We do the same for Timer2.
To demonstrate cancelling a timer, we immediately call
ClearTimer('OnTimer2Fired')
to stop Timer2 before it fires.We use
Sleep(2.0)
to pause execution for 2 seconds, giving Timer2 a chance to fire if it wasn’t stopped.The
OnTimer1Fired
andOnTimer2Fired
functions are what get called when the timers fire. They simply log a message.
To run this code, you would typically place it in a script file in your Unreal project, and it would be executed as part of the game’s logic.
Note that UnrealScript doesn’t have direct equivalents to Go’s channels or goroutines. The timer system in UnrealScript is more integrated with the game engine’s tick system.
The first timer will fire ~2s after we start the program, but the second should be stopped before it has a chance to fire. The output would look something like this:
This example demonstrates how to use timers in UnrealScript for delayed execution and how to cancel timers before they fire.