Goroutines in UnrealScript

Based on the instructions, here is the markdown format suitable for Hugo, translating the given Go code example to UnrealScript.

A **goroutine** is a lightweight thread of execution.

Suppose we have a function call `f(s)`. Here’s how we’d call that in the usual way, running it synchronously.

```unrealscript
class GoroutineExample extends Object;

function f(string from)
{
    local int i;
    for (i = 0; i < 3; i++)
    {
        `log(from @ ":" @ i);
    }
}

static function Main()
{
    // Direct function call, runs synchronously
    f("direct");

    // Simulating concurrent execution using threads in UnrealScript
    Spawn(class'GoroutineThreadExample').Init(f, "goroutine");
    Spawn(class'GoroutineThreadExample').Init(function(string msg) {
        `log(msg);
    }, "going");

    // Wait for a moment to let threads finish (not robust, for illustration)
    Sleep(1.0);
    `log("done");
}

DefaultProperties
{
}

We have created a class GoroutineExample with a function f which prints a message three times.

To invoke this function in a concurrent manner (similar to goroutines), we use a helper class to manage threading:

class GoroutineThreadExample extends Actor;

var function PointerDelegate;
var string DelegateParam;

function Init(function InPointerDelegate, string InDelegateParam)
{
    PointerDelegate = InPointerDelegate;
    DelegateParam = InDelegateParam;
    SetTimer(0.1, false);
}

function Timer()
{
    PointerDelegate(DelegateParam);
    Destroy();
}

DefaultProperties
{
    bHidden=true
}

GoroutineThreadExample is a helper class to simulate concurrent execution. It uses a Timer event to call the function after initialization.

When we run this program, we would see the output of the blocking call first, then the output from the concurrent execution. The order might look like:

direct : 0
direct : 1
direct : 2
goroutine : 0
going
goroutine : 1
goroutine : 2
done

Next, we’ll look at a complement to this simulation in concurrent programs: inter-thread communication.