Timeouts in UnrealScript

Timeouts are important for programs that connect to external resources or that otherwise need to bound execution time. Implementing timeouts in UnrealScript can be achieved using timers and events.

class TimeoutExample extends Object;

var string Result1, Result2;
var bool bTimedOut1, bTimedOut2;

function Init()
{
    // Start our simulated long-running operations
    SetTimer(2.0, false, 'Operation1');
    SetTimer(2.0, false, 'Operation2');

    // Set up our timeouts
    SetTimer(1.0, false, 'Timeout1');
    SetTimer(3.0, false, 'Timeout2');
}

function Operation1()
{
    Result1 = "result 1";
    CheckResult1();
}

function Operation2()
{
    Result2 = "result 2";
    CheckResult2();
}

function Timeout1()
{
    bTimedOut1 = true;
    CheckResult1();
}

function Timeout2()
{
    bTimedOut2 = true;
    CheckResult2();
}

function CheckResult1()
{
    if (Result1 != "")
    {
        `log("Operation 1: " $ Result1);
    }
    else if (bTimedOut1)
    {
        `log("Operation 1: timeout");
    }
}

function CheckResult2()
{
    if (Result2 != "")
    {
        `log("Operation 2: " $ Result2);
    }
    else if (bTimedOut2)
    {
        `log("Operation 2: timeout");
    }
}

defaultproperties
{
}

In this UnrealScript example, we’re simulating long-running operations using timers. The Init function starts two operations and sets up their corresponding timeouts.

For our example, suppose we’re executing an external call that returns its result after 2 seconds. We simulate this with the Operation1 and Operation2 functions, which are called after a 2-second delay.

The timeout mechanism is implemented using additional timers. If the timeout timer fires before the operation completes, it sets a flag indicating that the operation has timed out.

The CheckResult1 and CheckResult2 functions are responsible for checking whether the operation completed successfully or timed out. They’re called both when the operation completes and when the timeout occurs.

In the first case (Operation1), we set a 1-second timeout. Since the operation takes 2 seconds, it will time out.

In the second case (Operation2), we set a 3-second timeout. The operation takes 2 seconds, so it will complete successfully.

To run this program, you would typically integrate it into a larger UnrealScript project and call the Init function when appropriate. The results would be logged to the Unreal Engine log.

Running this program would show the first operation timing out and the second succeeding:

Operation 1: timeout
Operation 2: result 2

This example demonstrates how to implement basic timeout functionality in UnrealScript. While it doesn’t have built-in concurrency primitives like channels or goroutines, we can achieve similar behavior using UnrealScript’s timer system.