Panic in UnrealScript

Our example demonstrates how to handle unexpected errors or critical situations in UnrealScript. In UnrealScript, we don’t have a direct equivalent to panic, but we can use assert for similar purposes.

class PanicExample extends Object;

function CheckCondition()
{
    // We'll use assert throughout this site to check for
    // unexpected errors. This is the only program on the
    // site designed to fail an assertion.
    assert(false, "A problem occurred");

    // A common use of assert is to abort if a function
    // encounters an unexpected condition. Here's an example
    // of asserting if we get an unexpected error when trying
    // to create a new object.
    local Object NewObj;
    NewObj = new class'Object';
    assert(NewObj != None, "Failed to create new object");
}

defaultproperties
{
}

Running this code will cause an assertion failure, print an error message, and potentially crash the game or editor, depending on how assertions are configured in the project.

When the first assertion in CheckCondition fails, the function execution will stop, and the rest of the code won’t be reached. If you’d like to see the function try to create a new object, comment out the first assertion.

In UnrealScript, assertion failures typically result in error messages in the log or on-screen, and may halt execution depending on project settings. The exact output format can vary, but it might look something like this:

Critical Error: Assertion failed: A problem occurred
    Function PanicExample.CheckCondition
    File: PanicExample.uc
    Line: 8

Note that unlike some languages which use exceptions for handling many errors, in UnrealScript it’s common to use return values, boolean flags, or specific error-handling functions to manage and respond to error conditions. Assertions are typically used for detecting programmer errors or impossible situations, rather than for regular error handling.

In UnrealScript, you might also use the log function for non-critical error reporting:

if (SomeErrorCondition)
{
    `log("An error occurred: " $ ErrorMessage);
}

This allows for error reporting without necessarily halting execution, which can be useful during development and debugging.