godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

ResourceArray preloadDirectory(constPath: String, constExtension: String) const # preload folder at compile time #3933

Open comods opened 2 years ago

comods commented 2 years ago

Describe the project you are working on

When doing Game Jams, artists should just be able to drop .png files into an folder and Godot should preload all ".png"s from the folder into an array for instancing later.

Describe the problem or limitation you are having in your project

During a Game Jam artists can't add new art.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

ResourceArray preloadDirectory(constPath: String, constExtension: String) const

This should do all the Godot Editor tricks that preload(constPathFileExt: String) does like

  1. Bundles files at compile time like preload(filename) was called on each filename.
  2. Not be displayed as an Orphaned Resource (Menu Project -> Tools -> Orphan Resource Explorer)
  3. Using the FileSystem to rename a folder should also edit all preloadDirectory paths in script files to the new name. (Renaming files should edit preload paths too)

You want the "constExtension: String" argument (without the preceding ".") so that you don't preload ".import" files.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Setup project for Game Jam.

const art = preloadDirectory("res://art/", "png")
func _ready:
    var count = art.size()
    if count > 0:
        for node in get_node("FoeGroup").get_children():
            var i = randi() % count
            node.texture = art[i]

Artists can now add png files to the art folder and they will be preloaded and used without any other changes to the project.

If this enhancement will not be used often, can it be worked around with a few lines of script?

Can't do Godot Editor tricks that preload(constPathFileExt: String) does.

Is there a reason why this should be core and not an add-on in the asset library?

For Game Jams and Prototyping, having this in core would improve work flow and conversion rate of new users. Why should new users have to learn the Asset system or learn to use Directory command chain (that warns new users you can mess it up without multiple correct error checks) just to preload a folder into an array?

Calinou commented 2 years ago

Did you check the ResourcePreloader node? It can be used to preload resources from the editor, without requiring anyone to edit a script to preload a new resource.

comods commented 2 years ago

image The ResourcePreloader node will not let you link to a folder nor auto-import any new files added to that folder.

You can also preload resources. Unlike load, this function will read the file from disk and load it at compile-time. As a result, you cannot call preload with a variable path: you need to use a constant string.

ResourcePreloader.add_resource(path, preload(path)) In a script you still need to use preload to get the resource to add to ResourcePreloader, but there is no preload(path) that returns an array of the preloads of all non .import files in that folder.

comods commented 2 years ago
  1. Godot project displays preloadDirectory("art").size()
  2. CI re-compiles godot project.
  3. Opening index.html shows 0.
  4. Artist drops new.pngs into the art folder.
  5. CI re-compiles godot project.
  6. Opening index.html shows 1.

Note: No scene, json, tres, nor any other file file was edited; only a new file was added. (Besides compiler auto-import magic which may update some files that humans are not suppose to edit directly.)

Note: There are no git merge conflicts when the art team just adds a file.