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.
This commit is contained in:
kanym 2026-10-18 13:33:21 +02:00
commit 6d9f07217e

View file

@ -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<Character>("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;