From af2f62a278a9e1188f244a6b86aa6a69d1e905d6 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 8 Feb 2020 09:39:36 +0100 Subject: [PATCH] When calling script hooks from another script, re-use existing script context. --- .../AngelScript/AngelScriptScript.hpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/ScriptResolving/AngelScript/AngelScriptScript.hpp b/src/ScriptResolving/AngelScript/AngelScriptScript.hpp index a50080a..7e01a2c 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptScript.hpp +++ b/src/ScriptResolving/AngelScript/AngelScriptScript.hpp @@ -33,7 +33,14 @@ public: auto s = _type->Get##name(); \ if (!s.Exists) \ return; \ - auto ctx = _ctxPool->RequestContext(); \ + auto ctx = asGetActiveContext(); \ + bool newContext = false; \ + if (ctx == nullptr) { \ + ctx = _ctxPool->RequestContext(); \ + newContext = true; \ + } else { \ + ctx->PushState(); \ + } \ ctx->Prepare(s.Function); \ ctx->SetObject(_obj); \ setup; \ @@ -41,11 +48,15 @@ public: if (scriptResult != 0) { \ throw CreatureException("Script didn't finish properly; message " + std::to_string(scriptResult)); \ } \ - _ctxPool->ReturnContextToPool(ctx); + if (newContext) { \ + _ctxPool->ReturnContextToPool(ctx); \ + } else { \ + ctx->PopState(); \ + } - void Stack() override { CALLHOOK(Stack,); } + void Stack() override { CALLHOOK(Stack, ); } - void OnRemove() override { CALLHOOK(OnRemove,); } + void OnRemove() override { CALLHOOK(OnRemove, ); } void OnBeforeTurn(const CreatureLib::Battling::BaseTurnChoice* choice) override { throw NotImplementedException(); // TODO