Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to reference a class constructor as a Callable #73657

Merged
merged 1 commit into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion modules/gdscript/gdscript_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3264,7 +3264,7 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod
base = *p_base;
}

const StringName &name = p_identifier->name;
StringName name = p_identifier->name;

if (base.kind == GDScriptParser::DataType::ENUM) {
if (base.is_meta_type) {
Expand Down Expand Up @@ -3359,12 +3359,18 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod
get_class_node_current_scope_classes(base_class, &script_classes);
}

bool is_constructor = base.is_meta_type && p_identifier->name == SNAME("new");

for (GDScriptParser::ClassNode *script_class : script_classes) {
if (p_base == nullptr && script_class->identifier && script_class->identifier->name == name) {
reduce_identifier_from_base_set_class(p_identifier, script_class->get_datatype());
return;
}

if (is_constructor) {
name = "_init";
}

if (script_class->has_member(name)) {
resolve_class_member(script_class, name, p_identifier);

Expand Down Expand Up @@ -3443,6 +3449,10 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod
const StringName &native = base.native_type;

if (class_exists(native)) {
if (is_constructor) {
name = "_init";
}

MethodInfo method_info;
if (ClassDB::has_property(native, name)) {
StringName getter_name = ClassDB::get_property_getter(native, name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# https://github.com/godotengine/godot/issues/70319

class InnerClass:
pass

func test():
var inner_ctor : Callable = InnerClass.new
print(inner_ctor)
var native_ctor : Callable = Node.new
print(native_ctor)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GDTEST_OK
GDScript::new
GDScriptNativeClass::new