You can prevent instantiating an autoload multiple times using a static variable to store a reference to the singleton instance. Here's an example of how you can do this:class_name MySingleton extends Node
static var singleton_instance: MySingleton = null
func _init() -> void:
if singleton_instance == null:
singleton_instance = self
else:
printerr("Trying to create another instance of MySingleton. Deleting it.")
queue_free()
This is the closest you can get in GDScript, as you cannot override the new()
method of classes or the instantiate()
method of scenes. So, you cannot prevent instantiating the class a second time; you can only delete the second instance if it happens.