Exit in UnrealScript

Here’s an idiomatic UnrealScript example demonstrating the concept of exiting a program:

class ExitExample extends Object;

event Init()
{
    local PlayerController PC;

    // This line will be executed
    `log("Initializing ExitExample");

    // This line will not be executed due to the ConsoleCommand call
    `log("This line will not be printed");

    // Get the first PlayerController
    PC = class'WorldInfo'.static.GetWorldInfo().GetALocalPlayerController();

    // Exit the game
    PC.ConsoleCommand("exit");
}

defaultproperties
{
}

This UnrealScript example demonstrates the concept of exiting a program, similar to the Go example provided. Here’s an explanation of the code:

  1. We define a class called ExitExample that extends from Object.

  2. We use the Init() event, which is called when the object is initialized.

  3. Inside Init(), we first log a message to demonstrate that the code is running.

  4. We then attempt to log another message, but this line will not be executed due to the exit command.

  5. We get a reference to the PlayerController using the WorldInfo class.

  6. Finally, we use the ConsoleCommand method of the PlayerController to execute the “exit” command, which will close the game.

In UnrealScript, there isn’t a direct equivalent to os.Exit(). Instead, we use the ConsoleCommand method to execute the “exit” command, which effectively closes the game.

To use this script in an Unreal Engine project:

  1. Create a new script file named ExitExample.uc in your project’s Scripts folder.
  2. Paste the above code into the file.
  3. Compile the script within the Unreal Editor.
  4. To test the script, you would typically create an instance of this class or call its Init() method from another part of your game logic.

Note that in a real game scenario, you would usually want to perform cleanup operations or save game state before exiting. This example is simplified to demonstrate the concept of exiting the program.

Also, be aware that abruptly exiting the game like this may not be the best user experience. In a real game, you might want to show a confirmation dialog or return to a main menu instead of directly exiting.