Line Filters in UnrealScript

A line filter is a common type of program that reads input, processes it, and then prints some derived result. In UnrealScript, we can create a similar functionality using string manipulation and file I/O operations.

Here’s an example line filter in UnrealScript that writes a capitalized version of all input text:

class LineFilter extends Object;

var FileReader Reader;
var FileWriter Writer;

function ProcessLines()
{
    local string Line;

    Reader = new class'FileReader';
    Writer = new class'FileWriter';

    if (Reader.OpenFile("Input.txt"))
    {
        Writer.OpenFile("Output.txt");

        while (Reader.ReadLine(Line))
        {
            Line = Caps(Line);
            Writer.WriteLine(Line);
        }

        Reader.Close();
        Writer.Close();
    }
    else
    {
        `log("Error: Unable to open input file");
    }
}

defaultproperties
{
}

In this UnrealScript version:

  1. We define a LineFilter class that extends Object.

  2. We use FileReader and FileWriter classes for file I/O operations, which are similar to the buffered scanner in the original example.

  3. The ProcessLines function reads lines from an input file, capitalizes them, and writes them to an output file.

  4. We use a while loop to read lines until the end of the file, similar to the for scanner.Scan() loop in the original.

  5. The Caps function is used to capitalize each line, which is equivalent to strings.ToUpper in the original.

  6. Error handling is done by checking if the file was successfully opened.

To use this line filter:

  1. Create an input file named “Input.txt” with some lowercase text.

  2. In your game or editor, create an instance of the LineFilter class and call the ProcessLines function.

  3. After execution, you’ll find an “Output.txt” file with the capitalized version of the input.

Note that UnrealScript doesn’t have direct console input/output like Go, so we’re using file I/O as an alternative. Also, error handling and logging are simplified for this example.