this problem keeps me from doing some progress for about a week now, it makes me so mad.
Essentially what is happening:
ListenServer: I pick up an Item, add it to the Inventory. The player sees the Item instantly in his Inventory Widget.
Client: I pick up an Item. Item gets added to the Inventory. The player does NOT see the Item in his Inventory Widget. Client picks up another Item. I now see one Item in the Client’s Inventory Widget. Pick up more Items, and Client’s UI is always one Item behind of his actual Inventory.
In both cases, the Inventory variable is correctly replicated. I test this by outputting the number of Inventory Items on Client and Server, it’s the same.
First I thought that I update my HUD on the Client too early, that is before replication has taken place. Moving the HUD-Update to an OnRep_Function didn’t fix the problem though.
All HUD-Updates are Clientside Functions. Adding to the Inventory is done on the Server. I also checked all my pointers.
The Inventory and InventoryManager are UActorComponents in the PlayerController. Everything is set to replicate via SetIsReplicated(true); after creation. Every important Variable in these UActorComponents is set to replicate.
After further investigation, it seems that when I want to create the InventorySlot for the Item that I just picked up, the size of the Inventory is not updated.
Now here’s a list of what I do when picking up an Item:
1. Server: Call InterfaceFunction “Use” of that Item from the PlayerController (takes a Controller Reference (self) as parameter)
- 2. Server: Cast PC Reference to my custom PC, get his InventoryManagerComponent->AddItemToInventory(ItemData, PC->InventoryComponent);
- 3. Server: Add Item to Inventory. Call OnRep_Function() which calls the two following Functions:
- 4. Client: Clear all Inventory Slots in InventoryUI
- 5. Client: Get PlayerInventory in InventoryManagerComponent (which now holds updated replicated Value), and add an InventorySlot for every Item in there.
I think the problem lies in step 5.
When I pick up the first Item and output the Number of InventoryItems in Step 5, it says the number is 0. What’s happening here?
Even if I don’t do it via an OnRep_Function(), it still doesn’t work. It’s so weird.
I feel like I’m missing something basic. As I said, every Function is set to Client or Server respectively and no pointers are NULL.
Has anyone here encountered a similar problem? I really feel like the solution “should” be easy since it’s such a weird problem and I couldn’t find much on the net about it.
Please save me from this suffering, and thanks for your time!
PS: If you guys want to see some code directly, I can post it! Essentially, I bought an Action RPG Inventory (can only recommend!) and I’m trying to translate it to C++. I didn’t do much yet, just adding to Inventory and showing the Item there.
I also posted something similar in the ARPG Inventory Thread. There’s at least some code and Screenshots there if you need more info. I can always post some more code though!
Something I noticed:
When I update HUD manually (on button press) after adding an Item to inventory, it works. Some weird delay/connection stuff must be going on?! Updating HUD via button press is not viable for me. I’m really confused now.