I am creating a dispenser that should be able to spawn anything (enemies, power-ups, etc.)
Fo now, I have this code:
extends Node2D
tool
export(float) var delay: float = 1.0
export(PackedScene) var dispense: PackedScene
func _ready() -> void:
$Timer.wait_time = delay
$Timer.start()
func _on_Timer_timeout() -> void:
var newElement = dispense.new() # This doesn't work
func _get_configuration_warning() -> String:
if dispense == null:
return "%s needs an element to dispense" % name
return ""
The dispense
attribute is the thing that is supposed to be emmited every delay
seconds.
How do I instanciate a new element based on its type? I tried element.new()
, but it doesn't work (Invalid call. Nonexistent function 'new' in base 'PackedScene'. when interpreted).
How do I instanciate it?