Fix error when having BoneAttachment before PhysicalBone

This commit is contained in:
Haoyu Qiu 2022-10-21 10:05:22 +08:00
parent 28afd32d29
commit 3acc0779a4
2 changed files with 19 additions and 12 deletions

View file

@ -140,8 +140,9 @@ bool Skeleton::_get(const StringName &p_path, Variant &r_ret) const {
} else if (what == "bound_children") { } else if (what == "bound_children") {
Array children; Array children;
for (const List<uint32_t>::Element *E = bones[which].nodes_bound.front(); E; E = E->next()) { const LocalVectori<uint32_t> &nodes = bones[which].nodes_bound;
Object *obj = ObjectDB::get_instance(E->get()); for (int i = 0; i < nodes.size(); i++) {
Object *obj = ObjectDB::get_instance(nodes[i]);
ERR_CONTINUE(!obj); ERR_CONTINUE(!obj);
Node *node = Object::cast_to<Node>(obj); Node *node = Object::cast_to<Node>(obj);
ERR_CONTINUE(!node); ERR_CONTINUE(!node);
@ -290,8 +291,9 @@ void Skeleton::_notification(int p_what) {
b.global_pose_override_amount = 0.0; b.global_pose_override_amount = 0.0;
} }
for (List<uint32_t>::Element *E = b.nodes_bound.front(); E; E = E->next()) { const LocalVectori<uint32_t> &nodes = b.nodes_bound;
Object *obj = ObjectDB::get_instance(E->get()); for (int j = 0; j < nodes.size(); j++) {
Object *obj = ObjectDB::get_instance(nodes[j]);
ERR_CONTINUE(!obj); ERR_CONTINUE(!obj);
Spatial *sp = Object::cast_to<Spatial>(obj); Spatial *sp = Object::cast_to<Spatial>(obj);
ERR_CONTINUE(!sp); 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(); uint32_t id = p_node->get_instance_id();
for (const List<uint32_t>::Element *E = bones[p_bone].nodes_bound.front(); E; E = E->next()) { if (bones[p_bone].nodes_bound.find(id) != -1) {
if (E->get() == id) { return; // Already here.
return; // already here
}
} }
bones.write[p_bone].nodes_bound.push_back(id); 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()); ERR_FAIL_INDEX(p_bone, bones.size());
uint32_t id = p_node->get_instance_id(); 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<Node *> *p_bound) const { void Skeleton::get_bound_child_nodes_to_bone(int p_bone, List<Node *> *p_bound) const {
ERR_FAIL_INDEX(p_bone, bones.size()); ERR_FAIL_INDEX(p_bone, bones.size());
for (const List<uint32_t>::Element *E = bones[p_bone].nodes_bound.front(); E; E = E->next()) { const LocalVectori<uint32_t> &nodes = bones[p_bone].nodes_bound;
Object *obj = ObjectDB::get_instance(E->get()); for (int i = 0; i < nodes.size(); i++) {
Object *obj = ObjectDB::get_instance(nodes[i]);
ERR_CONTINUE(!obj); ERR_CONTINUE(!obj);
p_bound->push_back(Object::cast_to<Node>(obj)); p_bound->push_back(Object::cast_to<Node>(obj));
} }

View file

@ -102,7 +102,7 @@ private:
PhysicalBone *cache_parent_physical_bone; PhysicalBone *cache_parent_physical_bone;
#endif // _3D_DISABLED #endif // _3D_DISABLED
List<uint32_t> nodes_bound; LocalVectori<uint32_t> nodes_bound;
Bone() { Bone() {
parent = -1; parent = -1;