How to instance a scene in C#?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hnuqweasd
:warning: Old Version Published before Godot 3 was released.

E.g. in GDscript you can do:
var scene = preload(“res://myscene.scn”)
var node = scene.instance()
add_child(node)

How do you do the equivalent in C#?

:bust_in_silhouette: Reply From: hnuqweasd

Not sure if this is the way to do it but after reading Introducing C# in Godot again I did:

PackedScene ground;

public override void _Ready()
{
    ground = (PackedScene)ResourceLoader.Load("res://scenes/Ground.tscn");
    StaticBody2D newGround = (StaticBody2D)ground.Instance();
    AddChild(newGround);
}

Yup, that’s it :wink:

Zylann | 2017-11-03 01:10

:bust_in_silhouette: Reply From: steelx

my answer is not detailed, but
you can initial MyScene.cs with _Notification so that it can be preloaded in another Csharp file just as a variable.

public class AnotherScene : Node2D
{
     public PackedScene Rooms = ResourceLoader.Load<PackedScene>("res://myscene.tscn");

}