From 6d9f07217e371d243e2a29bb79e3b3503627d496 Mon Sep 17 00:00:00 2001 From: kanym Date: Sun, 18 Oct 2026 13:33:21 +0200 Subject: [PATCH] fix(factions): no AI freeze when a targeted piece is destroyed FactionStaticTargetPatch read m_targetStatic as a boxed object, so the null check missed Unity's destroyed-object state. A piece destroyed while a mob targeted it passed the check, and reading its gameObject threw. The throw escaped into MonoUpdaters.FixedUpdate and stopped every AI after that mob, every frame, until the field changed. Cast to Component and use Unity's null check; clear a dead target. --- JotunnModStub/Factions/FactionHostilityPatch.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/JotunnModStub/Factions/FactionHostilityPatch.cs b/JotunnModStub/Factions/FactionHostilityPatch.cs index b445dbe..7fee617 100644 --- a/JotunnModStub/Factions/FactionHostilityPatch.cs +++ b/JotunnModStub/Factions/FactionHostilityPatch.cs @@ -207,14 +207,23 @@ namespace RacesOfValheim { if (!FactionsConfig.Hostility) return; Traverse targetStatic = Traverse.Create(__instance).Field("m_targetStatic"); - if (targetStatic.GetValue() == null) return; + // GetValue() hands back a plain object, and object == null skips Unity's destroyed + // check: a piece smashed or removed while targeted stays non-null here, and touching + // its gameObject throws. That throw escapes into MonoUpdaters.FixedUpdate and stops + // every AI after this one for the frame, every frame, until the field changes. + Component target = targetStatic.GetValue() as Component; + if (target == null) + { + if (targetStatic.GetValue() != null) targetStatic.SetValue(null); + return; + } Character monster = Traverse.Create(__instance).Field("m_character").Value; if (monster == null) return; // A piece an allied race built is never a target, with or without anyone around. // See FactionPieceTruce. - if (targetStatic.GetValue() is Component target && FactionPieceTruce.Protects(target.gameObject, monster)) + if (FactionPieceTruce.Protects(target.gameObject, monster)) { targetStatic.SetValue(null); return;