Skip to content

Commit

Permalink
GH-416 Support instantiation of script-based classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Jun 22, 2024
1 parent 07c8e84 commit db2811e
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/script/nodes/memory/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,46 @@

#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/node.hpp>
#include <godot_cpp/classes/project_settings.hpp>
#include <godot_cpp/classes/resource_loader.hpp>

class OScriptNodeNewInstance : public OScriptNodeInstance
{
DECLARE_SCRIPT_NODE_INSTANCE(OScriptNodeNew);
String _class_name;
String _script_path;

public:
int step(OScriptExecutionContext& p_context) override
{
if (!_class_name.is_empty() && ClassDB::can_instantiate(_class_name))
{
Variant object = ClassDB::instantiate(_class_name);
p_context.set_output(0, object);
if (!_script_path.is_empty())
{
// Loading a script object instance type
Ref<Script> script = ResourceLoader::get_singleton()->load(_script_path);
if (script.is_valid())
{
Variant base = ClassDB::instantiate(script->get_instance_base_type());
Object* object = Object::cast_to<Object>(base);
if (object)
{
object->set_script(script);
p_context.set_output(0, object);
return 0;
}
}
}
else
{
// Loading a native class
Variant object = ClassDB::instantiate(_class_name);
p_context.set_output(0, object);
return 0;
}
}

p_context.set_output(0, Variant());
return 0;
}
};
Expand Down Expand Up @@ -139,6 +165,18 @@ OScriptNodeInstance* OScriptNodeNew::instantiate()
OScriptNodeNewInstance* i = memnew(OScriptNodeNewInstance);
i->_node = this;
i->_class_name = _class_name;

const TypedArray<Dictionary> global_class_list = ProjectSettings::get_singleton()->get_global_class_list();
for (int index = 0; index < global_class_list.size(); index++)
{
const Dictionary& entry = global_class_list[index];
if (entry.has("class") && _class_name.match(entry["class"]))
{
i->_script_path = entry["path"];
break;
}
}

return i;
}

Expand Down

0 comments on commit db2811e

Please sign in to comment.