title: UE4 C++ saving inventory TArray of item pointers across multiple levels
body: <p>I have a basic inventory system in UE4 using a TArray of pointers to my custom Item class. It works fine in individual levels, but when I open a new level, the inventory disappears. I've looked at multiple tutorials and posts about this issue and tried various solutions including migrating my inventory array to the Game Instance, and creating a SaveGame class that holds a copy of the array that saves before and loads after opening a level</p>
  <p>After all these, inventory still disappears. My code has changed a lot so it's probably not that helpful but here are some snippets of my current solution.</p>
  <p>Declaration in character header</p>
  <pre><code>UPROPERTY(EditAnywhere, BlueprintReadWrite)
      TArray&lt;AItem*&gt; Inventory_Space;
  </code></pre>
  <p>Declaration in SaveGame</p>
  <pre><code>UPROPERTY(SaveGame)
      TArray&lt;AItem*&gt; Inventory_Save;
  </code></pre>
  <p>Save and load functions in character implementation</p>
  <pre><code>void ABatteryManPlayer::SaveInventory()
  {
      
      UBatteryMan_SaveGame* SaveInstance = Cast&lt;UBatteryMan_SaveGame&gt;
          (UGameplayStatics::CreateSaveGameObject(UBatteryMan_SaveGame::StaticClass()));
  
      for (int i = 0; i &lt; INVENTORY_SIZE; i++) {
          SaveInstance-&gt;Inventory_Save[i] = Inventory_Space[i];
      }
      
      UGameplayStatics::SaveGameToSlot(SaveInstance, TEXT(&quot;Slot0&quot;), 0);
  }
  
  void ABatteryManPlayer::LoadInventory()
  {
      
      UBatteryMan_SaveGame* SaveInstance = Cast&lt;UBatteryMan_SaveGame&gt;
          (UGameplayStatics::CreateSaveGameObject(UBatteryMan_SaveGame::StaticClass()));
  
      UBatteryMan_SaveGame* SaveInstance = Cast&lt;UBatteryMan_SaveGame&gt;
          (UGameplayStatics::LoadGameFromSlot(&quot;Slot0&quot;,0));
  
      for (int i = 0; i &lt; INVENTORY_SIZE; i++) {
          Inventory_Space[i] = SaveInstance-&gt;Inventory_Save[i];
      }
  }
  </code></pre>
  <p>Saving after game timer goes to 0 (character implementation)</p>
  <pre><code>CurrentTime--;
      if (CurrentTime == 0) {
  
          SaveInventory();
          Instance-&gt;Levels_Complete++;
  
          if (Instance-&gt;Levels_Complete &lt; Instance-&gt;NUM_LEVELS) {
  
              FName Level_Name = FName(TEXT(&quot;Level_&quot; + FString::FromInt(++Instance-&gt;Levels_Complete)));
              UGameplayStatics::OpenLevel(this, Level_Name, false);
  
          }
  </code></pre>
  <p>Loading back into player inventory in GameMode</p>
  <pre><code>void ABatteryMan_GameMode::BeginPlay() {
      Super::BeginPlay();
  
      ABatteryManPlayer* Player = Cast&lt;ABatteryManPlayer&gt;(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
      Player-&gt;LoadInventory();
  
      FTimerHandle UnusedHandle;
      GetWorldTimerManager().SetTimer(
          UnusedHandle, this, &amp;ABatteryMan_GameMode::SpawnPlayerRecharge, FMath::RandRange(2,5), true);
  }
  </code></pre>