How best to avoid "@implicit_new()" error?

Godot Version

4.2.1

Question

I’ve recently started running into an error:

E 0:00:00:0334   scene_manager.gd:11 @ @implicit_new(): res://src/characters/enemies/enemy.tscn:132 - Parse Error: [ext_resource] referenced non-existent resource at: res://src/ai/actions/attack.tscn
  <C++ Source>   scene/resources/resource_format_text.cpp:163 @ _parse_ext_resource()
  <Stack Trace>  scene_manager.gd:11 @ @implicit_new()

If I understand correctly, the problem on a basic level seems to be that it’s looking for something in the attack_action which isn’t there. What confuses me is that this error doesn’t get thrown if I manually add these characters to the scene and run it. It only happens when I instance them and I’m not sure why.

Nothing special is happening with the instancing. It’s super standard stuff, which leads me to believe the problem is in how the tree of the scene is being built and what is being connected when.

I have a lot of small scripts which are used by the behavior tree to trigger certain actions and these store a reference to the CharacterBody2D. I had been doing this as export variables and connected them manually, but I started connecting it in the ready function of the CharacterBody script. I don’t know if there’s a preferable way to do that.

Since there’s too much code to show it all here, I’ll give an example of how it’s set up…

The behavior tree is a child of the CharacterBody2D. It has it’s own hierarchy and scripts and stores a reference to the Character as a variable called ‘actor’. The behavior tree’s first condition check looks to see if the actor value is not null. If there’s a reference, it continues through the behavior tree.

When it reaches a certain point, let’s say, the attack action, it’ll call that action like player.attack_action.emit_signal(“attack”). The actor/CharacterBody script has a reference to the attack_action node, so the signal is passed as you’d expect.

This seems to be where the issues crop up though. Every node that needs to know about another node seems to have the references set correctly. It’s possible I’m missing something, but I’ve looked it all over multiple times and am coming up empty so far (I’ll keep looking).

What I’m wondering is less how to solve this particular issue, and more a question of how should I best structure more complex scenes to avoid this implicit_new() error being an issue to begin with? I’m totally willing to do a refactor if it’ll make these scenes more robust and less prone to errors. I’m just not sure what I should be doing in that regard. Thanks!

For anyone who stumbles upon this, the issue in this case seems to be that I was instancing certain scenes using preload on certain variables instead of load.