Getting correct type when instantiating a PackedScene from a [Tool] script.

Godot Version

4.4.1

Question

I’m using C#. When using packedScene.Instantiate() in a [Tool] script, the type returned is always the base type of the node, not the type specified in the node script.

Eg, if my packedScene root node is a NinePatchRect with a DoorSprite.cs script, the type returned from the Instantiate call is NinePatchRect, when I expected it to be DoorSprite. I cannot cast this returned instance to DoorSprite.

However, if I put the [Tool] attribute on the DoorSprite class, I do get the expected type returned (DoorSprite) from the Instantiate call.

Is this expected behaviour?

Yes. Scripts that aren’t marked as tool will behave as empty scripts. When you instantiate a scene with a script attached to its root node that’s not marked as a tool script it will behave as if no script is attached. More info here Running code in the editor — Godot Engine (stable) documentation in English

1 Like