GDScript中Object的has_method
方法对应着 C++中的Object::has_method
方法,之前想在每一帧都判断一下has_method
,方法很优雅不过时间复杂度至少是O(n)还是算了吧,附上该方法C++的实现:
Object::has_method
bool Object::has_method(const StringName &p_method) const {
if (p_method == CoreStringNames::get_singleton()->_free) {
return true;
}
if (script_instance && script_instance->has_method(p_method)) {
return true;
}
MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
return method != NULL;
}
ClassDB::get_method
MethodBind *ClassDB::get_method(StringName p_class, StringName p_name) {
OBJTYPE_RLOCK;
ClassInfo *type = classes.getptr(p_class);
while (type) {
MethodBind **method = type->method_map.getptr(p_name);
if (method && *method)
return *method;
type = type->inherits_ptr;
}
return NULL;
}