v4.4.1.stable.official [49a5bc7b6]
Hi! I need some help in thinking about a factory design. The problem:
I have a directory with scenes. Each scene is a button for a hotbar. My hotbar is responsible for adding the buttons to itself and call their animation afterwards.
Right now, I add every scene manually in the script. I was wondering, if I can write a factory for reading the directory and looping through all files, create a variable for every scene and assign the scene to that variable.
Scoping is a problem, of course. Can Godot dynamically create variables outside a function scope?
Is this approach flawed?
extends Control
var btn_power = preload("res://scenes/buttons/btn_power.tscn")
var btn_storage = preload("res://scenes/buttons/btn_storage.tscn")
var btn_satellite = preload("res://scenes/buttons/btn_satellite.tscn")
var btn_bigpack = preload("res://scenes/buttons/btn_bigpack.tscn")
func _ready() -> void:
$Hotbar.add_child(btn_power.instantiate(), true)
$Hotbar.add_child(btn_storage.instantiate(), true)
$Hotbar.add_child(btn_satellite.instantiate(), true)
$Hotbar.add_child(btn_bigpack.instantiate(), true)
# call add_module("btn_power") from somewhere else...
func add_module(mod_str: String) -> void:
var mod: Node = $Hotbar.get_node(mod_str)
if mod == null:
print("[HOTBAR] Could not find module with name %s." % mod_str)
return
mod.animate_in()