Cyclic Preload instancing scenes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By VictorSeven
:warning: Old Version Published before Godot 3 was released.

Hello all,

I am working in a project with Godot. The thing is that I have three scenes that are very similar between them. Basically, they have the same node structure, and mostly the same content in the scripts. So I decided to create a class: particle_class.gd, which extends RigidBody.

Then, the three scenes (which are RigidBodies) have a script attached, that extends particle_class.gd. Until this point, everything is fine.

But now, I want to make an instance of the children scenes. Then, inside particle_class.gd, I have something like:

var particle = preload("../scenes/free_particle.tscn")

func do_things():
    var aux = particle.instance()
    add_child(aux)

When I try to compile this, I get this cyclic preload error:

Parser Error: Script not fully loaded (cyclic preload?): res://scripts/particle_class.gd

I imagine that this is coming from the fact that when I load free_particle.tscn, this tries to extends particle_class.gd, which iteratively tries to load again the free_particle scene.

My question is: how to avoid the cyclic preload error?

I get the same when extending another script - but only if the other script has an error.
Check your script to see if this could be the possible solution.
The other fix I believe has something to do with class names and a engine bug … in 3.1 I believe

crazyhat62 | 2020-07-24 09:35

:bust_in_silhouette: Reply From: Zylann

In an object-oriented thinking, I would not try to load specialized objects from within a base object. But if you really want to do this, you can try to use load instead of preload. The difference is, load will run after assets are loaded.

That works, thank you. In any case, I would like to know what are the alternatives to calling childs from base objects… sending a signal and catching it elsewhere or something like that?

VictorSeven | 2017-08-31 15:48

Why do you need these in the base class in the first place? If it’s about spawning “particles” in your case, you can either have a particle spawner script that does this. Or you can assign the specialized particle scenes as an exported variable on scenes that need to spawn them.

Zylann | 2017-08-31 20:31

Well, I did it that way basically the particle desintegrates. Then, I think that it is logical that “particle_class.gd” includes a method that does the desintegration, spawning more particles.
I don’t think that the exported variable is a good idea. I need to assign the instance beforehand, but in that moment I don’t know how many particles I will instantiate. Moreover, they will be deleted after…

VictorSeven | 2017-08-31 21:29

I still think there is a way to not have this cyclic coupling though, but I might miss the big picture here :slight_smile:

Zylann | 2017-08-31 21:31

:bust_in_silhouette: Reply From: WolframR

Try using the onready keyword?