"Accessed none trying to read property Item" Inventory system (Blueprint/C++)

I’ve been trying to get this inventory system working for quite a few days now and I’m honestly just stumped. It says it can’t access the item variable and it’s not able show the thumbnail I have for that item. I don’t know how I would go about fixing this though because I feel like I have everything working correctly but apparently not.




image

One thing I notice is it does seem like its trying to change the icons to the items I have which is why its going from the blue icons i set up to the white default icons.

As the error states, the issue is that the Item property in the BP_InventoryIcon instance is invalid. Since you have no valid item set, you cannot get the texture from it and I assume that makes the Set Brush from Texture function use a default white texture.

So look into how you are setting the Item property in the BP_InventoryIcon class. Maybe you are forgetting to set it or retrieve the relevant item. Also, I am no UI expert but I am under the impression that the Construct event is called when the widget is created. So if you create an instance and then pass it the Item reference, the event was already called with the default None/nullptr value. So I would try to create a custom function that takes the item reference that calls the Set Brush from Texture function, that might fix it.

Hey I appreciate the reply, sorry for the late response but when you say “Maybe you are forgetting to set or retrieve the relevant item” do you mean setting it like this on BP?
image

If so when I try to select Asset it shows nothing, even though I have 2 items as children under this variable. (Btw I followed a tutorial on how to make this so I’m not even 100% sure what I’m saying makes any sense)

Also on your second point, you said I should try making a custom reference that calls the Set Brush from Texture function. I’m not 100% sure what that means, could you give me an example on how I would do that?

Hey I appreciate the reply, sorry for the late response but when you say “Maybe you are forgetting to set or retrieve the relevant item” do you mean setting it like this on BP?

Yes, sort of. The approach depends on how you are structuring your items/inventory. I am going to make some assumptions in the following text. I guess that you have an inventory widget that then contains those four BP_InventoryIcon instances. When this master widget initializes, it somehow retrieves the UInventoryComponent which contains a collection of UItem instances. The widget iterates over those and assigns them to the individual inventory slot widgets. What you do there is up to you, you can cache the UItem reference using the set node, or just get the item’s texture and apply it to the widget.

If so when I try to select Asset it shows nothing, even though I have 2 items as children under this variable. (Btw I followed a tutorial on how to make this so I’m not even 100% sure what I’m saying makes any sense)

That’s because your UItems sort of only exist when the game is running. I guess you have two child blueprints in which you assign the mesh and texture. Those are classes that represent a type of item, you need to create instances of the classes for the actual items to exist inside the game. When you try to reference an UItem in the blueprint like this, the editor cannot find any because only item classes exist at editor time. So you need to get the item from the inventory and pass it into the set node. If you are following a tutorial, you should go through it again and make sure you have done all the steps correctly.

Also on your second point, you said I should try making a custom reference that calls the Set Brush from Texture function. I’m not 100% sure what that means, could you give me an example on how I would do that?

To follow on the example from the first paragraph, you get to a point where you have obtained an UItem instance from the inventory component and have a BP_InventoryIcon in which you want to display it. What I meant is that you should add an event or fuction to the BP_InventoryIcon class that has an UItem parameter. This function would then call the Set Brush from Texture node, where you would take the item parameter and use it to obtain the Thumbnail texture.

To put it all into one place (still using some assumptions):

  1. The widget displaying the inventory gets initialized
  2. This widget somehow obtains the UInventoryComponent and uses it to get a collection of UItems that are currently contained within the inventory.
  3. The widget iterates over this collection and for each item (creates a child BP_InventoryIcon widget) and calls the custom event/function on it, passing the UItem into it.
  4. The event/function takes the UItem parameter and uses it to obtain the Thumbnail texture which it then uses in the Set Brush from Texture node

Hope that this helps.

Alright so I now understand that the items don’t exist until I actually start running the game, but I’m still left with more questions after what you just said. (I’m more used to C++ and this is my first tutorial trying to use Blueprints so you might have to bare with me lol.)

When you say stuff like

You can cache the UItem reference using the set node, or just get the item’s texture and apply it to the widget.

(I’m not sure what it means to cache the UItem reference)

or

you should add an event or function to the BP_InventoryIcon class that has an UItem parameter.

I don’t really understand how I would get that to work in Blueprint. I’m more of a visual learner so if you could maybe give me a screenshot example of how that would look it would be a lot easier to understand.

So you need to get the item from the inventory and pass it into the set node.

Alright so I passed the item through a set node and I got an error that I don’t really understand.

I also forgot to put a screenshot of the actual Inventory logic, maybe that might be useful to see if anything looks off that could interfere with the Inventory Icon code.

Also the assumptions you made about the Inventory having 4 icon instances and the widget iterating over those and assigning them to individual inventory slot widgets is true along with the UInventoryComponent containing a collection of UItem instances, just to clarify.

I also did look over the tutorial probably about 8 times, I have kind of a hard time believing I might’ve missed something. Part of me thinks the tutorial is just outdated (the tutorial is 4 years old https://www.youtube.com/watch?v=-WNwo-riV1Y&t=1121s) but I would really like to get this inventory working because it’s exactly what I want in terms of a simple inventory and I’m already this far into it.

I will be picking this up on my personal account since I won’t have access to the other one anymore in a couple of days.

When you say stuff like “You can cache the UItem reference using the set node, or just get the item’s texture and apply it to the widget.” (I’m not sure what it means to cache the UItem reference) or “you should add an event or function to the BP_InventoryIcon class that has an UItem parameter.” I don’t really understand how I would get that to work in Blueprint.

Caching just means storing the UItem reference so that you can access it directly later on. In C++, you would create a UPROPERTY() with the type UItem*. In blueprints, you just add a variable in the sidebar with the type Item. Functions and events in blueprints are just like functions in C++. There are some subtle differences, but those should not matter in this scenario. You add a function similarly to variables in the side bar under the functions section. As for events, you right click inside the event graph and type in Add Custom Event, select the option and then name the created node.

Alright so I passed the item through a set node and I got an error that I don’t really understand.

The issue here is that the Set node is not pure, so it needs to be called using the execution flow (white arrows). In this situation, it is not called so it’s as if it didn’t exist. The result is that the item reference would be still invalid and you would still have the original issue. Basically, if you have a node with the white arrows at the top, you need to connect them for the node to work.

I’m more of a visual learner so if you could maybe give me a screenshot example of how that would look it would be a lot easier to understand.

Here is a quickly stitched together representation of what I am trying to say. You add the function/event to the BP_InventoryIcon that takes in the UItem reference. Then in the inventory widget you do what you are already doing but after you add the child widget, you call the function/event created in the previous step on it, passing it the item to display.

Doing that should work. The only other thing that might cause an issue is relying on the On Inventory Updated delegate for the widget to initialize. If this delegate is not invoked after the inventory widget is created, it will appear empty until the inventory changes. Hard to say whether it is desirable, but you might want to call Refresh Inventory after the bind node, sort of like I did in my example.

As for the tutorial, they show assigning the Item into the widget at around 23:00. The only difference is that they are using the Expose On Spawn approach (they set up the widget for that at around 18:20), where they set the UItem reference when creating the widget. This way they cache it before the event Construct is called in the child widget, hence why they don’t need to create a custom function/event. I have no idea whether this is the recommended approach in UMG, as I have said, I have minimal experience when it comes to that. I can also see that they indeed call the Initialize Inventory event after binding the delegate like I mentioned before, so probably do that as well.

1 Like

(post deleted by author)

(post deleted by author)

Yo it works, thank you for the help. Seriously