Skip to content

Conversation

@LordLundin
Copy link

Fix nil reference crashes in scanner and equipment systems

Summary

This PR fixes two critical crashes that occur when loading saves created with older versions of FactorioAccess or when player state is not fully initialized.

Issues Fixed

1. Scanner crash on refresh (line 116)

  • Error: attempt to index field 'scanner_cursor' (a nil value)
  • Cause: Loading saves where player_state[pindex] was never initialized
  • Fix: Added nil check before accessing scanner_cursor.category

2. Equipment/guns menu crash (line 500)

  • Error: attempt to index field 'guns_menu' (a nil value) when pressing R
  • Cause: storage.players[pindex].guns_menu not initialized for existing saves
  • Fix: Added defensive initialization in all 8 functions that access guns_menu

Changes Made

scripts/scanner/entrypoint.lua

  • Added nil check for player_state[pindex] in do_refresh_after_sfx()
  • Safely initializes player state if missing

scripts/equipment.lua

  • Added guns_menu initialization checks to:
    • guns_menu_open()
    • guns_menu_left()
    • guns_menu_right()
    • guns_menu_up_or_down()
    • guns_menu_get_selected_slot()
    • guns_menu_read_slot()
    • guns_menu_click_slot()

control.lua

  • Added nil check in coordinate announcement handler for guns menu

Testing

✅ Scanner no longer crashes when pressing PAGE UP/DOWN with old saves
✅ Guns menu (R key) now opens successfully without crashes
✅ All gun navigation keys (WASD in guns menu) work without errors
✅ Tested with both fresh saves and saves from before these features existed

Technical Details

Both issues stem from the same root cause: when loading saves created before certain features were added, the corresponding storage structures don't exist. The mod previously assumed these would always be present, leading to nil reference errors.

This PR adds defensive programming practices by checking for nil values and initializing missing structures with sensible defaults when needed.

Backwards Compatibility

These changes maintain full backwards compatibility. Old saves will have the missing structures created on-demand when the relevant features are first accessed.

Andreas added 2 commits July 7, 2025 19:16
When loading saves created before the scanner module was added,
player_state[pindex] could be nil, causing a crash when accessing
scanner_cursor.category. Added defensive initialization check.
Added nil checks for storage.players[pindex].guns_menu across all
equipment functions. This prevents crashes when pressing R or using
other gun-related keybindings with saves that predate the guns menu
feature or have incomplete initialization.
@EphDoering EphDoering changed the base branch from main to f2.0-dev July 7, 2025 19:07
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