Skip to content

Fix NullReferenceException for pawns with no needs (OWD compat)#5

Open
junikka wants to merge 1 commit intoaRandomKiwi:mainfrom
junikka:fix/null-needs-compatibility
Open

Fix NullReferenceException for pawns with no needs (OWD compat)#5
junikka wants to merge 1 commit intoaRandomKiwi:mainfrom
junikka:fix/null-needs-compatibility

Conversation

@junikka
Copy link

@junikka junikka commented Mar 25, 2026

Summary

  • Adds null checks for pawn.needs in all guard need-check functions in Utils.cs and ThinkNode_ConditionalNeedPercentageBelow.cs
  • Adds null-safe access for cp.needs.mood.thoughts.memories in Comp_Guard.ReassureSurroundingPawns()
  • Adds null checks for pawn.timetable in ThinkNode_ConditionalShouldGuardSpot, ThinkNode_ConditionalShouldPatrol, and ThinkNode_ConditionalShouldSearchAndKill

Problem

Mods like "One With Death" create pawns (zombies) that have no needs tracker (pawn.needs == null) and possibly no timetable. When these pawns interact with Guards For Me code paths, the game throws NullReferenceException because the code assumes all pawns have needs.

The existing code in Utils.cs correctly null-checks individual needs (e.g., pawn.needs.rest != null) but does not check whether pawn.needs itself is null. The crash happens before reaching those checks.

Changes (1.6 only, 6 files)

File Fix
Utils.cs Added if (pawn.needs == null) return false; to all 7 need-check functions
ThinkNode_ConditionalNeedPercentageBelow.cs Added pawn.needs null check + null check on TryGetNeed() return value
Comp_Guard.cs Wrapped TryGainMemory with cp.needs?.mood?.thoughts?.memories != null
ThinkNode_ConditionalShouldGuardSpot.cs Added pawn.timetable != null before accessing CurrentAssignment
ThinkNode_ConditionalShouldPatrol.cs Same timetable null check
ThinkNode_ConditionalShouldSearchAndKill.cs Same timetable null check

Test plan

  • Build the project with no compile errors
  • Load RimWorld with GFM + OWD mods, verify no NullReferenceException in logs
  • Verify guards still stop duty when their needs are low (regression)

🤖 Generated with Claude Code

Pawns from mods like "One With Death" can have null needs tracker
or null timetable, causing crashes in guard logic. Added null checks
for pawn.needs and pawn.timetable across all affected code paths.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant