From 3acc0779a418783fb627f62f482eb4c108690fce Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Fri, 21 Oct 2022 10:05:22 +0800 Subject: [PATCH] Fix error when having BoneAttachment before PhysicalBone --- scene/3d/skeleton.cpp | 29 ++++++++++++++++++----------- scene/3d/skeleton.h | 2 +- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/scene/3d/skeleton.cpp b/scene/3d/skeleton.cpp index 2d467eb29e6..9bc9222b37f 100644 --- a/scene/3d/skeleton.cpp +++ b/scene/3d/skeleton.cpp @@ -140,8 +140,9 @@ bool Skeleton::_get(const StringName &p_path, Variant &r_ret) const { } else if (what == "bound_children") { Array children; - for (const List::Element *E = bones[which].nodes_bound.front(); E; E = E->next()) { - Object *obj = ObjectDB::get_instance(E->get()); + const LocalVectori &nodes = bones[which].nodes_bound; + for (int i = 0; i < nodes.size(); i++) { + Object *obj = ObjectDB::get_instance(nodes[i]); ERR_CONTINUE(!obj); Node *node = Object::cast_to(obj); ERR_CONTINUE(!node); @@ -290,8 +291,9 @@ void Skeleton::_notification(int p_what) { b.global_pose_override_amount = 0.0; } - for (List::Element *E = b.nodes_bound.front(); E; E = E->next()) { - Object *obj = ObjectDB::get_instance(E->get()); + const LocalVectori &nodes = b.nodes_bound; + for (int j = 0; j < nodes.size(); j++) { + Object *obj = ObjectDB::get_instance(nodes[j]); ERR_CONTINUE(!obj); Spatial *sp = Object::cast_to(obj); ERR_CONTINUE(!sp); @@ -525,10 +527,8 @@ void Skeleton::bind_child_node_to_bone(int p_bone, Node *p_node) { uint32_t id = p_node->get_instance_id(); - for (const List::Element *E = bones[p_bone].nodes_bound.front(); E; E = E->next()) { - if (E->get() == id) { - return; // already here - } + if (bones[p_bone].nodes_bound.find(id) != -1) { + return; // Already here. } bones.write[p_bone].nodes_bound.push_back(id); @@ -538,13 +538,20 @@ void Skeleton::unbind_child_node_from_bone(int p_bone, Node *p_node) { ERR_FAIL_INDEX(p_bone, bones.size()); uint32_t id = p_node->get_instance_id(); - bones.write[p_bone].nodes_bound.erase(id); + + int index = bones[p_bone].nodes_bound.find(id); + if (index == -1) { + return; // Doesn't exist in the first place. + } + + bones.write[p_bone].nodes_bound.remove(index); } void Skeleton::get_bound_child_nodes_to_bone(int p_bone, List *p_bound) const { ERR_FAIL_INDEX(p_bone, bones.size()); - for (const List::Element *E = bones[p_bone].nodes_bound.front(); E; E = E->next()) { - Object *obj = ObjectDB::get_instance(E->get()); + const LocalVectori &nodes = bones[p_bone].nodes_bound; + for (int i = 0; i < nodes.size(); i++) { + Object *obj = ObjectDB::get_instance(nodes[i]); ERR_CONTINUE(!obj); p_bound->push_back(Object::cast_to(obj)); } diff --git a/scene/3d/skeleton.h b/scene/3d/skeleton.h index 1b8d1630836..17d89d1bde1 100644 --- a/scene/3d/skeleton.h +++ b/scene/3d/skeleton.h @@ -102,7 +102,7 @@ private: PhysicalBone *cache_parent_physical_bone; #endif // _3D_DISABLED - List nodes_bound; + LocalVectori nodes_bound; Bone() { parent = -1;