Godot Version
4.5.1
Question
This is more-so a general question about logistics.
Say I’m making a game with a vast amount of Enemy types, stored as individual scenes in a folder at res://scenes/entity/enemy
If I wanted to create a system that randomly chooses one of the Enemies stored in the folder to spawn, I’d need to create some sort of Array or Dictionary to store all the scenes
Something along the lines of
# Using a Dictionary
var enemy_scenes_dict: Dictionary = {
"basic_enemy": (scene_path_1),
"ranged_enemy": (scene_path_2),
#etc ...
}
# Using an Array
var enemy_scenes_array: Array = [
(scene_path_1),
(scene_path_2),
#etc ...
]
This seems okay until you decide that you want to create brand new enemies. You then have to remember to add them to the Array or Dictionary every single time.
I’m curious: Is there a more automated or modular way of accessing all of the files located inside of a specific folder in scripts?
For example if I wanted my system to automatically grab all the enemies inside of the res://scenes/entity/enemy folder and append them to an Array (see below code snippet), what would I need to do?
var enemy_scenes_array: Array = []
func _ready():
populate_scenes_array()
# Expecting this to print out a full array
print(enemy_scenes_array)
func populate_scenes_array():
# Do something
Just to clarify, my example scenario is exactly that, an example, so I’m not necessarily looking for specifically the best way to get an enemy spawning system up-and-running. In theory, this question and any answers given should apply to a few scenarios, but I figured that this scenario would be the easiest to use as an example.