Tickers in UnrealScript

Tickers are used when you want to do something repeatedly at regular intervals. Here’s an example of a ticker that ticks periodically until we stop it.

class TickerExample extends Object;

var float TickInterval;
var bool bShouldStop;

function BeginPlay()
{
    TickInterval = 0.5; // 500 milliseconds
    SetTimer(TickInterval, true, 'Tick');
    
    // Stop the ticker after 1.6 seconds
    SetTimer(1.6, false, 'StopTicker');
}

function Tick()
{
    if (!bShouldStop)
    {
        `Log("Tick at" @ WorldInfo.TimeSeconds);
    }
}

function StopTicker()
{
    bShouldStop = true;
    ClearTimer('Tick');
    `Log("Ticker stopped");
}

defaultproperties
{
    bShouldStop=false
}

In UnrealScript, we use the SetTimer function to create a repeating timer that acts as our ticker. The Tick function is called every 500 milliseconds (0.5 seconds).

We set another timer to stop the ticker after 1.6 seconds, simulating the behavior of the original example.

The bShouldStop variable is used to control when the ticker should stop. When StopTicker is called, it sets this flag to true and clears the timer.

When we run this script, the ticker should tick 3 times before we stop it.

Tick at 0.5
Tick at 1.0
Tick at 1.5
Ticker stopped

Note that UnrealScript doesn’t have built-in concurrency like goroutines, so we’ve adapted the example to use UnrealScript’s timer system. The WorldInfo.TimeSeconds is used to get the current game time for logging purposes.

This example demonstrates how to create a repeating timer in UnrealScript and how to stop it after a certain duration.