Select in UnrealScript

Our example demonstrates how to use multiple channels and select from them. This is a powerful feature in UnrealScript for handling concurrent operations.

class SelectExample extends Object;

var array<String> Messages;
var int ReceivedCount;

function Initialize()
{
    Messages.Length = 2;
    ReceivedCount = 0;
}

function Tick(float DeltaTime)
{
    local int i;
    
    super.Tick(DeltaTime);

    // Simulate receiving messages after some delay
    if (ReceivedCount == 0 && WorldInfo.TimeSeconds >= 1.0)
    {
        Messages[0] = "one";
        ReceivedCount++;
    }
    else if (ReceivedCount == 1 && WorldInfo.TimeSeconds >= 2.0)
    {
        Messages[1] = "two";
        ReceivedCount++;
    }

    // Check for received messages
    for (i = 0; i < 2; i++)
    {
        if (Messages[i] != "")
        {
            `log("received " $ Messages[i]);
            Messages[i] = "";
        }
    }

    // Stop ticking after receiving both messages
    if (ReceivedCount >= 2)
    {
        SetTickIsDisabled(true);
    }
}

defaultproperties
{
    bTickIsDisabled=false
}

In this example, we’re simulating the behavior of selecting from multiple channels. UnrealScript doesn’t have built-in channels or a select statement, so we’re using arrays and the Tick function to achieve similar functionality.

We create an array Messages to hold our “channel” values, and use ReceivedCount to keep track of how many messages we’ve received.

The Tick function is called every frame and serves as our main loop. In it, we:

  1. Simulate receiving messages after a delay (1 second for the first message, 2 seconds for the second).
  2. Check for received messages and log them.
  3. Stop ticking after we’ve received both messages.

To run this code, you would typically include it in an actor or game object in your Unreal Engine project. The engine will automatically call the Tick function each frame.

Note that the total execution time would be about 2 seconds, similar to the original example, as both “channels” are checked concurrently in each tick.

This approach doesn’t provide true concurrency like goroutines, but it simulates the behavior in a way that fits with UnrealScript’s event-driven paradigm.