Godot.Resource missing Instantiate() method

Godot Version

v4.3.stable.mono.official [77dcf97d8]

Question

I am extremely new to the engine and using C# because it’s what I’m familiar with. I am making a little 4key rhythm game so as to get myself used to how the engine operates and to better my optimization (yes I know GD script is faster).
I need to make instances of my FallingNote Sprite2D objects so there can be multiple of them at a time per lane, however it won’t let me call the Instantiate() method as referenced here (Nodes and scene instances — Godot Engine (stable) documentation in English)
I am running on Fedora linux, using Rider as my IDE.

public partial class NoteListeners : Sprite2D 
...
    private Sprite2D _fallingNote;
    public override void _Ready()
    {
        // Called every time the node is added to the scene.
        var scene = GD.Load("res://objects/nodes/falling_note.tscn"); // This was copied from the editor
        var _fallingNote = scene.Instantiate(); // this was copied from the godot support docs
        AddChild(_fallingNote);
        
    }

the scene.Instantiate(); method does not exist. How am I supposed to create instances?

Hi!

Use the typed Load method to specify you’re loading a scene, else, the compiler has no idea of the specific asset type you’re loading, thus it cannot assume it has an Instantiate method (that method being part of the PackedScene class, not Resource). Like this:

var scene = GD.Load<PackedScene>("res://objects/nodes/falling_note.tscn");

And that should work fine! Let me know if not.

Another way is to cast the load result:

var scene = (PackedScene)GD.Load("res://objects/nodes/falling_note.tscn");

or

var scene = GD.Load("res://objects/nodes/falling_note.tscn") as PackedScene;

But I believe the typed method is a cleaner way of doing that, since it exists precisely for that purpose.

3 Likes

Ah, I see that in the support doc. I somehow missed it, despite copy pasting. Must have removed it during renaming to my variable by mistake. Thanks!

1 Like

Please mark his answer as a solution. Also GdScript will always be slower than C#, being interpreted.

Done, whoops my bad.
I thought the way Godot handled C# and GD script was converting it all before compilation anyway? I saw a ton of documentation and forum discussion (documentation, forum, reddit)about how both are technically interpreted since Godot runs on C++ and everything you write is scripted anyway. There are a lot of things C# is faster for, but there are more that GD scripts are.
EDIT: I specifically mean Godot API calls, core integration is faster in c# but any Godot function is going to be slightly slower in C#, and even then the core speed increase is marginal and not noticeable unless you’re working with frankly absurd amounts of data, like scientific research program amounts of data, significantly more than any game frankly should be handling in this current day and age.

See GDScript: An introduction to dynamic languages — Godot Engine (stable) documentation in English
You’re right, core C# is certainly faster, but GdScript is faster at Godot functions. My bad.