Instantiate Scene in C++

Godot Version



Hello everyone,

I want to have a field exposed in the editor properties panel where the user can set a .scene that then I will instantiate multiple times in the game when running in C++ code.

A simple example of that in GD Script would be:

@export var ButtonSceneRes : Resource = null; # This is a scene with nodes inside

func _ready() :
    var ButtonSceneResLoaded = load( ButtonSceneRes.get_path() );
    var newButton = ButtonSceneResLoaded .instantiate();
    self.add_child( newButton );

I am wondering how I can do this in Godot C++ ( not GD Native, nor GD Extension, but core engine code ).

Note that I know how to expose the Resource in the editor ( _bind_methods() and all that good stuff ). The thing I am not so sure is the instantiate and how to store the scene.

Should I store it as: Ref< PackedScene > or Ref< Resource > after calling ResourceLoader?

And what would this line be in C++?

var newButton = ButtonSceneResLoaded .instantiate();

Thank you a lot for the help! :grin: