From 786f057971468ea4dd187c402a49f47a3fa90d82 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 3 Jan 2021 23:18:53 +0100 Subject: [PATCH] Fix crash on scripting reload when using Anim Graph with custom nodes --- Source/Engine/Animations/Graph/AnimGraph.cpp | 16 +++++++++++++++- Source/Engine/Animations/Graph/AnimGraph.h | 5 ++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Animations/Graph/AnimGraph.cpp b/Source/Engine/Animations/Graph/AnimGraph.cpp index c02161a07..2abbb3ff3 100644 --- a/Source/Engine/Animations/Graph/AnimGraph.cpp +++ b/Source/Engine/Animations/Graph/AnimGraph.cpp @@ -119,8 +119,9 @@ bool AnimGraph::Load(ReadStream* stream, bool loadMeta) // Register for scripts reloading events (only if using any custom nodes) // Handle load event always because anim graph asset may be loaded before game scripts - if (_customNodes.HasItems()) + if (_customNodes.HasItems() && !_isRegisteredForScriptingEvents) { + _isRegisteredForScriptingEvents = true; #if USE_EDITOR Scripting::ScriptsReloading.Bind(this); Scripting::ScriptsReloaded.Bind(this); @@ -131,6 +132,19 @@ bool AnimGraph::Load(ReadStream* stream, bool loadMeta) return false; } +AnimGraph::~AnimGraph() +{ + // Unregister for scripts reloading events (only if using any custom nodes) + if (_isRegisteredForScriptingEvents) + { +#if USE_EDITOR + Scripting::ScriptsReloading.Unbind(this); + Scripting::ScriptsReloaded.Unbind(this); +#endif + Scripting::ScriptsLoaded.Unbind(this); + } +} + bool AnimGraph::onParamCreated(Parameter* p) { if (p->Identifier == ANIM_GRAPH_PARAM_BASE_MODEL_ID) diff --git a/Source/Engine/Animations/Graph/AnimGraph.h b/Source/Engine/Animations/Graph/AnimGraph.h index 34050793a..714f6e9b8 100644 --- a/Source/Engine/Animations/Graph/AnimGraph.h +++ b/Source/Engine/Animations/Graph/AnimGraph.h @@ -733,7 +733,7 @@ private: typedef void (*InitBucketHandler)(AnimGraphInstanceData::Bucket&); - bool _isFunction; + bool _isFunction, _isRegisteredForScriptingEvents; int32 _bucketsCounter; Array _bucketInitializerList; Array _customNodes; @@ -751,9 +751,12 @@ public: , _isFunction(isFunction) , _bucketInitializerList(64) , _owner(owner) + , _isRegisteredForScriptingEvents(false) { } + ~AnimGraph(); + public: ///