Hi, for some reason items aren’t being added to the inventory, even though the items always follow their correct path and hit their correct return node as successful. I’ve tried using Get Reference and Get Copy but it seems to make no difference, I’ve also made the Inventory array inputs on the functions pass by as reference and that didn’t make a difference either. I have no idea why this is happening, because I have it on a different project which works fine, but is slightly different (I’m trying to remake it in this one), though nothing has changed in these areas and they are receiving their correct inputs, so I don’t see what’s going wrong.
It’s been a day and I still haven’t been able to see where I’ve gone wrong, can anyone help please?
Come on, it’s been 5 days I’ve been stuck on this now, and I’ve not gotten anywhere, it’s forced my project to a standstill and I have no idea what’s going wrong, I’ve lost count of how many different tests I’ve made printing the strings and using break points, returning copies and references, but to no avail, everything is executing as it should, returning the correct variables, locations, items, etc…, just nothing is being added to the array, where am I going wrong!? Is it because this project is 4.26 rather than 4.25 where the original project is? It shouldn’t make a difference though, because this is a new project, not one being updated to 4.26
I dont see any thing related, put the whole code here.
Also try debug it yourself trying add breakpoints and check all things step by step.
I’ve tried debugging it myself for 6 days now, I’ve added break points and print strings at every step, and everything is executing correctly when it should be, and giving/receiving the correct variables that it should when needed, so there’s nothing wrong with the rest of the code, the problem is that nothing is being added to the array, which isn’t making any sense because it knows what inventory and sub-inventory it’s adding to, and what item and amount should be added
I even created a quick debug code to force giving myself an item using this code here, but that didn’t work either (I’ll only show this code because I have about 10 functions and some are quite large, but all work as they should, it’s literally just the adding to the array that isn’t working)
This is the Find Sub-Inventory function used, which again, works exactly as it should:
(The ‘Find Item Info from Slot’ macro is literally just a Get Data Table Row that uses the name of the item to find the row, and returns the row with all of the items info)
The only thing I can actually think of is that I’m trying to edit an array that is part of an array of structs, and for some reason it’s not letting me edit that (even though in the old project it did), but I have no idea how I would get around that, or why it’s not letting me in the first place…
It’s set up so the Inventory variable is an array of Sub-Inventory structs, which each sub-inventory contains its name, max slots, etc…, and an array of slots, and it’s the array of slots that I’m trying to add to/edit, nothing else in the code is relevant because it prints the correct information when it tries to add the item (the item itself, amount, location, and the inventory it’s adding to), but doesn’t add a slot, and prints 0 when I print the new item’s index.
Here’s the values the Add is receiving when using a break point on the Add node:
why you add +1 to the array last index? it’s an array and last index +1 is not valid i guess.
what’s the logic in add a value to an array length instead of taking(substract) 1 from it ?
Why isn’t row not found connected to anything, also what are your found and sub inventory index initial values, you should reset them before going to the loop.
Also, you have nothing going from success and there is a missing exe line to add unique.
That’s the index for the new slot being created, so I get the index of the last current slot, then +1 and add that to the Inventory Coordinate struct (Sub-Inventory Index, and Item/Slot Index (both Integers)), it’s basically calculating the new array element’s index before it’s created.
I forgot to add a return node, but it is returning as found because everything afterwards is executing.
They’re set to false and 0 respectively, and they are local variables so they reset every time the function is called.
Yeah I quickly made that code to force add a Medkit to my inventory, in the actual functions which sorts the items and adds them to specific slots or adds new slots or finding slots all use the success and return nodes everywhere, I just forgot on that one, but again it’s working as it should, and the FindSub-Inventory function is definitely returning successful as seen in the last image because it’s returning 2 as the sub-inventory index, which is the Consumables sub-inventory, and the AddUnique isn’t being used, that was there from when I was trying to see if that would make it work, but it didn’t.
You get an array item from Local Inventory array. You then get another item from the sub-inventory array. You update the sub-inventory array, but that array/sub-item doesn’t seem like it’s put back in to the top-level Local Inventory.
In Unreal, array items are copied, and structs are value objects, not reference objects. The sub-item that you modify is a copy, not the one that lives inside the Local Inventory.
When you use get (in all cases where you are adding something(system or subsystem) or just setting an array element), make sure its a reference and not a copy.
When you are adding try it with make, not some local variable struct.
I understand all of that, but I AM using references, every instance of the ‘Get’ node used in the necessary functions are Get (Ref), not Get (Copy), as you can see with the diamond pin on each ‘Get’ node, and in the ‘Find Sub-Inventory’ function the return node output returns a reference too. Also the Inventory input for each function of mine has ‘Pass-by-Reference’ set to true in the function’s details panel. I need to use references because I have external inventories I can trade with, such as shops, chests, other players, etc… so I can’t just plug in the player’s Inventory variable straight into each node requiring an Inventory, I need to pass references between functions.
What I don’t understand is why it’s not working here, because these parts of the functions are set up exactly the same in my previous inventory project, as in adding to the array inside of another array of structs, setting the array element, etc… and they work flawlessly, yet they receive the exact same information as they are here, using references, and executing at their correct times, following the correct execution paths, etc…
Did you set a breakpoint (select node, press F9, then run) and single-step through each node, looking at the contents of the objects of interest?
If so, where which exact node does not behave the way you expect it to?
Sorry, I took a break from the project for a bit, I came back with fresh eyes and set a breakpoint at every single step of the code, and went through it a few times and I still see absolutely no issue anywhere
I was using breakpoints before, but this time I literally did it for every node and watched what values each one received and returned, and literally everything was working as it should, but still nothing gets added to the inventory
It’s behaving as if I’m editing a copy of the inventory, but every ‘Get’ node is returning a reference, and the input variables on all of the functions have ‘Pass By Reference’ checked on the Inventory input, so I have no clue what’s wrong with it, but yeah, I’ve checked a breakpoint at every single point in the code that’s running, and used print strings too, and it shows nothing out of place
Well, maybe it isn’t. or maybe you’re editing the server object but displaying the client object, or something along those lines?
Or the thing looking at the inventory isn’t looking at the inventory you think it’s looking at – e g, your code may edit the inventory just fine, but the code USING the inventory may be looking at the wrong thing.
Well they should be, they all have the diamond pin instead of a circle, and when I right click them they give me the option to convert it to return a copy. And I have no idea how to check if it’s editing a server object, but that shouldn’t be the case, I’m testing it in single player, and also it’s in a component attached to the player character, that is only communicating with itself and a lootable object in the world (which all works fine, has the correct values, destroys itself because it thinks the item was added successfully, etc…)
And it is definitely looking at the correct inventory, I manually added items to the default value of the Inventory variable, so when I mouse over or print the Inventory variable that is being used, it shows the manually added items too, which it does, so that’s not the issue.
The reason it’s not working is because you are trying to add a value to an array in a structure in an array. (from what I can see) When your doing this, always assume your getting a copy and never editing the original. What you need to do is save the array of the sub inventory slot as a local variable, add the new entry and then use that to rebuild the sub inventory array to set the array element in your main inventory array.
I hope this make sense.
I’ve figured it out, because I tried to plan ahead for trading with other characters or items with inventories (shops, chests, etc…) I wanted to take a reference of the inventory variable, but for whatever reason it wasn’t using it as a reference, even though it worked in an older version, but I had to rework all the functions to take a reference of a newly created component, that contains only the inventory variable and can be added to anything to give it an inventory rather than give it everything, and now the functions take a reference of the component and use its Inventory variable directly, rather than passing it through functions, and it finally works! Thanks for your help everyone who did!