Why am I unable to get a reference to an item from an array in blueprint?

This seems like such a simple and rather crucial feature but I can’t figure out how to do it. All I see is Get (a copy). I have an inventory-class as a variable on my playercharacter which contains a fixed-size array of items. I have also made a temporary UI to display my inventory. This is an array of buttons in a widget. Upon picking up a lootable item I want to update the inventoryUI by getting the index of the looted item in my inventory and then changing the equivalent button to show that the item-slot is now occupied. Currently all it does is change the color of the button which I thought would be a simple way to temporarily show whether or not the inventory is full or not.

In this case I obviously need a reference to the button I want to change, and not a copy. Can someone explain to me how I would go about doing this? I know there is a ‘Get (by ref)’ as well which is clearly what I need to use, but UE4 changes it to get (a copy) on its own if I drag in that node.

The person in this thread seems to be having a similar issue, but the comments do not provide a solution: Get (a ref) in Array of UObjects altered to Get (a copy) - Programming & Scripting - Epic Developer Community Forums

You need to give each button in your inventory a integer variable called something like “Slot Index”. Make sure each button has a unique slot index, this is easy to do if you create the buttons in a loop as you can use the loop array index as the slot index.

Then whenever you pickup an item you find the first empty index in your inventory and use that to find the corresponding button that you will need to change.

If you need more info on creating an inventory system basics then epic has two good tutorials on the subject.

Here:

and here:

I appreciate the reply and I’ve actually already watched both of those. My issue isn’t that I can’t get it to work, it’s more the fact that in my opinion “Get by reference” is such a core feature that I don’t understand why I’d need to implement something like that on my own. It is clearly supposed to be on the Array-class already but the editor just doesn’t allow using it.

I fixed it by creating a custom-widget that includes a Button-widget and an ID-variable (similar to what you suggested). It just seems unnecessarily complicated. When I’ve already defined an array that has a unique index for each element it contains I should be able to reference those elements. In code it would be as simple as: “var v = array[0];”

1 Like

I believe even though the array is copied it still references the original button/widget directly so you can still change the button, you just can’t edit the array itself

For everyone still interested years later, I actually found a rather stupid workaround, but as they say, if it looks stupid but it works, it’s not stupid.
If you use a for-each loop, you actually get a reference, not just a copy. This little snippet gets an index, goes through the array till it reaches the index and then uses the reference at that position (In my case I don’t return anything, but just set a reference variable that is used by the rest of the class). The break is just a small performance improvement. It’s not necessary for small(ish) arrays.