Command Line Arguments in UnrealScript

Our first example demonstrates how to handle command-line arguments in UnrealScript. Command-line arguments are a common way to parameterize execution of programs.

class CommandLineArguments extends Commandlet;

function int Main(string[] Args)
{
    local string ArgsWithProg, ArgsWithoutProg, Arg;
    local int i;

    // In UnrealScript, we don't have direct access to the program name
    // So we'll simulate it by adding it to the beginning of our args
    ArgsWithProg = "CommandLineArguments";
    for (i = 0; i < Args.Length; i++)
    {
        ArgsWithProg $= " " $ Args[i];
    }

    // ArgsWithoutProg is just our original Args array
    ArgsWithoutProg = "";
    for (i = 0; i < Args.Length; i++)
    {
        if (i > 0)
        {
            ArgsWithoutProg $= " ";
        }
        ArgsWithoutProg $= Args[i];
    }

    // You can get individual args with normal indexing
    if (Args.Length > 2)
    {
        Arg = Args[2];
    }
    else
    {
        Arg = "Not enough arguments";
    }

    `log("ArgsWithProg: " $ ArgsWithProg);
    `log("ArgsWithoutProg: " $ ArgsWithoutProg);
    `log("Arg: " $ Arg);

    return 0;
}

To experiment with command-line arguments in UnrealScript, you would typically set up your development environment to run the script with arguments. The exact method might vary depending on your UnrealScript environment.

$ YourUnrealScriptEnvironment CommandLineArguments a b c d
ArgsWithProg: CommandLineArguments a b c d       
ArgsWithoutProg: a b c d
Arg: c

Note that UnrealScript doesn’t have built-in support for command-line arguments in the same way as some other languages. This example simulates similar behavior, but in a real UnrealScript environment, you might need to use different methods to pass and retrieve arguments, depending on how your script is being executed.

Next, we’ll look at more advanced ways to process input in UnrealScript.