Merge pull request #73178 from KoBeWi/tinder_for_types
Fix `tween_method()` type validation
This commit is contained in:
commit
e3b07bf7b8
@ -60,6 +60,20 @@ void Tweener::_bind_methods() {
|
|||||||
ADD_SIGNAL(MethodInfo("finished"));
|
ADD_SIGNAL(MethodInfo("finished"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Tween::_validate_type_match(const Variant &p_from, Variant &r_to) {
|
||||||
|
if (p_from.get_type() != r_to.get_type()) {
|
||||||
|
// Cast r_to between double and int to avoid minor annoyances.
|
||||||
|
if (p_from.get_type() == Variant::FLOAT && r_to.get_type() == Variant::INT) {
|
||||||
|
r_to = double(r_to);
|
||||||
|
} else if (p_from.get_type() == Variant::INT && r_to.get_type() == Variant::FLOAT) {
|
||||||
|
r_to = int(r_to);
|
||||||
|
} else {
|
||||||
|
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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void Tween::_start_tweeners() {
|
void Tween::_start_tweeners() {
|
||||||
if (tweeners.is_empty()) {
|
if (tweeners.is_empty()) {
|
||||||
dead = true;
|
dead = true;
|
||||||
@ -85,16 +99,8 @@ Ref<PropertyTweener> Tween::tween_property(Object *p_target, NodePath p_property
|
|||||||
ERR_FAIL_COND_V_MSG(!valid, nullptr, "Tween invalid. Either finished or created outside scene tree.");
|
ERR_FAIL_COND_V_MSG(!valid, nullptr, "Tween invalid. Either finished or created outside scene tree.");
|
||||||
ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first.");
|
ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first.");
|
||||||
|
|
||||||
Variant::Type property_type = p_target->get_indexed(p_property.get_as_property_path().get_subnames()).get_type();
|
if (!_validate_type_match(p_target->get_indexed(p_property.get_as_property_path().get_subnames()), p_to)) {
|
||||||
if (property_type != p_to.get_type()) {
|
return nullptr;
|
||||||
// Cast p_to between double and int to avoid minor annoyances.
|
|
||||||
if (property_type == Variant::FLOAT && p_to.get_type() == Variant::INT) {
|
|
||||||
p_to = double(p_to);
|
|
||||||
} else if (property_type == Variant::INT && p_to.get_type() == Variant::FLOAT) {
|
|
||||||
p_to = int(p_to);
|
|
||||||
} else {
|
|
||||||
ERR_FAIL_V_MSG(Ref<PropertyTweener>(), "Type mismatch between property and final value: " + Variant::get_type_name(property_type) + " and " + Variant::get_type_name(p_to.get_type()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ref<PropertyTweener> tweener = memnew(PropertyTweener(p_target, p_property, p_to, p_duration));
|
Ref<PropertyTweener> tweener = memnew(PropertyTweener(p_target, p_property, p_to, p_duration));
|
||||||
@ -124,6 +130,10 @@ Ref<MethodTweener> Tween::tween_method(Callable p_callback, Variant p_from, Vari
|
|||||||
ERR_FAIL_COND_V_MSG(!valid, nullptr, "Tween invalid. Either finished or created outside scene tree.");
|
ERR_FAIL_COND_V_MSG(!valid, nullptr, "Tween invalid. Either finished or created outside scene tree.");
|
||||||
ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first.");
|
ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first.");
|
||||||
|
|
||||||
|
if (!_validate_type_match(p_from, p_to)) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
Ref<MethodTweener> tweener = memnew(MethodTweener(p_callback, p_from, p_to, p_duration));
|
Ref<MethodTweener> tweener = memnew(MethodTweener(p_callback, p_from, p_to, p_duration));
|
||||||
append(tweener);
|
append(tweener);
|
||||||
return tweener;
|
return tweener;
|
||||||
|
@ -47,6 +47,7 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
||||||
Ref<Tween> tween;
|
Ref<Tween> tween;
|
||||||
double elapsed_time = 0;
|
double elapsed_time = 0;
|
||||||
bool finished = false;
|
bool finished = false;
|
||||||
@ -125,6 +126,7 @@ private:
|
|||||||
|
|
||||||
void _start_tweeners();
|
void _start_tweeners();
|
||||||
void _stop_internal(bool p_reset);
|
void _stop_internal(bool p_reset);
|
||||||
|
bool _validate_type_match(const Variant &p_from, Variant &r_to);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
Loading…
Reference in New Issue
Block a user