Fix nil reference crashes in scanner and equipment systems #332
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)
attempt to index field 'scanner_cursor' (a nil value)player_state[pindex]was never initializedscanner_cursor.category2. Equipment/guns menu crash (line 500)
attempt to index field 'guns_menu' (a nil value)when pressing Rstorage.players[pindex].guns_menunot initialized for existing savesChanges Made
scripts/scanner/entrypoint.luaplayer_state[pindex]indo_refresh_after_sfx()scripts/equipment.luaguns_menuinitialization 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.luaTesting
✅ 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.