Inventory System Save System - One Object Spawns on Play After Being Destroyed

could you show a visual? I’m sorry, otherwise I’m not quite following

exactly what you did here, you saved the createsavegameobject and you cast/saved the load object

Alright! The error is gone, and the saves are showing up in my ‘saved’ folder but it’s still not loading in the changes to the game when reopened. Here are the updated blueprints. I had to add the ‘is valid’ because it claimed the inventory system wasn’t valid.

| Game Mode | Load Game | Load All Data | Save All Data |

if your inventory is Invalid it means you havent set the variable so it wont work.

other than that regarding it not loading you have to debug it with printstrings/breakpoints and see where it fails

How do you go about setting the inventory component? I could be completely missing something but I don’t recall doing that before.

It looks like my casting to the save data fails, and that’s why the rest doesn’t work. The load game from slot right before it is valid, so I’m not sure what the issue is.

where is it? probably something like GetPlayerController->GetComponentByClass for example

itll only fail if its the wrong class, so make sure you’re using the right savegame object, or if its just invalid

1 Like

I’ve done this, but the error hasn’t resolved. Here’s the code.

Blueprint Runtime Error: "Accessed None trying to read property Inventory System". Node:  Set InventoryContent Graph:  Save All Data Function:  Save All Data Blueprint:  BP_GameInstance
Blueprint Runtime Error: "Accessed None trying to read property Inventory System". Node:  Set ObjectData Graph:  Save All Data Function:  Save All Data Blueprint:  BP_GameInstance

I’m having issues with the clear actor data, assumedly because actor data doesn’t have anything in it to clear, but I’m not sure how to check that so the error is eliminated.

It looks like the 'load game from slot’s class is null. That’s probably why the cast is failing here. The savegame does exist and it should be of the right class, I cast and set everything.

Game Instance Event Init happens only once and before the controller exists.

to ensure order of operations its better the have the PlayerController BeginPlay set the variable on the gameinstance

that shouldnt be an issue.

just likely means your slot name is wrong, for player data just Hardcode it for now, ie use Player

The cast is successful now, but I’m still having issues with the inventory system being invalid.

I’ve done so here

It looks like the level data is saving and loading on the first time loading, but the second time… all items are gone??

how you order things really depends on your game

given your setup requires the playercontroller to be loaded for its inventory to be valid its better to call loadlevel off your playercontroller beginplay instead of the gamemode.

or add a bigger delay to the gamemode for testing. this is kinda hacky but we’re just trying to get it to work so you can understand the logic

So it looks like the inventory component was removed from the controller so that’s why it came back null. I recall in the inventory system tutorial, every time there was a reference drawn to another blueprint’s inventory system component, he would draw it from the player character. I’m getting an error in the Item Data Component Blueprint

, I can’t pick items up either- and I think it may have to do with the fact that the player character’s component isn’t being set, but the controller’s is. So the code is split between referencing one or the other. I’m not sure. Here’s the error. I’ll change all of the references to the controller (?) and see if that helps.

Blueprint Runtime Error: "Accessed None trying to read property K2Node_Event_PlayerCharacter". Node:  Branch Graph:  IsValid Function:  Execute Ubergraph C Item Data Component Blueprint:  C_ItemDataComponent

The error goes away if I use the controller’s component, and I can pick up items now, but this part is still happening

when I open the inventory, and I’m able to move around while the inventory is open which isn’t meant to happen

And nothing shoes up in the inventory either.

I realize I should show exactly what I mean by this-

its your game, your inventory component is where ever you put it ha, just don’t forget where it is :slight_smile:

if its single player it doesnt really matter where you put but this is where encapsulation is useful, once you’ve decided where it is, create a function library to get it and use that everywhere, that way if you ever move/change it you only have to change one function

I’ve made sure to go ahead and get rid of the inventory component everywhere it’s not mean to be and replaced those references with the player controller’s inventory component. I’m not sure if that’s correct, though, because I believe the tutorial had the component in multiple places. I’m not sure in that first sentence if you mean that there has to be only one, but I took it as such. I don’t think it helped much though.

I’m did some research on what a common function library is, and I understand- I suppose I don’t understand what you are suggesting I turn into a function in a library, though. Which function? My apologies if it’s clear. I’m going to make one for casting to and getting the inventory system from the player controller.

Right now I suppose I’m concerned about why the inventory has become blank. I hadn’t changed anything in the blueprints in the inventory system, so I assume it must be the fault of a blueprint I previously sent, maybe the Item Data Component, recently I haven’t changed much else. Hm.

it just depends on your game, if its the player inventory it can go anywhere in a single player game, recommended could be the playerpawn or playerstate.

if objects also have inventories then they can exist on them too, ie crates.

so an example of where to use it is common getter functions, so if GetPlayerInventory is something along the lines of GetPlayerPawn(0)->GetComponentByClass etc you can put that in a function library and reuse it. this allows you to handle the IsValid checks with error checking too

you mean the inventory component is empty? as in lost all its functions etc? possibly you made a duplicate by mistake?

So I decided to make getting the inventory component from the player controller into a shared function and replaced it in many places. This has caused most of the save system to work. The only thing that isn’t saving is the chest inventory.
I’m not having any errors anymore, so it seems as if creating the shared library function and using that to create and set the inventory system has worked. I’m not completely sure, but I think that it may not be saving or loading at all. Because it’s located in an actor and not the player controller, game instance, etc. I’m not sure how to access the data inside. Here’s my chest inventory, save, and load BPs. Again, everything else is working though. Very close to figuring this out, I think.

for chests you just need a reference to it, likely you’d get this on some OnInteract event

So, when the character interacts with the chest, I set the data variable, and it’d be saved when the save button is pressed, then loaded when the chest inventory opens. Made sure it only happens the first time the player opens the chest after loading, so that it doesn’t override the data in the chest when the player adds something, leaves, and comes back while still playing in the same session (didn’t close down the game). Unfortunately, this doesn’t seem to be working. I feel like it has something to do with the order things are performed but I’m not sure. It looks as if the chest inventory data from SaveDataPlayer doesn’t have anything inside of it when it should. If I’m not mistaken, it should save when the SaveDataPlayer is saved to slot, which happens when I hit save (the same place the player inventory loads).

| Chest UI | Game Mode | Save All Data |

Save All Data doesn’t set the chest inventory’s object data, but it saves it. The loading takes place when the inventory opens in the Chest UI BP.

Dont use preconstuct if references are involved.
construct is spawn, preconstruct is like editor preview which means those references dont exist.

what is the chest exactly? will there ever only be one or are there many? etc