Godot Engine – Multi-platform 2D and 3D game engine
Move property maps from `ClassDB` to `GDType`. Reject more name clashes in properties. Make debug and release more consistent w.r.t. properties.
Lukas Tenbrinkc97f21f9f480
diff --git a/core/object/class_db.cpp b/core/object/class_db.cppindex cd4c7647fed..6b5e9b4f428 100644--- a/core/object/class_db.cpp+++ b/core/object/class_db.cpp@@ -479,33 +479,35 @@ uint32_t ClassDB::get_api_hash(APIType p_api) {}}- { //properties+ {+ //properties- List<StringName> snames;+ LocalVector<StringName> snames;- for (const KeyValue<StringName, PropertySetGet> &F : t->property_setget) {- snames.push_back(F.key);+ for (const KeyValue<StringName, GDType::Property> &kv : t->gdtype->get_property_map(true)) {+ if (kv.value.type == GDType::Property::Type::SETGET) {+ snames.push_back(kv.key);+ }}-snames.sort_custom<StringName::AlphCompare>();for (const StringName &F : snames) {- PropertySetGet *psg = t->property_setget.getptr(F);- ERR_FAIL_NULL_V(psg, 0);+ const GDType::Property &property = t->gdtype->get_property_map(true)[F];+ const GDType::Property::SetGet &psg = property.payload.setget;hash = hash_murmur3_one_64(F.hash(), hash);- hash = hash_murmur3_one_64(psg->setter.hash(), hash);- hash = hash_murmur3_one_64(psg->getter.hash(), hash);+ hash = hash_murmur3_one_64((psg.setter ? psg.setter->get_name() : StringName()).hash(), hash);+ hash = hash_murmur3_one_64((psg.getter ? psg.getter->get_name() : StringName()).hash(), hash);}}//property list- for (const PropertyInfo &F : t->property_list) {- hash = hash_murmur3_one_64(F.name.hash(), hash);- hash = hash_murmur3_one_64(F.type, hash);- hash = hash_murmur3_one_64(F.hint, hash);- hash = hash_murmur3_one_64(F.hint_string.hash(), hash);- hash = hash_murmur3_one_64(F.usage, hash);+ for (const PropertyInfo *F : t->gdtype->get_ordered_self_properties()) {+ hash = hash_murmur3_one_64(F->name.hash(), hash);+ hash = hash_murmur3_one_64(F->type, hash);+ hash = hash_murmur3_one_64(F->hint, hash);+ hash = hash_murmur3_one_64(F->hint_string.hash(), hash);+ hash = hash_murmur3_one_64(F->usage, hash);}}@@ -973,7 +975,7 @@ static MethodInfo info_from_bind(const MethodBind *p_method) {return minfo;}-void ClassDB::get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {+void ClassDB::get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {Locker::Lock lock(Locker::STATE_READ);ClassInfo *type = classes.getptr(p_class);@@ -981,11 +983,6 @@ void ClassDB::get_method_list(const StringName &p_class, List<MethodInfo> *p_metif (type->disabled) {continue;}-#ifdef DEBUG_ENABLED- if (p_exclude_from_properties && type->methods_in_properties.has(kv.key)) {- continue;- }-#endifp_methods->push_back(info_from_bind(kv.value));}@@ -995,7 +992,7 @@ void ClassDB::get_method_list(const StringName &p_class, List<MethodInfo> *p_met#endif}-void ClassDB::get_method_list_with_compatibility(const StringName &p_class, List<Pair<MethodInfo, uint32_t>> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {+void ClassDB::get_method_list_with_compatibility(const StringName &p_class, List<Pair<MethodInfo, uint32_t>> *p_methods, bool p_no_inheritance) {Locker::Lock lock(Locker::STATE_READ);ClassInfo *type = classes.getptr(p_class);@@ -1003,11 +1000,6 @@ void ClassDB::get_method_list_with_compatibility(const StringName &p_class, Listif (type->disabled) {continue;}-#ifdef DEBUG_ENABLED- if (p_exclude_from_properties && type->methods_in_properties.has(kv.key)) {- continue;- }-#endifp_methods->push_back(Pair(info_from_bind(kv.value), kv.value->get_hash()));}@@ -1044,7 +1036,7 @@ void ClassDB::get_method_list_with_compatibility(const StringName &p_class, List}}-bool ClassDB::get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance, bool p_exclude_from_properties) {+bool ClassDB::get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance) {Locker::Lock lock(Locker::STATE_READ);ClassInfo *type = classes.getptr(p_class);@@ -1344,7 +1336,7 @@ bool ClassDB::get_signal(const StringName &p_class, const StringName &p_signal,}void ClassDB::add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix, int p_indent_depth) {- Locker::Lock lock(Locker::STATE_WRITE);+ Locker::Lock lock(Locker::STATE_READ); // Doesn't modify ClassDB stuff.ClassInfo *type = classes.getptr(p_class);ERR_FAIL_NO_CLASS(type, p_class);@@ -1353,11 +1345,11 @@ void ClassDB::add_property_group(const StringName &p_class, const String &p_nameprefix = vformat("%s,%d", p_prefix, p_indent_depth);}- type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_GROUP));+ type->gdtype->add_to_ordered_properties(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_GROUP));}void ClassDB::add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix, int p_indent_depth) {- Locker::Lock lock(Locker::STATE_WRITE);+ Locker::Lock lock(Locker::STATE_READ); // Doesn't modify ClassDB stuff.ClassInfo *type = classes.getptr(p_class);ERR_FAIL_NO_CLASS(type, p_class);@@ -1366,7 +1358,7 @@ void ClassDB::add_property_subgroup(const StringName &p_class, const String &p_nprefix = vformat("%s,%d", p_prefix, p_indent_depth);}- type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_SUBGROUP));+ type->gdtype->add_to_ordered_properties(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_SUBGROUP));}void ClassDB::add_property_array_count(const StringName &p_class, const String &p_label, const StringName &p_count_property, const StringName &p_count_setter, const StringName &p_count_getter, const String &p_array_element_prefix, uint32_t p_count_usage) {@@ -1374,71 +1366,20 @@ void ClassDB::add_property_array_count(const StringName &p_class, const String &}void ClassDB::add_property_array(const StringName &p_class, const StringName &p_path, const String &p_array_element_prefix) {- Locker::Lock lock(Locker::STATE_WRITE);+ Locker::Lock lock(Locker::STATE_READ); // Doesn't modify ClassDB stuff.ClassInfo *type = classes.getptr(p_class);ERR_FAIL_NO_CLASS(type, p_class);- type->property_list.push_back(PropertyInfo(Variant::NIL, p_path, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY, p_array_element_prefix));+ type->gdtype->add_to_ordered_properties(PropertyInfo(Variant::NIL, p_path, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY, p_array_element_prefix));}// NOTE: For implementation simplicity reasons, this method doesn't allow setters to have optional arguments at the end.void ClassDB::add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {- Locker::Lock lock(Locker::STATE_WRITE);+ Locker::Lock lock(Locker::STATE_READ); // Doesn't modify ClassDB stuff.ClassInfo *type = classes.getptr(p_class);-ERR_FAIL_NO_CLASS(type, p_class);-- const MethodBind *mb_set = nullptr;- if (p_setter) {- mb_set = get_method(p_class, p_setter);-#ifdef DEBUG_ENABLED-- ERR_FAIL_NULL_MSG(mb_set, vformat("Invalid setter '%s::%s' for property '%s'.", p_class, p_setter, p_pinfo.name));-- int exp_args = 1 + (p_index >= 0 ? 1 : 0);- ERR_FAIL_COND_MSG(mb_set->get_argument_count() != exp_args, vformat("Invalid function for setter '%s::%s' for property '%s'.", p_class, p_setter, p_pinfo.name));-#endif // DEBUG_ENABLED- }-- const MethodBind *mb_get = nullptr;- if (p_getter) {- mb_get = get_method(p_class, p_getter);-#ifdef DEBUG_ENABLED-- ERR_FAIL_NULL_MSG(mb_get, vformat("Invalid getter '%s::%s' for property '%s'.", p_class, p_getter, p_pinfo.name));-- int exp_args = 0 + (p_index >= 0 ? 1 : 0);- ERR_FAIL_COND_MSG(mb_get->get_argument_count() != exp_args, vformat("Invalid function for getter '%s::%s' for property '%s'.", p_class, p_getter, p_pinfo.name));-#endif // DEBUG_ENABLED- }--#ifdef DEBUG_ENABLED- ERR_FAIL_COND_MSG(type->property_setget.has(p_pinfo.name), vformat("Object '%s' already has property '%s'.", p_class, p_pinfo.name));-#endif // DEBUG_ENABLED-- type->property_list.push_back(p_pinfo);- type->property_map[p_pinfo.name] = p_pinfo;-#ifdef DEBUG_ENABLED- // Used to filter out setters and getters in the editor (e.g. autocomplete) to not clutter menus. We only want to filter methods from properties that are easily available to users.- if (p_index == -1 && !(p_pinfo.usage & PropertyUsageFlags::PROPERTY_USAGE_INTERNAL)) {- if (mb_get) {- type->methods_in_properties.insert(p_getter);- }- if (mb_set) {- type->methods_in_properties.insert(p_setter);- }- }-#endif // DEBUG_ENABLED- PropertySetGet psg;- psg.setter = p_setter;- psg.getter = p_getter;- psg._setptr = mb_set;- psg._getptr = mb_get;- psg.index = p_index;- psg.type = p_pinfo.type;-- type->property_setget[p_pinfo.name] = psg;+ type->gdtype->add_property(p_pinfo, p_setter, p_getter, p_index);}void ClassDB::set_property_default_value(const StringName &p_class, const StringName &p_name, const Variant &p_default) {@@ -1450,12 +1391,17 @@ void ClassDB::set_property_default_value(const StringName &p_class, const Stringvoid ClassDB::add_linked_property(const StringName &p_class, const String &p_property, const String &p_linked_property) {#ifdef TOOLS_ENABLED- Locker::Lock lock(Locker::STATE_WRITE);+ Locker::Lock lock(Locker::STATE_READ); // Doesn't modify ClassDB stuff.ClassInfo *type = classes.getptr(p_class);ERR_FAIL_NO_CLASS(type, p_class);- ERR_FAIL_COND(!type->property_map.has(p_property));- ERR_FAIL_COND(!type->property_map.has(p_linked_property));+ const GDType::Property *property = type->gdtype->get_property_map(true).getptr(p_property);+ ERR_FAIL_NULL(property);+ ERR_FAIL_COND(property->type != GDType::Property::Type::SETGET);++ const GDType::Property *linked_property = type->gdtype->get_property_map(true).getptr(p_linked_property);+ ERR_FAIL_NULL(linked_property);+ ERR_FAIL_COND(linked_property->type != GDType::Property::Type::SETGET);if (!type->linked_properties.has(p_property)) {type->linked_properties.insert(p_property, List<StringName>());@@ -1466,13 +1412,13 @@ void ClassDB::add_linked_property(const StringName &p_class, const String &p_pro}void ClassDB::get_property_list(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) {- Locker::Lock lock(Locker::STATE_READ);+ Locker::Lock lock(Locker::STATE_READ); // Doesn't modify ClassDB stuff.ClassInfo *type = classes.getptr(p_class);ClassInfo *check = type;while (check) {- for (const PropertyInfo &pi : check->property_list) {- p_list->push_back(pi);+ for (const PropertyInfo *pi : check->gdtype->get_ordered_self_properties()) {+ p_list->push_back(*pi);if (p_validator) {p_validator->validate_property(p_list->back()->get());}@@ -1507,22 +1453,18 @@ void ClassDB::get_linked_properties_info(const StringName &p_class, const Stringbool ClassDB::get_property_info(const StringName &p_class, const StringName &p_property, PropertyInfo *r_info, bool p_no_inheritance, const Object *p_validator) {Locker::Lock lock(Locker::STATE_READ);- ClassInfo *check = classes.getptr(p_class);- while (check) {- if (check->property_map.has(p_property)) {- PropertyInfo pinfo = check->property_map[p_property];- if (p_validator) {- p_validator->validate_property(pinfo);- }+ ClassInfo *class_info = classes.getptr(p_class);+ if (class_info) {+ const GDType::Property *property = class_info->gdtype->get_property_map(p_no_inheritance).getptr(p_property);+ if (property && property->type == GDType::Property::Type::SETGET) {if (r_info) {- *r_info = pinfo;+ *r_info = *property->payload.setget.property_info;+ }+ if (p_validator) {+ p_validator->validate_property(*r_info);}return true;}- if (p_no_inheritance) {- break;- }- check = check->inherits_ptr;}return false;@@ -1531,126 +1473,29 @@ bool ClassDB::get_property_info(const StringName &p_class, const StringName &p_pbool ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid) {ERR_FAIL_NULL_V(p_object, false);- ClassInfo *type = classes.getptr(p_object->get_class_name());- ClassInfo *check = type;- while (check) {- const PropertySetGet *psg = check->property_setget.getptr(p_property);- if (psg) {- if (!psg->setter) {- if (r_valid) {- *r_valid = false;- }- return true; //return true but do nothing- }-- Callable::CallError ce;-- if (psg->index >= 0) {- Variant index = psg->index;- const Variant *arg[2] = { &index, &p_value };- //p_object->call(psg->setter,arg,2,ce);- if (psg->_setptr) {- psg->_setptr->call(p_object, arg, 2, ce);- } else {- p_object->callp(psg->setter, arg, 2, ce);- }-- } else {- const Variant *arg[1] = { &p_value };- if (psg->_setptr) {- psg->_setptr->call(p_object, arg, 1, ce);- } else {- p_object->callp(psg->setter, arg, 1, ce);- }- }-- if (r_valid) {- *r_valid = ce.error == Callable::CallError::CALL_OK;- }-- return true;- }-- check = check->inherits_ptr;- }-- return false;+ return p_object->set_native(p_property, p_value, r_valid);}bool ClassDB::get_property(Object *p_object, const StringName &p_property, Variant &r_value) {ERR_FAIL_NULL_V(p_object, false);- ClassInfo *type = classes.getptr(p_object->get_class_name());- ClassInfo *check = type;- while (check) {- const PropertySetGet *psg = check->property_setget.getptr(p_property);- if (psg) {- if (!psg->getter) {- return true; //return true but do nothing- }-- if (psg->index >= 0) {- Variant index = psg->index;- const Variant *arg[1] = { &index };- Callable::CallError ce;- const Variant value = p_object->callp(psg->getter, arg, 1, ce);- r_value = (ce.error == Callable::CallError::CALL_OK) ? value : Variant();-- } else {- Callable::CallError ce;- if (psg->_getptr) {- r_value = psg->_getptr->call(p_object, nullptr, 0, ce);- } else {- const Variant value = p_object->callp(psg->getter, nullptr, 0, ce);- r_value = (ce.error == Callable::CallError::CALL_OK) ? value : Variant();- }- }- return true;- }-- const int64_t *c = check->gdtype->get_integer_constant_map(true).getptr(p_property); //constants count- if (c) {- r_value = *c;- return true;- }-- if (check->gdtype->get_method_map(true).has(p_property)) { //methods count- r_value = Callable(p_object, p_property);- return true;- }-- if (check->gdtype->get_signal_map(true).has(p_property)) { //signals count- r_value = Signal(p_object, p_property);- return true;- }-- check = check->inherits_ptr;- }-- // The "free()" method is special, so we assume it exists and return a Callable.- if (p_property == CoreStringName(free_)) {- r_value = Callable(p_object, p_property);- return true;- }-- return false;+ return p_object->get_native(p_property, r_value);}int ClassDB::get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {- ClassInfo *type = classes.getptr(p_class);- ClassInfo *check = type;- while (check) {- const PropertySetGet *psg = check->property_setget.getptr(p_property);- if (psg) {+ ClassInfo *class_info = classes.getptr(p_class);+ if (class_info) {+ const GDType::Property *property = class_info->gdtype->get_property_map().getptr(p_property);+ if (property && property->type == GDType::Property::Type::SETGET) {+ const GDType::Property::SetGet &psg = property->payload.setget;if (r_is_valid) {*r_is_valid = true;}- return psg->index;+ return psg.index;}-- check = check->inherits_ptr;}+if (r_is_valid) {*r_is_valid = false;}@@ -1659,20 +1504,19 @@ int ClassDB::get_property_index(const StringName &p_class, const StringName &p_p}Variant::Type ClassDB::get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {- ClassInfo *type = classes.getptr(p_class);- ClassInfo *check = type;- while (check) {- const PropertySetGet *psg = check->property_setget.getptr(p_property);- if (psg) {+ ClassInfo *class_info = classes.getptr(p_class);+ if (class_info) {+ const GDType::Property *property = class_info->gdtype->get_property_map().getptr(p_property);+ if (property && property->type == GDType::Property::Type::SETGET) {+ const GDType::Property::SetGet &psg = property->payload.setget;if (r_is_valid) {*r_is_valid = true;}- return psg->type;+ return psg.property_info->type;}-- check = check->inherits_ptr;}+if (r_is_valid) {*r_is_valid = false;}@@ -1681,47 +1525,34 @@ Variant::Type ClassDB::get_property_type(const StringName &p_class, const String}StringName ClassDB::get_property_setter(const StringName &p_class, const StringName &p_property) {- ClassInfo *type = classes.getptr(p_class);- ClassInfo *check = type;- while (check) {- const PropertySetGet *psg = check->property_setget.getptr(p_property);- if (psg) {- return psg->setter;+ ClassInfo *class_info = classes.getptr(p_class);+ if (class_info) {+ const GDType::Property *property = class_info->gdtype->get_property_map().getptr(p_property);+ if (property && property->type == GDType::Property::Type::SETGET) {+ return property->payload.setget.setter ? property->payload.setget.setter->get_name() : StringName();}-- check = check->inherits_ptr;}return StringName();}StringName ClassDB::get_property_getter(const StringName &p_class, const StringName &p_property) {- ClassInfo *type = classes.getptr(p_class);- ClassInfo *check = type;- while (check) {- const PropertySetGet *psg = check->property_setget.getptr(p_property);- if (psg) {- return psg->getter;+ ClassInfo *class_info = classes.getptr(p_class);+ if (class_info) {+ const GDType::Property *property = class_info->gdtype->get_property_map().getptr(p_property);+ if (property && property->type == GDType::Property::Type::SETGET) {+ return property->payload.setget.getter ? property->payload.setget.getter->get_name() : StringName();}-- check = check->inherits_ptr;}return StringName();}bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {- ClassInfo *type = classes.getptr(p_class);- ClassInfo *check = type;- while (check) {- if (check->property_setget.has(p_property)) {- return true;- }-- if (p_no_inheritance) {- break;- }- check = check->inherits_ptr;+ ClassInfo *class_info = classes.getptr(p_class);+ if (class_info) {+ const GDType::Property *property = class_info->gdtype->get_property_map(p_no_inheritance).getptr(p_property);+ return property ? property->type == GDType::Property::Type::SETGET : false;}return false;diff --git a/core/object/class_db.h b/core/object/class_db.hindex 70ff02abfca..8672bc9272a 100644--- a/core/object/class_db.h+++ b/core/object/class_db.h@@ -108,15 +108,6 @@ public:};public:- struct PropertySetGet {- int index;- StringName setter;- StringName getter;- const MethodBind *_setptr = nullptr;- const MethodBind *_getptr = nullptr;- Variant::Type type;- };-struct ClassInfo {APIType api = API_NONE;ClassInfo *inherits_ptr = nullptr;@@ -127,11 +118,7 @@ public:HashMap<StringName, LocalVector<MethodBind *>> method_map_compatibility;- List<PropertyInfo> property_list;- HashMap<StringName, PropertyInfo> property_map;-#ifdef DEBUG_ENABLED- HashSet<StringName> methods_in_properties;List<MethodInfo> virtual_methods;HashMap<StringName, MethodInfo> virtual_methods_map;HashMap<StringName, Vector<Error>> method_error_values;@@ -142,7 +129,6 @@ public:List<StringName> dependency_list;#endif- AHashMap<StringName, PropertySetGet> property_setget;HashMap<StringName, Vector<uint32_t>> virtual_methods_compat;bool disabled = false;@@ -481,9 +467,9 @@ public:static bool has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance = false);static void set_method_flags(const StringName &p_class, const StringName &p_method, int p_flags);- static void get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false, bool p_exclude_from_properties = false);- static void get_method_list_with_compatibility(const StringName &p_class, List<Pair<MethodInfo, uint32_t>> *p_methods_with_hash, bool p_no_inheritance = false, bool p_exclude_from_properties = false);- static bool get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance = false, bool p_exclude_from_properties = false);+ static void get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false);+ static void get_method_list_with_compatibility(const StringName &p_class, List<Pair<MethodInfo, uint32_t>> *p_methods_with_hash, bool p_no_inheritance = false);+ static bool get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance = false);static int get_method_argument_count(const StringName &p_class, const StringName &p_method, bool *r_is_valid = nullptr, bool p_no_inheritance = false);static const MethodBind *get_method(const StringName &p_class, const StringName &p_name);static const MethodBind *get_method_with_compatibility(const StringName &p_class, const StringName &p_name, uint64_t p_hash, bool *r_method_exists = nullptr, bool *r_is_deprecated = nullptr);diff --git a/core/object/gdtype.cpp b/core/object/gdtype.cppindex 6ade9fa0039..ecec9cb4fee 100644--- a/core/object/gdtype.cpp+++ b/core/object/gdtype.cpp@@ -55,6 +55,9 @@ GDType::~GDType() {for (MethodBind *bind : owned_method_map) {memdelete(bind);}+ for (const PropertyInfo *property : ordered_self_properties) {+ memdelete(const_cast<PropertyInfo *>(property));+ }}void GDType::initialize() {@@ -71,6 +74,7 @@ void GDType::initialize() {enum_map = super_type->enum_map;signal_map = super_type->signal_map;method_map = super_type->method_map;+ property_map = super_type->property_map;}init_state = InitState::MUTABLE;@@ -80,9 +84,12 @@ void GDType::bind_integer_constant(const StringName &p_enum, const StringName &pERR_FAIL_COND(!Thread::is_main_thread());ERR_FAIL_COND(init_state != InitState::MUTABLE);ERR_FAIL_COND_MSG(self_constant_map.has(p_name), vformat("Class '%s' already has constant '%s'.", String(name), String(p_name)));+ ERR_FAIL_COND_MSG(property_map.has(p_name), vformat("Object '%s' already has property '%s'.", get_name(), p_name));constant_map[p_name] = p_constant;self_constant_map[p_name] = p_constant;+ property_map.insert(p_name, Property(Property::Type::INTEGER_CONSTANT, p_constant));+ self_property_map.insert(p_name, Property(Property::Type::INTEGER_CONSTANT, p_constant));String enum_name = p_enum;if (!enum_name.is_empty()) {@@ -123,22 +130,25 @@ void GDType::add_signal(MethodInfo p_signal) {const StringName signal_name(p_signal.name);ERR_FAIL_COND_MSG(signal_map.has(signal_name), vformat("Class '%s' already has signal '%s'.", String(name), String(signal_name)));+ ERR_FAIL_COND_MSG(property_map.has(signal_name), vformat("Object '%s' already has property '%s'.", get_name(), signal_name));const MethodInfo *ptr = memnew(MethodInfo(std::move(p_signal)));signal_map[signal_name] = ptr;self_signal_map[signal_name] = ptr;+ property_map.insert(ptr->name, Property(Property::Type::SIGNAL));+ self_property_map.insert(ptr->name, Property(Property::Type::SIGNAL));}bool GDType::bind_method(MethodBind *p_method, bool p_take_ownership) {ERR_FAIL_COND_V(!Thread::is_main_thread(), false);ERR_FAIL_COND_V(init_state != InitState::MUTABLE, false);- if (self_method_map.has(p_method->get_name())) {+ if (property_map.has(p_method->get_name())) {if (p_take_ownership) {memdelete(p_method);}- ERR_FAIL_V_MSG(false, vformat("Method already bound '%s::%s'.", name, p_method->get_name()));+ ERR_FAIL_V_MSG(false, vformat("Object '%s' already has property '%s'.", get_name(), p_method->get_name()));}method_map[p_method->get_name()] = p_method;@@ -146,6 +156,9 @@ bool GDType::bind_method(MethodBind *p_method, bool p_take_ownership) {if (p_take_ownership) {owned_method_map.push_back(p_method);}+ property_map.insert(p_method->get_name(), Property(Property::Type::METHOD));+ self_property_map.insert(p_method->get_name(), Property(Property::Type::METHOD));+return true;}@@ -158,3 +171,49 @@ void GDType::set_method_flags(const StringName &p_method, int p_flags) {const_cast<MethodBind *>(*method)->set_hint_flags(p_flags);}++void GDType::add_property(const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter,+ int p_index) {+ ERR_FAIL_COND(!Thread::is_main_thread());+ ERR_FAIL_COND(init_state != InitState::MUTABLE);++ ERR_FAIL_COND_MSG(property_map.has(p_pinfo.name), vformat("Object '%s' already has property '%s'.", get_name(), p_pinfo.name));++ const MethodBind *const *mb_set = nullptr;+ if (p_setter) {+ mb_set = get_method_map().getptr(p_setter);++ ERR_FAIL_NULL_MSG(mb_set, vformat("Invalid setter '%s::%s' for property '%s'.", get_name(), p_setter, p_pinfo.name));+ int exp_args = 1 + (p_index >= 0 ? 1 : 0);+ ERR_FAIL_COND_MSG((*mb_set)->get_argument_count() != exp_args, vformat("Invalid function for setter '%s::%s' for property '%s'.", get_name(), p_setter, p_pinfo.name));+ }++ const MethodBind *const *mb_get = nullptr;+ if (p_getter) {+ mb_get = get_method_map().getptr(p_getter);++ ERR_FAIL_NULL_MSG(mb_get, vformat("Invalid getter '%s::%s' for property '%s'.", get_name(), p_getter, p_pinfo.name));+ int exp_args = 0 + (p_index >= 0 ? 1 : 0);+ ERR_FAIL_COND_MSG((*mb_get)->get_argument_count() != exp_args, vformat("Invalid function for getter '%s::%s' for property '%s'.", get_name(), p_getter, p_pinfo.name));+ }++ PropertyInfo *info = memnew(PropertyInfo(p_pinfo));++ Property::SetGet psg;+ psg.property_info = info;+ psg.setter = mb_set ? *mb_set : nullptr;+ psg.getter = mb_get ? *mb_get : nullptr;+ psg.index = p_index;++ property_map.insert(p_pinfo.name, Property(Property::Type::SETGET, psg));+ self_property_map.insert(p_pinfo.name, Property(Property::Type::SETGET, psg));+ ordered_self_properties.push_back(info);+}++void GDType::add_to_ordered_properties(const PropertyInfo &p_pinfo) {+ ERR_FAIL_COND(!Thread::is_main_thread());+ ERR_FAIL_COND(init_state != InitState::MUTABLE);++ PropertyInfo *info = memnew(PropertyInfo(p_pinfo));+ ordered_self_properties.push_back(info);+}diff --git a/core/object/gdtype.h b/core/object/gdtype.hindex c0f82a542db..703190b41a5 100644--- a/core/object/gdtype.h+++ b/core/object/gdtype.h@@ -51,6 +51,41 @@ public:bool is_bitfield = false;};+ struct Property {+ enum class Type {+ SETGET,+ INTEGER_CONSTANT,+ METHOD,+ SIGNAL+ };++ struct SetGet {+ const PropertyInfo *property_info;+ const MethodBind *setter;+ const MethodBind *getter;+ int index;+ };++ union Payload {+ int64_t integer = 0;+ SetGet setget;+ };++ Type type;+ Payload payload;++ Property &operator=(const Property &p) = default;++ Property(const Property &) = default;+ Property(Type p_type, const SetGet &p_pointer) : type(p_type) {+ payload.setget = p_pointer;+ }+ Property(Type p_type, int64_t p_int) : type(p_type) {+ payload.integer = p_int;+ }+ Property(Type p_type) : type(p_type) {}+ };+protected:const GDType *super_type;mutable InitState init_state = InitState::UNINITIALIZED;@@ -75,6 +110,11 @@ protected:// bind_method supports binding non-owned methods.LocalVector<MethodBind *> owned_method_map;+ /// Contains all properties that can be obtained or set with `object.property`.+ AHashMap<StringName, Property> property_map;+ AHashMap<StringName, Property> self_property_map;+ LocalVector<const PropertyInfo *> ordered_self_properties;+public:GDType(const GDType *p_super_type, StringName p_name);~GDType();@@ -101,4 +141,9 @@ public:bool bind_method(MethodBind *p_method, bool p_take_ownership = true);void set_method_flags(const StringName &p_method, int p_flags);const AHashMap<StringName, const MethodBind *> &get_method_map(bool p_no_inheritance = false) const { return p_no_inheritance ? self_method_map : method_map; }++ void add_property(const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index);+ void add_to_ordered_properties(const PropertyInfo &p_pinfo);+ const AHashMap<StringName, Property> &get_property_map(bool p_no_inheritance = false) const { return p_no_inheritance ? self_property_map : property_map; }+ const LocalVector<const PropertyInfo *> &get_ordered_self_properties() const { return ordered_self_properties; }};diff --git a/core/object/object.cpp b/core/object/object.cppindex ab3a4742667..efbca035d26 100644--- a/core/object/object.cpp+++ b/core/object/object.cpp@@ -220,10 +220,8 @@ void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid}// Try built-in setter.- {- if (ClassDB::set_property(this, p_name, p_value, r_valid)) {- return;- }+ if (set_native(p_name, p_value, r_valid)) {+ return;}if (p_name == CoreStringName(script)) {@@ -291,13 +289,8 @@ Variant Object::get(const StringName &p_name, bool *r_valid) const {}// Try built-in getter.- {- if (ClassDB::get_property(const_cast<Object *>(this), p_name, ret)) {- if (r_valid) {- *r_valid = true;- }- return ret;- }+ if (Variant value; get_native(p_name, value, r_valid)) {+ return value;}if (p_name == CoreStringName(script)) {@@ -353,6 +346,122 @@ Variant Object::get(const StringName &p_name, bool *r_valid) const {}}+bool Object::set_native(const StringName &p_name, const Variant &p_value, bool *r_valid) {+ const GDType::Property *property = get_gdtype().get_property_map().getptr(p_name);+ if (property) {+ switch (property->type) {+ case GDType::Property::Type::SETGET: {+ const GDType::Property::SetGet &psg = property->payload.setget;+ if (!psg.setter) {+ if (r_valid) {+ *r_valid = false;+ }+ return true;+ }++ Callable::CallError ce;++ if (psg.index >= 0) {+ Variant index = psg.index;+ const Variant *arg[2] = { &index, &p_value };+ //p_object->call(psg->setter,arg,2,ce);+ psg.setter->call(this, arg, 2, ce);+ } else {+ const Variant *arg[1] = { &p_value };+ psg.setter->call(this, arg, 1, ce);+ }++ if (r_valid) {+ *r_valid = ce.error == Callable::CallError::CALL_OK;+ }+ return true;+ }+ default: {+ // All other properties are unsettable.+ if (r_valid) {+ *r_valid = false;+ }+ return true;+ }+ }+ }++ return false;+}++bool Object::get_native(const StringName &p_name, Variant &r_value, bool *r_valid) const {+ const GDType::Property *property = get_gdtype().get_property_map().getptr(p_name);+ if (property) {+ switch (property->type) {+ case GDType::Property::Type::SETGET: {+ const GDType::Property::SetGet &psg = property->payload.setget;+ if (!psg.getter) {+ if (r_valid) {+ *r_valid = true; // Set to true for compat reasons.+ }+ r_value = Variant();+ return true;+ }++ Callable::CallError ce;+ if (psg.index >= 0) {+ Variant index = psg.index;+ const Variant *arg[1] = { &index };+ r_value = psg.getter->call(const_cast<Object *>(this), arg, 1, ce);+ } else {+ r_value = psg.getter->call(const_cast<Object *>(this), nullptr, 0, ce);+ }++ if (ce.error != Callable::CallError::CALL_OK) {+ if (r_valid) {+ *r_valid = false;+ }+ r_value = Variant();+ } else if (r_valid) {+ *r_valid = true;+ }+ return true;+ }+ case GDType::Property::Type::INTEGER_CONSTANT: {+ if (r_valid) {+ *r_valid = true;+ }+ r_value = property->payload.integer;+ return true;+ }+ case GDType::Property::Type::METHOD: {+ if (r_valid) {+ *r_valid = true;+ }+ r_value = Callable(this, p_name);+ return true;+ }+ case GDType::Property::Type::SIGNAL: {+ if (r_valid) {+ *r_valid = true;+ }+ r_value = Signal(this, p_name);+ return true;+ }+ }+ }++ // The "free()" method is special, so we assume it exists and return a Callable.+ if (p_name == CoreStringName(free_)) {+ if (r_valid) {+ *r_valid = true;+ }++ r_value = Callable(this, p_name);+ return true;+ }++ if (r_valid) {+ *r_valid = false;+ }+ return false;+}+void Object::set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid) {if (p_names.is_empty()) {if (r_valid) {diff --git a/core/object/object.h b/core/object/object.hindex 1096d13730f..f5452722516 100644--- a/core/object/object.h+++ b/core/object/object.h@@ -690,6 +690,10 @@ public:void set(const StringName &p_name, const Variant &p_value, bool *r_valid = nullptr);Variant get(const StringName &p_name, bool *r_valid = nullptr) const;+ /// Like set/get but only uses the internal path. Used from ClassDB::set_property and for GDScript optimization.+ bool set_native(const StringName &p_name, const Variant &p_value, bool *r_valid = nullptr);+ bool get_native(const StringName &p_name, Variant &r_value, bool *r_valid = nullptr) const;+void set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid = nullptr);Variant get_indexed(const Vector<StringName> &p_names, bool *r_valid = nullptr) const;diff --git a/editor/docks/scene_tree_dock.cpp b/editor/docks/scene_tree_dock.cppindex 7d0cb04623d..da29213f027 100644--- a/editor/docks/scene_tree_dock.cpp+++ b/editor/docks/scene_tree_dock.cpp@@ -5182,7 +5182,7 @@ SceneTreeDock::SceneTreeDock(Node *p_scene_root, EditorSelection *p_editor_selecreparent_dialog = memnew(ReparentDialog);add_child(reparent_dialog);- reparent_dialog->connect("reparent", callable_mp(this, &SceneTreeDock::_node_reparent));+ reparent_dialog->connect("reparent_requested", callable_mp(this, &SceneTreeDock::_node_reparent));accept = memnew(AcceptDialog);add_child(accept);diff --git a/editor/inspector/property_selector.cpp b/editor/inspector/property_selector.cppindex 19d9b1dad65..e72ab829eba 100644--- a/editor/inspector/property_selector.cpp+++ b/editor/inspector/property_selector.cpp@@ -188,10 +188,34 @@ void PropertySelector::_update_search() {}}+ // Skip getters and setters of properties because users will usually use the property instead.+ HashSet<StringName> methods_to_skip;+ for (const KeyValue<StringName, GDType::Property> &kv : ClassDB::get_gdtype(base_type)->get_property_map()) {+ if (kv.value.type != GDType::Property::Type::SETGET) {+ continue; // Not relevant.+ }+ const GDType::Property::SetGet &psg = kv.value.payload.setget;+ if (psg.index != -1 || (psg.property_info->usage & PROPERTY_USAGE_INTERNAL)) {+ continue; // Not exposed.+ }+ if (psg.setter) {+ methods_to_skip.insert(psg.setter->get_name());+ }+ if (psg.getter) {+ methods_to_skip.insert(psg.getter->get_name());+ }+ }+StringName base = base_type;while (base) {methods.push_back(MethodInfo("*" + String(base)));- ClassDB::get_method_list(base, &methods, true, true);+ List<MethodInfo> class_methods;+ ClassDB::get_method_list(base, &class_methods, true);+ for (const MethodInfo &mi : class_methods) {+ if (!methods_to_skip.has(mi.name)) {+ methods.push_back(mi);+ }+ }base = ClassDB::get_parent_class(base);}}diff --git a/editor/scene/reparent_dialog.cpp b/editor/scene/reparent_dialog.cppindex 07475df5420..cfaf1b30ebf 100644--- a/editor/scene/reparent_dialog.cpp+++ b/editor/scene/reparent_dialog.cpp@@ -54,7 +54,7 @@ void ReparentDialog::_cancel() {void ReparentDialog::_reparent() {if (tree->get_selected()) {- emit_signal(SNAME("reparent"), tree->get_selected()->get_path(), keep_transform->is_pressed());+ emit_signal(SNAME("reparent_requested"), tree->get_selected()->get_path(), keep_transform->is_pressed());hide();}}@@ -67,7 +67,7 @@ void ReparentDialog::set_current(const HashSet<Node *> &p_selection) {void ReparentDialog::_bind_methods() {ClassDB::bind_method("_cancel", &ReparentDialog::_cancel);- ADD_SIGNAL(MethodInfo("reparent", PropertyInfo(Variant::NODE_PATH, "path"), PropertyInfo(Variant::BOOL, "keep_global_xform")));+ ADD_SIGNAL(MethodInfo("reparent_requested", PropertyInfo(Variant::NODE_PATH, "path"), PropertyInfo(Variant::BOOL, "keep_global_xform")));}ReparentDialog::ReparentDialog() {diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cppindex fdf5aca2500..cedf17f124e 100644--- a/modules/gdscript/gdscript_editor.cpp+++ b/modules/gdscript/gdscript_editor.cpp@@ -1479,8 +1479,26 @@ static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_basebool only_static = base_type.is_meta_type && !Engine::get_singleton()->has_singleton(type);+ // Skip getters and setters of properties because users will usually use the property instead.+ HashSet<StringName> methods_to_skip;+ for (const KeyValue<StringName, GDType::Property> &kv : ClassDB::get_gdtype(type)->get_property_map()) {+ if (kv.value.type != GDType::Property::Type::SETGET) {+ continue; // Not relevant.+ }+ const GDType::Property::SetGet &psg = kv.value.payload.setget;+ if (psg.index != -1 || (psg.property_info->usage & PROPERTY_USAGE_INTERNAL)) {+ continue; // Not exposed.+ }+ if (psg.setter) {+ methods_to_skip.insert(psg.setter->get_name());+ }+ if (psg.getter) {+ methods_to_skip.insert(psg.getter->get_name());+ }+ }+List<MethodInfo> methods;- ClassDB::get_method_list(type, &methods, false, true);+ ClassDB::get_method_list(type, &methods, false);for (const MethodInfo &E : methods) {if (only_static && (E.flags & METHOD_FLAG_STATIC) == 0) {continue;@@ -1488,6 +1506,9 @@ static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_baseif (E.name.begins_with("_")) {continue;}+ if (methods_to_skip.has(E.name)) {+ continue;+ }int location = p_recursion_depth + _get_method_location(type, E.name);ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);if (p_add_braces) {diff --git a/modules/mono/class_db_api_json.cpp b/modules/mono/class_db_api_json.cppindex 44ad1610f98..c7670e32646 100644--- a/modules/mono/class_db_api_json.cpp+++ b/modules/mono/class_db_api_json.cpp@@ -179,10 +179,11 @@ void class_db_api_to_json(const String &p_output_file, ClassDB::APIType p_api) {List<StringName> snames;- for (const KeyValue<StringName, ClassDB::PropertySetGet> &F : t->property_setget) {- snames.push_back(F.key);+ for (const KeyValue<StringName, GDType::Property> &kv : t->gdtype->get_property_map(true)) {+ if (kv.value.type == GDType::Property::Type::SETGET) {+ snames.push_back(kv.key);+ }}-snames.sort_custom<StringName::AlphCompare>();Array properties;@@ -191,11 +192,12 @@ void class_db_api_to_json(const String &p_output_file, ClassDB::APIType p_api) {Dictionary property_dict;properties.push_back(property_dict);- ClassDB::PropertySetGet *psg = t->property_setget.getptr(F);+ const GDType::Property &property = t->gdtype->get_property_map(true)[F];+ const GDType::Property::SetGet &psg = property.payload.setget;property_dict["name"] = F;- property_dict["setter"] = psg->setter;- property_dict["getter"] = psg->getter;+ property_dict["setter"] = psg.setter;+ property_dict["getter"] = psg.getter;}if (!properties.is_empty()) {@@ -206,15 +208,15 @@ void class_db_api_to_json(const String &p_output_file, ClassDB::APIType p_api) {Array property_list;//property list- for (const PropertyInfo &F : t->property_list) {+ for (const PropertyInfo *F : t->gdtype->get_ordered_self_properties()) {Dictionary property_dict;property_list.push_back(property_dict);- property_dict["name"] = F.name;- property_dict["type"] = F.type;- property_dict["hint"] = F.hint;- property_dict["hint_string"] = F.hint_string;- property_dict["usage"] = F.usage;+ property_dict["name"] = F->name;+ property_dict["type"] = F->type;+ property_dict["hint"] = F->hint;+ property_dict["hint_string"] = F->hint_string;+ property_dict["usage"] = F->usage;}if (!property_list.is_empty()) {