Directories in GDScript

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

Our first example demonstrates working with directories in the file system.

extends Node

func _ready():
    # Create a new sub-directory in the current working directory
    var dir = Directory.new()
    dir.make_dir("subdir")

    # When creating temporary directories, it's good practice to remove them later
    # We'll use a timer to simulate deferred cleanup
    var timer = Timer.new()
    timer.connect("timeout", self, "_on_cleanup_timer_timeout")
    timer.set_wait_time(5.0)  # 5 seconds delay
    timer.set_one_shot(true)
    add_child(timer)
    timer.start()

    # Helper function to create a new empty file
    create_empty_file("subdir/file1")

    # We can create a hierarchy of directories, including parents
    dir.make_dir_recursive("subdir/parent/child")

    create_empty_file("subdir/parent/file2")
    create_empty_file("subdir/parent/file3")
    create_empty_file("subdir/parent/child/file4")

    # List directory contents
    print("Listing subdir/parent")
    var files = []
    dir.open("subdir/parent")
    dir.list_dir_begin()
    var file_name = dir.get_next()
    while file_name != "":
        print(" ", file_name, dir.current_is_dir())
        file_name = dir.get_next()
    dir.list_dir_end()

    # Change the current working directory
    OS.chdir("subdir/parent/child")

    # Now we'll see the contents of subdir/parent/child when listing the current directory
    print("Listing current directory")
    dir.open(".")
    dir.list_dir_begin()
    file_name = dir.get_next()
    while file_name != "":
        print(" ", file_name, dir.current_is_dir())
        file_name = dir.get_next()
    dir.list_dir_end()

    # Change back to where we started
    OS.chdir("../../..")

    # We can also visit a directory recursively, including all its sub-directories
    print("Visiting subdir")
    visit_directory("subdir")

func create_empty_file(path):
    var file = File.new()
    file.open(path, File.WRITE)
    file.close()

func visit_directory(path):
    var dir = Directory.new()
    if dir.open(path) == OK:
        dir.list_dir_begin()
        var file_name = dir.get_next()
        while file_name != "":
            if dir.current_is_dir():
                print(" ", path + "/" + file_name, true)
                visit_directory(path + "/" + file_name)
            else:
                print(" ", path + "/" + file_name, false)
            file_name = dir.get_next()
        dir.list_dir_end()
    else:
        print("An error occurred when trying to access the path.")

func _on_cleanup_timer_timeout():
    var dir = Directory.new()
    dir.remove("subdir")

To run the program, save it as a script attached to a Node in your Godot project and run the scene.

This script demonstrates various operations with directories:

  1. Creating directories and files
  2. Listing directory contents
  3. Changing the current working directory
  4. Recursively visiting a directory tree

Note that GDScript, being designed for use within the Godot game engine, has some differences from general-purpose languages:

  • It uses Godot’s built-in Directory and File classes for file system operations.
  • There’s no direct equivalent to Go’s defer, so we use a Timer to simulate delayed cleanup.
  • Error handling is typically done by checking return values rather than using exceptions.

The output will be similar to the Go example, listing the created directories and files.

Remember to handle file system operations carefully in a real game project, as file access can be restricted on some platforms.