Best way of getting rid of a circular dependency?
Asked Answered
C

4

0

I'm no coder, and at times I'm confused with the underlying concepts of object programming.

I have a function that spawns a scene as a child of the main scene:

func spawn_location(location):
	current_location = location.instantiate()
	main.add_child(current_location)

location is a parameter of the object calling the function:

@export var target_scene:PackedScene

everything works fine until i try to go back to a previously visited location, in wich case i'm thrown an error:

E 0:00:00:0669   _parse_ext_resource: res://DragAndDrop/droppable.tscn:11 - Parse Error: [ext_resource] referenced non-existent resource at: res://locations/morlako.tscn
  <C++ Source>   scene/resources/resource_format_text.cpp:162 @ _parse_ext_resource()

looking for clues online i found this is very likely caused by a circular dependency error (which i can't understand, but guess there's a good reason for)

Assuming the problem is that, what is the correct way of dealing with it? I've read stuff online, but it's mainly workarounds (like using the filepath, that i find unconfortable and dangerous)?

Christophe answered 19/11, 2023 at 9:38 Comment(0)
H
0

Where does the object calling this function live?

Hypopituitarism answered 19/11, 2023 at 9:54 Comment(0)
C
0

Hypopituitarism

the function is in "global.gd" that's an Autoload

the calling object is a subscene of the instanced subscene

Christophe answered 19/11, 2023 at 10:57 Comment(0)
H
0

Ah, I see how that's triggering the circular dependency error, since they both have each other's PackedScene directly referenced. It probably would function fine if Godot weren't so zealous about circular dependencies, but who knows. It's probably something the engine itself could improve. For now, Idk if you're going to be able to avoid a workaround. Filepath isn't necessarily that bad. You can do:
@export_file("*.tscn") var target_scene_file:String
and Godot should automatically update it if you move the scene file. It's not actually more fragile than directly serializing the PackedScene, because when you do that Godot still serializes it by filepath under the hood.

Or, you could have your scenes in global.gd in a Dictionary with an enum which is what your spawned scenes would pass to request a scene load.

Hypopituitarism answered 19/11, 2023 at 21:19 Comment(0)
C
0

Hypopituitarism

Thank you very much, i changed my scripting to:

@export_file("*.tscn") var target_scene_file:String

then:

func spawn_location(location_file):
	current_location = load(location_file).instantiate()
	main.add_child(current_location)

and it works.

Christophe answered 20/11, 2023 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.