Merge branch 'fix-static-target-null': no AI freeze when a targeted piece is destroyed

This commit is contained in:
kanym 2026-10-19 14:24:31 +02:00
commit 64fbd66987

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;