Command Line Flags in UnrealScript

Here’s the translation of the Go command-line flags example to UnrealScript, formatted in Markdown suitable for Hugo:

Our example demonstrates how to use command-line arguments in UnrealScript. While UnrealScript doesn’t have a built-in flag parsing system like some other languages, we can implement a basic argument parsing system.

class CommandLineFlags extends Commandlet;

var string Word;
var int Numb;
var bool Fork;
var string Svar;

function int Main(string Parms)
{
    local array<string> Args;
    local string Arg;
    local int i;

    // Parse command line arguments
    Split(Parms, " ", Args);

    for (i = 0; i < Args.Length; i++)
    {
        Arg = Args[i];
        if (Left(Arg, 6) ~= "-word=")
        {
            Word = Mid(Arg, 6);
        }
        else if (Left(Arg, 6) ~= "-numb=")
        {
            Numb = int(Mid(Arg, 6));
        }
        else if (Arg ~= "-fork")
        {
            Fork = true;
        }
        else if (Left(Arg, 6) ~= "-svar=")
        {
            Svar = Mid(Arg, 6);
        }
    }

    // Print the parsed values
    `log("word:" @ Word);
    `log("numb:" @ Numb);
    `log("fork:" @ Fork);
    `log("svar:" @ Svar);

    // Print any remaining arguments
    `log("Remaining args:");
    for (i = 0; i < Args.Length; i++)
    {
        if (Left(Args[i], 1) != "-")
        {
            `log(Args[i]);
        }
    }

    return 0;
}

defaultproperties
{
    Word="foo"
    Numb=42
    Fork=false
    Svar="bar"
}

In this UnrealScript version:

  1. We define a class that extends Commandlet, which allows it to be run from the command line.

  2. We declare variables to store our flag values, with default values set in the defaultproperties block.

  3. The Main function is the entry point for the commandlet. It takes a single string parameter Parms which contains all the command-line arguments.

  4. We split the Parms string into an array of individual arguments.

  5. We then iterate through the arguments, parsing them based on their prefixes. This is a simple implementation and could be expanded for more robust parsing.

  6. After parsing, we print out the values of our flags and any remaining arguments.

To use this commandlet, you would typically run it from the UnrealEd command line or a batch file, like this:

UDK.exe MyGame CommandLineFlags -word=opt -numb=7 -fork -svar=flag extraArg1 extraArg2

This would output:

word: opt
numb: 7
fork: True
svar: flag
Remaining args:
extraArg1
extraArg2

Note that UnrealScript doesn’t have built-in support for generating help text or handling unknown flags as smoothly as some other languages. You would need to implement these features manually if needed.

Also, keep in mind that the exact method of running commandlets can vary depending on your Unreal Engine setup and project configuration.