Replicate Storing Inventory in Other Actors

I have been following this tutorial on how to create an inventory system.

I’ve gotten to the point I want to store items in a separate actor (video #13). I’m trying to figure out how to modify his process so it replicates.

Currently I run transferring items on a server RPC. That works and the host is able to deposit and retrieve items. The host is also able to retrieve items a client deposits, but I am having a really hard time getting the client to be able to see/retrieve items. I would think that after the the items are added to the other actor on the server I should have a rep notify, but I can’t figure out where/which variable to put it on.

Ok, replication is one way: server->clients.

There are several things you have to do:

  1. Make sure both the client and the server have the corresponding inventory objects or components.
  2. When you add an item to the inventory make sure an RPC is called and the exact same item is added in the corresponding server inventory too. This will handle your client->server communication.
  3. Make sure the inventory object component is marked as replicated and all values that you’ll use are also replicated. This will handle your server->all clients communications.

Now you should be able to see your inventory on all clients.

P.S. I’m sorry but I don’t have the time to watch through the videos so I’m not really sure what your setup is.

All the storing/removing etc has to happen on the server. Clients do not directly modify inventory.

Client wants to add/remove something : RPC the server → Server updates inventory

I’ll add some screenshots tonight that should help clarify.

Agreed, which is what I did to get the inventory transferred to the storage actor. But now when I go to retrieve items from the storage nothing happens for the client (works great for the host). My print string on the host shows that the storage actor has a populated inventory and the client shows an empty inventory. So when the client attempts to retrieve the items it says there’s nothing there to retrieve. I’ll add screenshots tonight. It seems like an ownership issue, or like I have a variable/component that should be set to replicate that isn’t.

To start off, the player presses a button in their UI to drop a quantity of some item.

Which triggers this interface event within the UI that calls the function “Remove Item by Amount” in the controller’s inventory blueprint component.

Within the blueprint component the selected amount of items are removed from the player’s inventory, a “pickup pack” actor is spawned (if not already), and the same quantity of items that were removed from the player’s inventory are added to the pickup pack by the server.

On Clicked (B_DropQuantity) needs to call an event on the SERVER. So RPC (Item name, Amount, unique id)

Server executes the Drop item by amount.
Server also has to be the proxy that spawns the pickup pack.
Pickup Pack actor needs to be set to Replicate.
Server then Adds items to the pack.
BPC Inventory needs to set to Replicates.

I think I’ve done that now. However, the client still isn’t retrieving any resources from the pack. I had thought I needed to make sure that the resources existed in the pack for the client but maybe they don’t, and perhaps it should all be run on the server’s version of the pack anyways. So I changed my code for the pickup to try and get the server to add the items to the client’s inventory, but that didn’t work either.

This is inside the pickup pack’s BP.

Convert all those RPC server events back to normal non-replicated events.

Overlap → switch has authority (Authority) means the overlap will only work on the server. Which is what you want.

Add items to player won’t fire because it requires the client to call it. You’re already on the server.

CL Add Item is the same. Non-replicated!

for loop → completed destroy actor doesn’t require the branch. Replicated actors only need to be destroyed on the server.

I’ve got it now! I had to set the overlapping controller to be the owner, then switch has authority. It still wasn’t adding it to the client’s inventory though so I added the run on client RPC back in and it works!