Fixed bug where scripts loaded after a scene change would not have serialised data

This commit is contained in:
Kah Wei 2022-11-25 00:07:19 +08:00
parent 9ada998151
commit 50232cd15f
2 changed files with 15 additions and 1 deletions

View File

@ -74,7 +74,7 @@ namespace SHADE
// Add the script in // Add the script in
script->Initialize(GameObject(entity)); script->Initialize(GameObject(entity));
entityScriptList->Insert(System::Math::Clamp(index, 0, entityScriptList->Count), script); entityScriptList->Insert(System::Math::Clamp(index, 0, entityScriptList->Count), script);
if (Application::IsPlaying && !SHSceneManager::HasSceneChanged()) if (Application::IsPlaying && !isDeserialising)
{ {
// Only call immediately if we are in game and is not loading another scene // Only call immediately if we are in game and is not loading another scene
script->Awake(); script->Awake();
@ -423,6 +423,8 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
void ScriptStore::Init() void ScriptStore::Init()
{ {
isDeserialising = false;
// Create an enumerable list of script types // Create an enumerable list of script types
refreshScriptTypeList(); refreshScriptTypeList();
// Get stored methods for interop variants of functions // Get stored methods for interop variants of functions
@ -724,6 +726,10 @@ namespace SHADE
bool ScriptStore::DeserialiseScripts(Entity entity, System::IntPtr yamlNodePtr) bool ScriptStore::DeserialiseScripts(Entity entity, System::IntPtr yamlNodePtr)
{ {
SAFE_NATIVE_CALL_BEGIN SAFE_NATIVE_CALL_BEGIN
// Flag that deserialization processs is ongoing
isDeserialising = true;
// Convert to pointer // Convert to pointer
YAML::Node* yamlNode = reinterpret_cast<YAML::Node*>(yamlNodePtr.ToPointer()); YAML::Node* yamlNode = reinterpret_cast<YAML::Node*>(yamlNodePtr.ToPointer());
@ -765,9 +771,16 @@ namespace SHADE
Debug::LogWarning("[ScriptStore] Script with unloaded type detected, skipping."); Debug::LogWarning("[ScriptStore] Script with unloaded type detected, skipping.");
} }
} }
// Unset flag for deserialization process
isDeserialising = false;
return true; return true;
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
// Unset flag for deserialization process
isDeserialising = false;
return false; return false;
} }

View File

@ -337,6 +337,7 @@ namespace SHADE
static ScriptSet disposalQueue; static ScriptSet disposalQueue;
static System::Collections::Generic::IEnumerable<System::Type^>^ scriptTypeList; static System::Collections::Generic::IEnumerable<System::Type^>^ scriptTypeList;
static System::Reflection::MethodInfo^ addScriptMethod; static System::Reflection::MethodInfo^ addScriptMethod;
static bool isDeserialising;
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */