Directories in UnrealScript

Here’s the translation of the Go code to UnrealScript, along with explanations in Markdown format suitable for Hugo:

class DirectoryOperations extends Object;

// Helper function to check for errors
function CheckError(bool bSuccess)
{
    if (!bSuccess)
    {
        `log("An error occurred");
        return;
    }
}

// Helper function to create an empty file
function CreateEmptyFile(string FileName)
{
    local bool bSuccess;
    bSuccess = class'FileWriter'.static.WriteStringToFile(FileName, "");
    CheckError(bSuccess);
}

// Main function to demonstrate directory operations
function Execute()
{
    local array<string> FileList;
    local string FileName;
    local bool bIsDirectory;

    // Create a new sub-directory in the current working directory
    CheckError(CreateDirectory("subdir"));

    // Create some files and directories
    CreateEmptyFile("subdir/file1");
    CheckError(CreateDirectory("subdir/parent/child"));
    CreateEmptyFile("subdir/parent/file2");
    CreateEmptyFile("subdir/parent/file3");
    CreateEmptyFile("subdir/parent/child/file4");

    // List directory contents
    `log("Listing subdir/parent");
    class'FileManager'.static.FindFiles(FileList, "subdir/parent/*.*");
    foreach FileList(FileName)
    {
        bIsDirectory = class'FileManager'.static.DirectoryExists(FileName);
        `log(" " $ FileName $ " " $ bIsDirectory);
    }

    // Change current working directory
    CheckError(class'FileManager'.static.SetCurrentDirectory("subdir/parent/child"));

    // List contents of the new current directory
    `log("Listing subdir/parent/child");
    class'FileManager'.static.FindFiles(FileList, "*.*");
    foreach FileList(FileName)
    {
        bIsDirectory = class'FileManager'.static.DirectoryExists(FileName);
        `log(" " $ FileName $ " " $ bIsDirectory);
    }

    // Change back to the original directory
    CheckError(class'FileManager'.static.SetCurrentDirectory("../../.."));

    // Recursively visit all files and directories
    `log("Visiting subdir");
    VisitDirectory("subdir");
}

// Function to recursively visit directories
function VisitDirectory(string Path)
{
    local array<string> FileList;
    local string FileName;
    local bool bIsDirectory;

    class'FileManager'.static.FindFiles(FileList, Path $ "/*.*");
    foreach FileList(FileName)
    {
        bIsDirectory = class'FileManager'.static.DirectoryExists(FileName);
        `log(" " $ FileName $ " " $ bIsDirectory);

        if (bIsDirectory)
        {
            VisitDirectory(FileName);
        }
    }
}

This UnrealScript code demonstrates various directory operations similar to the original example. Here’s a breakdown of the operations:

  1. We define helper functions CheckError and CreateEmptyFile to handle error checking and file creation.

  2. The Execute function is the main entry point, demonstrating different directory operations:

    • Creating directories using CreateDirectory
    • Creating empty files using CreateEmptyFile
    • Listing directory contents using FindFiles
    • Changing the current working directory using SetCurrentDirectory
  3. The VisitDirectory function recursively visits all files and directories, similar to the filepath.WalkDir functionality in the original example.

Note that UnrealScript doesn’t have built-in functions for all the operations shown in the Go example. We’ve used equivalent or similar functions from the FileManager class, which is part of the Unreal Engine’s file system API.

To run this code, you would typically integrate it into an Unreal Engine project and call the Execute function from an appropriate place in your game logic.

Remember that file and directory operations in UnrealScript are often restricted due to the sandboxed environment of games. In a real-world scenario, you might need to consider using the appropriate file system APIs provided by the Unreal Engine for your specific use case.