+1 vote

Let's say I have a C# class attached to the root node of a scene called CustomSprite.

If I wanted to instance the scene that is part of a scene in C# from another class - Should I load it as a scene then cast it to the class type when instanced?

var CustomSprite = GD.Load("res://customsprite.tscn").Instance() as CustomSprite

or will just instancing the class work?

var CustomSprite = new CustomSprite()

What about if I want to have constructor parameters that distinguish behaviour in the scene object? Something like

var CustomSprite = new CustomSprite(Color color)

Which would then lead to that scene being instanced, and the scene using that parameter in some way. Like setting it's colour to the specified colour.

asked May 25 in Engine by alibix (35 points)

1 Answer

0 votes
Best answer
  1. You can instance a whole Scene:

PackedScene packedScene =
ResourceLoader.Load("res://CustomSprite.tscn") as PackedScene;
CustomSprite customSprite = packedScene.Instance() as CustomSprite;

  1. Instancing a class will just load "the C# class". It wont load the whole scene. But it would be possible to generate the CustomSprite inside that class.
answered May 25 by juppi (304 points)
selected May 27 by alibix

Yeah I think I get it. I've just made most of my classes abstract because they technically don't get instantiated.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.