Context in UnrealScript

Here’s the translation of the Go code to UnrealScript, formatted in Markdown suitable for Hugo:

Our example demonstrates how to use context-like functionality in UnrealScript for handling HTTP-like requests. While UnrealScript doesn’t have built-in HTTP server capabilities or a direct equivalent to Go’s context.Context, we can simulate similar behavior using UnrealScript’s event system and timers.

class ContextExample extends Actor;

var float RequestTimeout;
var bool bRequestCancelled;

event BeginPlay()
{
    Super.BeginPlay();
    RequestTimeout = 10.0; // 10 seconds timeout
}

function SimulateRequest()
{
    `log("server: hello handler started");
    SetTimer(RequestTimeout, false, 'HandleRequest');
}

function HandleRequest()
{
    if (!bRequestCancelled)
    {
        `log("hello");
    }
    else
    {
        `log("server: request cancelled");
    }
    `log("server: hello handler ended");
}

function CancelRequest()
{
    bRequestCancelled = true;
    ClearTimer('HandleRequest');
    `log("server: request cancelled");
}

defaultproperties
{
    bRequestCancelled=false
}

In this UnrealScript example, we’ve created a class called ContextExample that simulates handling an HTTP-like request with a timeout and cancellation mechanism.

The SimulateRequest function starts the request handling process. It sets a timer for RequestTimeout seconds (10 in this case) to simulate the work being done.

The HandleRequest function is called when the timer expires. It checks if the request has been cancelled. If not, it prints “hello”. If the request was cancelled, it logs a cancellation message.

The CancelRequest function can be called to simulate cancelling the request before it completes. It sets the bRequestCancelled flag and clears the timer.

To use this in an UnrealScript environment:

  1. Create an instance of ContextExample in your game.
  2. Call SimulateRequest() to start a request.
  3. You can call CancelRequest() at any time to cancel the ongoing request.

This example demonstrates how to handle long-running operations with the ability to cancel them, similar to the context usage in the original example. However, it’s important to note that UnrealScript’s environment and capabilities are quite different from Go’s, so this is an approximation of the concept rather than a direct translation.