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 get list of non object methods #49053

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,26 @@ bool ClassDB::is_class_enabled(StringName p_class) const {
return ::ClassDB::is_class_enabled(p_class);
}

Array ClassDB::get_variant_method_list(Variant::Type p_type) const {
List<MethodInfo> methods;
Variant::get_method_list_by_type(&methods, p_type);
Array ret;

for (const MethodInfo &E : methods) {
#ifdef DEBUG_METHODS_ENABLED
ret.push_back(E.operator Dictionary());
#else
Dictionary dict;
dict["name"] = E.name;
ret.push_back(dict);
#endif
}

return ret;
}

void ClassDB::_bind_methods() {
::ClassDB::bind_method(D_METHOD("get_variant_method_list", "type"), &ClassDB::get_variant_method_list);
::ClassDB::bind_method(D_METHOD("get_class_list"), &ClassDB::get_class_list);
::ClassDB::bind_method(D_METHOD("get_inheriters_from_class", "class"), &ClassDB::get_inheriters_from_class);
::ClassDB::bind_method(D_METHOD("get_parent_class", "class"), &ClassDB::get_parent_class);
Expand Down
2 changes: 2 additions & 0 deletions core/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,9 @@ class ClassDB : public Object {

bool has_method(StringName p_class, StringName p_method, bool p_no_inheritance = false) const;


TypedArray<Dictionary> get_method_list(StringName p_class, bool p_no_inheritance = false) const;
Array get_variant_method_list(Variant::Type p_type) const;

PackedStringArray get_integer_constant_list(const StringName &p_class, bool p_no_inheritance = false) const;
bool has_integer_constant(const StringName &p_class, const StringName &p_name) const;
Expand Down
2 changes: 2 additions & 0 deletions core/variant/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,8 @@ class Variant {
static String get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
static String get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);

static void get_method_list_by_type(List<MethodInfo> *p_list, Variant::Type p_type);

//dynamic (includes Object)
void get_method_list(List<MethodInfo> *p_list) const;
bool has_method(const StringName &p_method) const;
Expand Down
85 changes: 45 additions & 40 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1248,54 +1248,59 @@ uint32_t Variant::get_builtin_method_hash(Variant::Type p_type, const StringName
return hash_fmix32(hash);
}

void Variant::get_method_list(List<MethodInfo> *p_list) const {
if (type == OBJECT) {
Object *obj = get_validated_object();
if (obj) {
obj->get_method_list(p_list);
}
} else {
for (const StringName &E : builtin_method_names[type]) {
const VariantBuiltInMethodInfo *method = builtin_method_info[type].lookup_ptr(E);
ERR_CONTINUE(!method);

MethodInfo mi;
mi.name = E;

//return type
if (method->has_return_type) {
mi.return_val.type = method->return_type;
if (mi.return_val.type == Variant::NIL) {
mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
}
}
void Variant::get_method_list_by_type(List<MethodInfo> *p_list, Variant::Type p_type) {
ERR_FAIL_INDEX(p_type, Variant::VARIANT_MAX);
for (const StringName &E : builtin_method_names[p_type]) {
const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(E);
ERR_CONTINUE(!method);

if (method->is_const) {
mi.flags |= METHOD_FLAG_CONST;
}
if (method->is_vararg) {
mi.flags |= METHOD_FLAG_VARARG;
}
if (method->is_static) {
mi.flags |= METHOD_FLAG_STATIC;
MethodInfo mi;
mi.name = E;

//return type
if (method->has_return_type) {
mi.return_val.type = method->return_type;
if (mi.return_val.type == Variant::NIL) {
mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
}
for (int i = 0; i < method->argument_count; i++) {
PropertyInfo pi;
}

if (method->is_const) {
mi.flags |= METHOD_FLAG_CONST;
}
if (method->is_vararg) {
mi.flags |= METHOD_FLAG_VARARG;
}
if (method->is_static) {
mi.flags |= METHOD_FLAG_STATIC;
}
for (int i = 0; i < method->argument_count; i++) {
PropertyInfo pi;
#ifdef DEBUG_METHODS_ENABLED
pi.name = method->argument_names[i];
pi.name = method->argument_names[i];
#else
pi.name = "arg" + itos(i + 1);
pi.name = "arg" + itos(i + 1);
#endif
pi.type = method->get_argument_type(i);
if (pi.type == Variant::NIL) {
pi.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
}
mi.arguments.push_back(pi);
pi.type = method->get_argument_type(i);
if (pi.type == Variant::NIL) {
pi.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
}
mi.arguments.push_back(pi);
}

mi.default_arguments = method->default_arguments;
p_list->push_back(mi);
}
}

mi.default_arguments = method->default_arguments;
p_list->push_back(mi);
void Variant::get_method_list(List<MethodInfo> *p_list) const {
if (type == OBJECT) {
Object *obj = get_validated_object();
if (obj) {
obj->get_method_list(p_list);
}
} else {
Variant::get_method_list_by_type(p_list, type);
}
}

Expand Down
7 changes: 7 additions & 0 deletions doc/classes/ClassDB.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@
Returns the parent class of [param class].
</description>
</method>
<method name="get_variant_method_list" qualifiers="const">
<return type="Array" />
<argument index="0" name="type" type="int" enum="Variant.Type" />
<description>
qarmin marked this conversation as resolved.
Show resolved Hide resolved
Returns an array of methods of built-in [code]class[/code] like [code]String[/code] or [code]Vector2[/code].
</description>
</method>
<method name="instantiate" qualifiers="const">
<return type="Variant" />
<param index="0" name="class" type="StringName" />
Expand Down