Skip to content

Commit e5c8fb8

Browse files
committed
Merge pull request godotengine#107005 from TokageItLab/fix-anim-node-param-type
Fix ambiguous AnimationNode's parameter type in default value and make `validate_type_match()` static function
2 parents d83c797 + 678fb63 commit e5c8fb8

File tree

8 files changed

+41
-33
lines changed

8 files changed

+41
-33
lines changed

scene/animation/animation_blend_space_1d.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ Variant AnimationNodeBlendSpace1D::get_parameter_default_value(const StringName
4545
}
4646

4747
if (p_parameter == closest) {
48-
return -1;
48+
return (int)-1;
4949
} else {
50-
return 0;
50+
return 0.0;
5151
}
5252
}
5353

scene/animation/animation_blend_space_2d.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Variant AnimationNodeBlendSpace2D::get_parameter_default_value(const StringName
4646
}
4747

4848
if (p_parameter == closest) {
49-
return -1;
49+
return (int)-1;
5050
} else {
5151
return Vector2();
5252
}

scene/animation/animation_blend_tree.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Variant AnimationNodeAnimation::get_parameter_default_value(const StringName &p_
5555
if (p_parameter == backward) {
5656
return false;
5757
}
58-
return 0;
58+
return 0.0;
5959
}
6060

6161
AnimationNode::NodeTimeInfo AnimationNodeAnimation::get_node_time_info() const {
@@ -438,10 +438,10 @@ Variant AnimationNodeOneShot::get_parameter_default_value(const StringName &p_pa
438438
} else if (p_parameter == active || p_parameter == internal_active) {
439439
return false;
440440
} else if (p_parameter == time_to_restart) {
441-
return -1;
442-
} else {
443-
return 0.0;
441+
return -1.0;
444442
}
443+
444+
return 0.0;
445445
}
446446

447447
bool AnimationNodeOneShot::is_parameter_read_only(const StringName &p_parameter) const {
@@ -774,7 +774,7 @@ Variant AnimationNodeAdd2::get_parameter_default_value(const StringName &p_param
774774
return ret;
775775
}
776776

777-
return 0;
777+
return 0.0;
778778
}
779779

780780
String AnimationNodeAdd2::get_caption() const {
@@ -815,7 +815,7 @@ Variant AnimationNodeAdd3::get_parameter_default_value(const StringName &p_param
815815
return ret;
816816
}
817817

818-
return 0;
818+
return 0.0;
819819
}
820820

821821
String AnimationNodeAdd3::get_caption() const {
@@ -859,7 +859,7 @@ Variant AnimationNodeBlend2::get_parameter_default_value(const StringName &p_par
859859
return ret;
860860
}
861861

862-
return 0; // For blend amount.
862+
return 0.0; // For blend amount.
863863
}
864864

865865
String AnimationNodeBlend2::get_caption() const {
@@ -900,7 +900,7 @@ Variant AnimationNodeBlend3::get_parameter_default_value(const StringName &p_par
900900
return ret;
901901
}
902902

903-
return 0; // For blend amount.
903+
return 0.0; // For blend amount.
904904
}
905905

906906
String AnimationNodeBlend3::get_caption() const {
@@ -940,7 +940,7 @@ Variant AnimationNodeSub2::get_parameter_default_value(const StringName &p_param
940940
return ret;
941941
}
942942

943-
return 0;
943+
return 0.0;
944944
}
945945

