From 88703d721bfdf02a2c395ecf80b03e4de5a79f0e Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Mon, 23 Dec 2024 16:51:37 -0600 Subject: [PATCH 1/2] Warn if wrong type while setting material parameter. --- Source/Engine/Content/Assets/MaterialBase.cpp | 13 ++++++++++--- Source/Engine/Content/Assets/MaterialBase.h | 5 +++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Source/Engine/Content/Assets/MaterialBase.cpp b/Source/Engine/Content/Assets/MaterialBase.cpp index d537a5746..6f111da68 100644 --- a/Source/Engine/Content/Assets/MaterialBase.cpp +++ b/Source/Engine/Content/Assets/MaterialBase.cpp @@ -25,13 +25,20 @@ Variant MaterialBase::GetParameterValue(const StringView& name) return Variant::Null; } -void MaterialBase::SetParameterValue(const StringView& name, const Variant& value, bool warnIfMissing) +void MaterialBase::SetParameterValue(const StringView& name, const Variant& value, bool warnIfMissing, bool warnIfWrongType) { const auto param = Params.Get(name); if (param) { - param->SetValue(value); - param->SetIsOverride(true); + if (param->GetValue().Type == value.Type) + { + param->SetValue(value); + param->SetIsOverride(true); + } + else if (warnIfWrongType) + { + LOG(Warning, "Material parameter '{0}' in material {1} is type '{2}' and not type '{3}'.", String(name), ToString(), param->GetValue().Type, value.Type); + } } else if (warnIfMissing) { diff --git a/Source/Engine/Content/Assets/MaterialBase.h b/Source/Engine/Content/Assets/MaterialBase.h index 81316b106..3d6487772 100644 --- a/Source/Engine/Content/Assets/MaterialBase.h +++ b/Source/Engine/Content/Assets/MaterialBase.h @@ -65,8 +65,9 @@ public: /// /// The parameter name. /// The value to set. - /// True if warn if parameter is missing, otherwise will do nothing. - API_FUNCTION() void SetParameterValue(const StringView& name, const Variant& value, bool warnIfMissing = true); + /// True to warn if parameter is missing, otherwise will do nothing. + /// True to warn if value is wrong type, otherwise will do nothing. + API_FUNCTION() void SetParameterValue(const StringView& name, const Variant& value, bool warnIfMissing = true, bool warnIfWrongType = true); /// /// Creates the virtual material instance of this material which allows to override any material parameters. From 7eb2088af0ad0c489bdb15df8e486ca6964bd642 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Mon, 6 Jan 2025 15:40:38 -0600 Subject: [PATCH 2/2] Use `Variant::CanCast()` for type check. --- Source/Engine/Content/Assets/MaterialBase.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Content/Assets/MaterialBase.cpp b/Source/Engine/Content/Assets/MaterialBase.cpp index 6f111da68..9751f86d4 100644 --- a/Source/Engine/Content/Assets/MaterialBase.cpp +++ b/Source/Engine/Content/Assets/MaterialBase.cpp @@ -30,7 +30,7 @@ void MaterialBase::SetParameterValue(const StringView& name, const Variant& valu const auto param = Params.Get(name); if (param) { - if (param->GetValue().Type == value.Type) + if (Variant::CanCast(value, param->GetValue().Type)) { param->SetValue(value); param->SetIsOverride(true);