godotengine / godot-cpp

C++ bindings for the Godot script API
MIT License
1.74k stars 575 forks source link

Instantiate class using its String name #1400

Closed RonYanDaik closed 7 months ago

RonYanDaik commented 8 months ago

Godot version

4.x

godot-cpp version

4.x

System information

Win

Issue description

Since ClassDB already stores ClassInfo which has a template of _create_instance_func it looks like its possible to create an instance of class using its name but its hidden in private field.

Would be useful to add an public function for this.

Steps to reproduce

-

Minimal reproduction project

-

Kehom commented 7 months ago

You can already create class instances by their names, provided they are properly registered within the ClassDB.

SomeCustomClass* some_instance = godot::Object::cast_to<SomeCustomClass>(godot::ClassDB::instantiate("SomeCustomClass"));
dsnopek commented 7 months ago

Sounds like we can close this one because @Kehom explains how to do this. :-)

Thanks @Kehom!