Question about saving sub resources

Godot Version

4.3

Question

I’m trying to decide on certain data structures for a quest system in my game. I know I could do this with a dictionary and that it would work just fine, but I’m experimenting with the possibility of using quest_item resources which essentially are resources that store all of the information pertaining to a particular quest. That way I could build new quests working in the inspector rather than just creating a big dictionary.

I was hoping to be able to use these with a more general quest_resource and to be able to simply save the quest resource and have the status of all quests save with it. I know if the individual quests were also classes I could use .new() to save the status of instances, but is there any way to do that without having to give each quest a named class? As far as I can tell, .new() only works with named classes. Am I mistaken?

Thanks!

You can preload a script and call .new() on it. It is similar to a non-global class_name once preloaded

const QuestThing = preload("res://Quests/Quest1Part2.gd")
var quest: QuestThing = QuestThing.new()
1 Like

So just so I understand this correctly, if I need to create an instance of a quest where I intend to save the state of it, I could do what you suggested but instead of doing .new() on the variable definition I could instead do something in _ready() like:

func _ready() -> void:
if not quest:
quest = QuestThing.new()

Is that right?

Yes you could, the preload can be .new()'d anywhere in scope of the constant QuestThing.

Thanks! I think this is the missing information I needed!