c++game-developmentgodotgodot3

How to instance scene in Godot but with C++ module?


I try to rewrite my Gdscript logic to C++ with modules way. There is no any issues if I load resource with line:

Ref<Resource> cpp_sprite = ResourceLoader::load("res://CppSprite.tscn");

But I can not make instance it to scene just as in gdscript do, this line return an error:

CppNode *cpp_sprite_instance = cpp_sprite->instance();

Compiler says: class Resource has no member “instance”. Allright, cpp_sprite is Resource type. But how to flip Resource type to PackedScene?

I checked the docs but there is not too much information about C++ code writing.


Solution

  • So on I was wrong with types. ResourceLoader::load can load packed scene straight ahead as PackedScene, not necessary use Resorce class. Solved wtih flip Ref<Resource> to Ref<PackedScene>:

    Ref<PackedScene> cpp_sprite = ResourceLoader::load("res://CppSprite.tscn");