Help: Resource script is being automatically embedded into a scene

Godot Version

v4.3.stable.official [77dcf97d8]

Question

Hi all - thanks in advance! (I’m very new to godot, but not to development.)

I have a script that extends Resource and defines a few basic fields.

I have a scene that has an external script attached to it; in that script, I @export a variable whose type is my custom Resource.

I can use the inspector for the scene to put a new instance of the resource in, and set some of the values; when I run the scene it works as expected.

If I edit the scene’s gdscript and save it, the contents of the resource’s script get dumped into scene file (I opened the .tscn file to see the difference before and after - prior to editing the gdscript the resource is referenced as an ext_resource pointing to the file; after a new sub_resource with type=GDScript is created and the entire contents of the resource script are copied in).

This is causing a problem due to a class_name (since there are now two in the project). I can’t figure out why this is happening though. It only happens when I edit and save the scene’s attached script - it does not happen if I edit the scene in the inspector. To temporarily fix the problem, I can revert the new resource in the inspector, reload the project, and redo the work in the inspector; this works until I edit the scene script again.

As another point of data, I have this same resource being used in other scenes with no problem. This feels like a bug - the editor is doing something strange and dropping all this code in where it shouldn’t be, but I’m not versed enough in the deep details of how everything works to be confident that I’m not doing something wrong.

Any ideas or pointers on how to resolve this?

As a follow-up to my own post, I’ve sorted out what the problem was. I’ll leave some notes here in case anyone else bumps into this:

I had added a few fields to my custom resource that were defined as inner classes (is that the right term?) that also extended Resource. These seem to be what was prompting (possibly requiring) that the script be attached to anywhere that the resource was being used by the inspector.

By moving those inner classes out to their own files, the problem went away. I’ll chalk this up to knowing enough to be dangerous - I jumped right on having some inner classes to keep things organized, but it seems like at least in this case, they should be avoided.