From 6afcaa92638624bdffc40a0a2097a7b798512e63 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 22 Nov 2022 18:09:50 +0800 Subject: [PATCH] Enabled state of scripts are now serialised --- SHADE_Managed/src/Scripts/Script.cxx | 8 +++++ SHADE_Managed/src/Scripts/Script.hxx | 9 ++++++ SHADE_Managed/src/Scripts/ScriptStore.cxx | 2 +- .../Serialisation/SerialisationUtilities.cxx | 30 +++++++++++++++++-- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/SHADE_Managed/src/Scripts/Script.cxx b/SHADE_Managed/src/Scripts/Script.cxx index 2ee7dbf7..294f4096 100644 --- a/SHADE_Managed/src/Scripts/Script.cxx +++ b/SHADE_Managed/src/Scripts/Script.cxx @@ -258,6 +258,14 @@ namespace SHADE Script::Script() : OnGizmosDrawOverriden { false } {} + + /*---------------------------------------------------------------------------------*/ + /* Manipulation Functions */ + /*---------------------------------------------------------------------------------*/ + void Script::SetEnabledWithoutEvents(bool enable) + { + enabled = enable; + } /*---------------------------------------------------------------------------------*/ /* Virtual "All-Time" Lifecycle Functions */ diff --git a/SHADE_Managed/src/Scripts/Script.hxx b/SHADE_Managed/src/Scripts/Script.hxx index 62c5015c..8fc36544 100644 --- a/SHADE_Managed/src/Scripts/Script.hxx +++ b/SHADE_Managed/src/Scripts/Script.hxx @@ -326,6 +326,15 @@ namespace SHADE /// Information on the collision event. void OnTriggerExit(CollisionInfo collision); + /*-----------------------------------------------------------------------------*/ + /* Manipulation Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Function to set the enabled state of this script without triggering events. + /// + /// Whether to enable or disable the script. + void SetEnabledWithoutEvents(bool enable); + protected: /*-----------------------------------------------------------------------------*/ /* Constructors */ diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index 96eb7361..d3d989cc 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -744,7 +744,7 @@ namespace SHADE for (YAML::Node& node : *yamlNode) { // Get the name of the script - if (!node["Type"]) + if (!node["Type"].IsDefined()) { Debug::LogWarning("[ScriptStore] Script with no type detected, skipping."); continue; diff --git a/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx b/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx index 8a36613c..2bf05bc5 100644 --- a/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx +++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx @@ -21,11 +21,13 @@ of DigiPen Institute of Technology is prohibited. #include "Assets/FontAsset.hxx" #include "Assets/MaterialAsset.hxx" #include "Assets/MeshAsset.hxx" +#include "Scripts/Script.hxx" /*-------------------------------------------------------------------------------------*/ /* File-Level Constants */ /*-------------------------------------------------------------------------------------*/ static const std::string_view SCRIPT_TYPE_YAMLTAG = "Type"; +static const std::string_view SCRIPT_ENABLED_YAMLTAG = "Enabled"; /*-------------------------------------------------------------------------------------*/ /* Function Definitions */ @@ -39,10 +41,19 @@ namespace SHADE { using namespace System::Reflection; + // Obtain script + Script^ script = safe_cast(object); + if (script == nullptr) + { + Debug::LogWarning("[SerialisationUtilities] Attempted to serialise an object that is not a script!"); + return; + } + // Create YAML object YAML::Node scriptNode; scriptNode.SetStyle(YAML::EmitterStyle::Block); scriptNode[SCRIPT_TYPE_YAMLTAG.data()] = Convert::ToNative(object->GetType()->FullName); + scriptNode[SCRIPT_ENABLED_YAMLTAG.data()] = script->Enabled; // Get all fields System::Collections::Generic::IEnumerable^ fields = ReflectionUtilities::GetInstanceFields(object); @@ -72,7 +83,7 @@ namespace SHADE { using namespace System::Reflection; - // Load the YAML + // Error Checking if (!yamlNode.IsMap()) { // Invalid @@ -83,6 +94,21 @@ namespace SHADE ); return; } + + // Get the script + Script^ script = safe_cast(object); + if (script == nullptr) + { + Debug::LogWarning("[SerialisationUtilities] Attempted to deserialise an object that is not a script!"); + return; + } + + // Set enabled state + if (yamlNode[SCRIPT_ENABLED_YAMLTAG.data()].IsDefined()) + { + script->SetEnabledWithoutEvents(yamlNode[SCRIPT_ENABLED_YAMLTAG.data()].as()); + } + // Get all fields System::Collections::Generic::IEnumerable^ fields = ReflectionUtilities::GetInstanceFields(object); for each (FieldInfo^ field in fields) @@ -95,7 +121,7 @@ namespace SHADE // Deserialise const std::string FIELD_NAME = Convert::ToNative(field->Name); - if (yamlNode[FIELD_NAME]) + if (yamlNode[FIELD_NAME].IsDefined()) { writeYamlIntoField(field, object, yamlNode[FIELD_NAME]); }