Errors in UnrealScript

In UnrealScript, error handling is typically done through return values or by throwing exceptions. While UnrealScript doesn’t have a built-in error type like Go, we can create our own error handling mechanism. Here’s an example of how we might implement error handling in UnrealScript:

class ErrorExample extends Object;

// Define our custom error class
class CustomError extends Object;

var string Message;

function CustomError Init(string InMessage)
{
    Message = InMessage;
    return self;
}

// Our main function that might return an error
function bool F(int Arg, out int Result, out CustomError Error)
{
    if (Arg == 42)
    {
        Error = new class'CustomError'.Init("can't work with 42");
        return false;
    }
    
    Result = Arg + 3;
    return true;
}

// Predefined errors (similar to sentinel errors in Go)
var CustomError ErrOutOfTea;
var CustomError ErrPower;

function bool MakeTea(int Arg, out CustomError Error)
{
    if (Arg == 2)
    {
        Error = ErrOutOfTea;
        return false;
    }
    else if (Arg == 4)
    {
        // In UnrealScript, we can't easily wrap errors, so we'll just return a new error
        Error = new class'CustomError'.Init("making tea: can't boil water");
        return false;
    }
    return true;
}

function Init()
{
    local int i, Result;
    local CustomError Error;
    
    ErrOutOfTea = new class'CustomError'.Init("no more tea available");
    ErrPower = new class'CustomError'.Init("can't boil water");
    
    // Test F function
    for (i = 0; i < 2; i++)
    {
        if (F(i == 0 ? 7 : 42, Result, Error))
        {
            `log("F worked:" @ Result);
        }
        else
        {
            `log("F failed:" @ Error.Message);
        }
    }
    
    // Test MakeTea function
    for (i = 0; i < 5; i++)
    {
        if (MakeTea(i, Error))
        {
            `log("Tea is ready!");
        }
        else
        {
            if (Error == ErrOutOfTea)
            {
                `log("We should buy new tea!");
            }
            else if (Error == ErrPower)
            {
                `log("Now it is dark.");
            }
            else
            {
                `log("Unknown error:" @ Error.Message);
            }
        }
    }
}

defaultproperties
{
}

In this UnrealScript example, we’ve created a custom CustomError class to represent errors. The F and MakeTea functions return a boolean indicating success or failure, and use an out parameter to return any error.

We’ve also created sentinel errors ErrOutOfTea and ErrPower, similar to the Go example. However, UnrealScript doesn’t have built-in error wrapping or checking mechanisms like errors.Is, so we’ve simplified the error handling.

To use this code, you would typically call the Init function when the game starts or when you want to run the example. The output would be logged to the Unreal Engine log.

This approach to error handling in UnrealScript is more verbose than in Go, but it achieves a similar result of explicitly handling and checking for specific error conditions.