Some advice needed

I’ve been working on my first game, a survival game, and it’s been going pretty well but my item management(code wise) is pretty bad in my opinion. Each item is an actor, and I have an inventory array that I add each item there for the player. I also have chests, a hotbar array and an equipment array for armors. It’s a bit hard to track which item is where, my code is way too complicated, I’d like some advice from some more experienced developers. For example, I have an event to move an item between a chest and the player inventory.

Take a look at this:

I am not talking about the lines but the logic behind it, like I have to check 3 arrays, check if its found and removed, then add it to another array, update, change ownership blah blah, and the more things I add, the more complicated will get. Can you help me find a better way of handling items? I am thinking of making a variable in each item with the actor that has the item, and if it’s a character, save if it’s in the hotbar, inventory or equipment slot to create an icon in the right spot.

That looks really complicated. Your PlayerController seems to be doing too much in my opinion (even the things it shouldn’t be responsible for). You probably need to think of it in a more object-oriented way. Each actor should be responsible for its own inventory and should have its own functions (methods) like Add Item and Remove Item then you can simply call those in your PlayerController and abstract away the complexity.

I’m not really that experienced when it comes to blueprints and unreal though, but I have a lot of experience in programming.

Perhaps, you can create a ‘inventory item’ class to store item type, active state, ownership, meshes, icons, etc. Afterwards, use a single Array to hold the inventory items.

Hmm probably you are right. I mostly use Player Controller so I can easily check who is the player that sends the request. I will probably make an input so each player can send his own player controller.

Each item is an actor, I save such things in the actor settings. My main issue is that i need different array for armor,hotbar and inventory(inventory isn’t slot based unlike the hotbar/equipment).
I am still not sure how to proceed. I’ll wait to see if someone else has any other idea about this.

Perhaps the item can store references to the UIs (armor, hotbar, inventory, other) it should be displayed in? To be honest, I personally don’t see a problem with using 3 different arrays for the different UIs. If they all use the same functions, you can create functions that you input the arrays for manipulation.

It’s not exactly a “problem” it’s more of a “complicated” issue. Also what I am doing at the moment is attach each item to the player so when someone walks near a player, all his equipment is replicated, mostly for his equipped item and armor, which kinda sucks. I might just make all items replicated at all times, but I am not sure how much network traffic it’s gonna use for all the items.

I would recommend only replicating what you need too, when you need too. For a better sense to your design, can you elaborate on the need to replicate all items when near the player? A video or illustrations would help.

Well I have a base item blueprint, with a scene and a static mesh as a child (not sure if scene is actually needed but oh well…). For each item I create a child, for example “Hatchet_BP”, and I set the mesh accordingly. Then if the user has a hatchet, he can equip it and it attaches to the character. However if it’s not replicated, the rest of the players can’t see it. I guess an alternative would be to make a mesh in the character and set it to the equipped item, and rotate it to it looks correct. Do you think that’s a better approach? Also if the items are in a chest, I need to attach the items to the chest so if they open it, they can see what items are inside.

I had to review the UE4 Replication concepts. In my opinion, some tweaks to replication properties to determine when the actors are replicated will provide optimum performance.

Well at first I thought of making each actor relevant to it’s owner only. Later on I realized items in chests won’t work, and I can’t change the replication settings during runtime, so I need to use the distance to determine if actors should be replicated. That’s the reason I attach the items to the chests/players, if they get close to the chest, they should get the item actors in case they open the chest, but that gets complicated there, but I guess there is no better way. My main “concern” right now is I want to find a better way of saving where the item is at the moment, so if I want to move an item from a player to a chest, change 1-2 variables instead of searching 3 player arrays and then adding it to another array… There must be something better than what I am doing

sorry for bumping this, I could still use some help. Right now what I am trying to do is this: When I move an item, check if I need to swap the 2 items, swap arrays, if an item is in player’s hand, remove it, hide it, if it’s armor check if the other item is armor too(for swapping), but it keeps getting messy. How would you do this? Right now I have like 100 checks and it’s extremely complicated. If someone could help me with this would be appreciated.

Why dont you create internal blueprint functions for this? You have your “removeitemfromsource”, your “additemtoplayerinventory” and in this you have your “checkifinventoryfull” and “checkifplayerholdsiteminhand” etc. etc.

You are correct that this logic is complicated, but thats how the cookie usually crumbles.

Thanks, I think my problem is with swapping items and doing a bunch of checks all together. For example the hot bar has 9 slots, if I have a hatchet in slot 1 and I drag my player helmet there, since the hatchet isn’t armor it shouldn’t swap, but add the hatchet to the inventory, and this is just an example of checks I have to do, but I keep complicating things I am afraid.

You can do the checks too in internal functions. Like “isSameType” or even “swapifSameType”.

As written - you are not wrong. It simply is complicated :wink: