Skip to content

Guard SpawnItem against null CurrentMap and preserve inventory spawn behavior#76

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-spawn-item-command-null
Draft

Guard SpawnItem against null CurrentMap and preserve inventory spawn behavior#76
Copilot wants to merge 3 commits intomainfrom
copilot/fix-spawn-item-command-null

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 7, 2026

SpawnItem could throw a null reference when called before a map is initialized because it unconditionally read CurrentMap.Id. This change makes map assignment conditional on map-based spawning and safely no-ops when no current map exists.

  • GameWorld spawn guard

    • Updated GameWorld.SpawnItem to only resolve CurrentMap.Id when:
      • no MapId is provided, and
      • item is not being spawned into an inventory.
    • If map-based spawn is requested with no initialized map, the method returns without spawning instead of throwing.
  • Regression coverage

    • Added tests in SpawnGameObjectsTests for:
      • map-based spawn before map initialization (should not throw / should not spawn),
      • player-inventory spawn before map initialization (should still spawn into inventory).
  • Behavioral intent

    • Map spawns require an initialized map context.
    • Inventory spawns remain independent of map lifecycle.
public void SpawnItem(SpawnItemParams spawnItemParams)
{
    if (spawnItemParams.MapId == Guid.Empty && spawnItemParams.Inventory == null && !spawnItemParams.IntoPlayerInventory)
    {
        if (CurrentMap == null)
            return;

        spawnItemParams.MapId = CurrentMap.Id;
    }

    Spawner.SpawnItem(spawnItemParams);
}

Copilot AI and others added 2 commits April 7, 2026 22:35
Copilot AI changed the title [WIP] Fix SpawnItemCommand failing due to CurrentMap being null Guard SpawnItem against null CurrentMap and preserve inventory spawn behavior Apr 7, 2026
Copilot AI requested a review from DavidFidge April 7, 2026 22:39
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.

SpawnItemCommand is failing due to CurrentMap being null

2 participants