946946
String AnimationNodeSub2::get_caption() const {
@@ -1146,7 +1146,7 @@ Variant AnimationNodeTransition::get_parameter_default_value(const StringName &p
11461146
if (p_parameter == prev_xfading) {
11471147
return 0.0;
11481148
} else if (p_parameter == prev_index || p_parameter == current_index) {
1149-
return -1;
1149+
return (int)-1;
11501150
} else {
11511151
return String();
11521152
}

scene/animation/animation_tree.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ void AnimationNode::set_parameter(const StringName &p_name, const Variant &p_val
7777

7878
const AHashMap<StringName, int>::Iterator it = property_cache.find(p_name);
7979
if (it) {
80-
process_state->tree->property_map.get_by_index(it->value).value.first = p_value;
80+
Pair<Variant, bool> &prop = process_state->tree->property_map.get_by_index(it->value).value;
81+
Variant value = p_value;
82+
if (Animation::validate_type_match(prop.first, value)) {
83+
prop.first = value;
84+
}
8185
return;
8286
}
8387

@@ -919,7 +923,11 @@ bool AnimationTree::_set(const StringName &p_name, const Variant &p_value) {
919923
if (is_inside_tree() && property_map[p_name].second) {
920924
return false; // Prevent to set property by user.
921925
}
922-
property_map[p_name].first = p_value;
926+
Pair<Variant, bool> &prop = property_map[p_name];
927+
Variant value = p_value;
928+
if (Animation::validate_type_match(prop.first, value)) {
929+
prop.first = value;
930+
}
923931
return true;
924932
}
925933

scene/animation/tween.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,6 @@ void Tweener::_bind_methods() {
7575
ADD_SIGNAL(MethodInfo("finished"));
7676
}
7777

78-
bool Tween::_validate_type_match(const Variant &p_from, Variant &r_to) {
79-
if (p_from.get_type() != r_to.get_type()) {
80-
// Cast r_to between double and int to avoid minor annoyances.
81-
if (p_from.get_type() == Variant::FLOAT && r_to.get_type() == Variant::INT) {
82-
r_to = double(r_to);
83-
} else if (p_from.get_type() == Variant::INT && r_to.get_type() == Variant::FLOAT) {
84-
r_to = int(r_to);
85-
} else {
86-
ERR_FAIL_V_MSG(false, "Type mismatch between initial and final value: " + Variant::get_type_name(p_from.get_type()) + " and " + Variant::get_type_name(r_to.get_type()));
87-
}
88-
}
89-
return true;
90-
}
91-
9278
void Tween::_start_tweeners() {
9379
if (tweeners.is_empty()) {
9480
dead = true;
@@ -122,7 +108,7 @@ Ref<PropertyTweener> Tween::tween_property(const Object *p_target, const NodePat
122108
const Variant &prop_value = p_target->get_indexed(property_subnames);
123109
#endif
124110

125-
if (!_validate_type_match(prop_value, p_to)) {
111+
if (!Animation::validate_type_match(prop_value, p_to)) {
126112
return nullptr;
127113
}
128114

@@ -153,7 +139,7 @@ Ref<CallbackTweener> Tween::tween_callback(const Callable &p_callback) {
153139
Ref<MethodTweener> Tween::tween_method(const Callable &p_callback, const Variant p_from, Variant p_to, double p_duration) {
154140
CHECK_VALID();
155141

156-
if (!_validate_type_match(p_from, p_to)) {
142+
if (!Animation::validate_type_match(p_from, p_to)) {
157143
return nullptr;
158144
}
159145

@@ -562,7 +548,7 @@ Ref<PropertyTweener> PropertyTweener::from(const Variant &p_value) {
562548
ERR_FAIL_COND_V(tween.is_null(), nullptr);
563549

564550
Variant from_value = p_value;
565-
if (!tween->_validate_type_match(final_val, from_value)) {
551+
if (!Animation::validate_type_match(final_val, from_value)) {
566552
return nullptr;
567553
}
568554

scene/animation/tween.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ class Tween : public RefCounted {
135135

136136
void _start_tweeners();
137137
void _stop_internal(bool p_reset);
138-
bool _validate_type_match(const Variant &p_from, Variant &r_to);
139138

140139
protected:
141140
static void _bind_methods();

scene/resources/animation.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5677,6 +5677,20 @@ bool Animation::is_variant_interpolatable(const Variant p_value) {
56775677
return (type >= Variant::BOOL && type <= Variant::STRING_NAME) || type == Variant::ARRAY || type >= Variant::PACKED_INT32_ARRAY; // PackedByteArray is unsigned, so it would be better to ignore since blending uses float.
56785678
}
56795679

5680+
bool Animation::validate_type_match(const Variant &p_from, Variant &r_to) {
5681+
if (p_from.get_type() != r_to.get_type()) {
5682+
// Cast r_to between double and int to avoid minor annoyances.
5683+
if (p_from.get_type() == Variant::FLOAT && r_to.get_type() == Variant::INT) {
5684+
r_to = double(r_to);
5685+
} else if (p_from.get_type() == Variant::INT && r_to.get_type() == Variant::FLOAT) {
5686+
r_to = int(r_to);
5687+
} else {
5688+
ERR_FAIL_V_MSG(false, "Type mismatch between initial and final value: " + Variant::get_type_name(p_from.get_type()) + " and " + Variant::get_type_name(r_to.get_type()));
5689+
}
5690+
}
5691+
return true;
5692+
}
5693+
56805694
Variant Animation::cast_to_blendwise(const Variant p_value) {
56815695
switch (p_value.get_type()) {
56825696
case Variant::BOOL:

scene/resources/animation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ class Animation : public Resource {
542542

543543
// Helper functions for Variant.
544544
static bool is_variant_interpolatable(const Variant p_value);
545+
static bool validate_type_match(const Variant &p_from, Variant &r_to);
545546

546547
static Variant cast_to_blendwise(const Variant p_value);
547548
static Variant cast_from_blendwise(const Variant p_value, const Variant::Type p_type);

0 commit comments

Comments
 (0)