From f301e6cc0ffd82dadcd2e72813d45d6691a34b7d Mon Sep 17 00:00:00 2001 From: Boondorl Date: Thu, 23 Jan 2025 10:21:26 -0500 Subject: [PATCH] Improve Behavior Tick() behavior Collect all objects first to account for the map being modified mid-iteration. --- src/playsim/actor.h | 2 +- src/playsim/p_mobj.cpp | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/playsim/actor.h b/src/playsim/actor.h index f5bfefb5a9..ffe48fdfd0 100644 --- a/src/playsim/actor.h +++ b/src/playsim/actor.h @@ -1441,7 +1441,7 @@ public: auto b = Behaviors.CheckKey(type.TypeName); return b != nullptr && *b != nullptr && !((*b)->ObjectFlags & OF_EuthanizeMe) ? *b : nullptr; } - DObject* AddBehavior(const PClass& type); + DObject* AddBehavior(PClass& type); bool RemoveBehavior(const PClass& type); void TickBehaviors(); void MoveBehaviors(AActor& from); diff --git a/src/playsim/p_mobj.cpp b/src/playsim/p_mobj.cpp index 743843852f..9fe615d0dc 100644 --- a/src/playsim/p_mobj.cpp +++ b/src/playsim/p_mobj.cpp @@ -497,7 +497,7 @@ DEFINE_ACTION_FUNCTION_NATIVE(AActor, RemoveBehavior, RemoveBehavior) ACTION_RETURN_BOOL(self->RemoveBehavior(*type)); } -DObject* AActor::AddBehavior(const PClass& type) +DObject* AActor::AddBehavior(PClass& type) { if (type.bAbstract || !type.IsDescendantOf(NAME_Behavior)) return nullptr; @@ -505,7 +505,7 @@ DObject* AActor::AddBehavior(const PClass& type) auto b = FindBehavior(type); if (b == nullptr) { - b = Create(); + b = type.CreateNew(); if (b == nullptr) return nullptr; @@ -558,6 +558,7 @@ DEFINE_ACTION_FUNCTION_NATIVE(AActor, AddBehavior, AddBehavior) void AActor::TickBehaviors() { TArray toRemove = {}; + TArray toTick = {}; TMap::Iterator it = { Behaviors }; TMap::Pair* pair = nullptr; @@ -570,6 +571,11 @@ void AActor::TickBehaviors() continue; } + toTick.Push(b); + } + + for (auto& b : toTick) + { auto& owner = b->PointerVar(NAME_Owner); if (owner != this) {