Hey all. New-ish to unreal, so apologies if I’m horribly misunderstanding something about replication here. I have an Inventory actor component which works great on my player character or on actors spawned by my player character. However, I’m trying to get it working with purely server owned characters (AI) and I’m struggling to get the updated data when I try to access that inventory with my character. This is on a dedicated server.
For example, when the player interacts with the AI (using an interface), I set the AI’s owner to the player and on the server I generate “loot” items in the AI’s inventory component. This is represented by a TArray, and on the server I can clearly see the array size increase from 0 to 1. However, the OnRep function I have for this array is never called, presumably because the component’s character is a SimulatedProxy and as such has no client of it’s own. When the player opens the AI’s inventory, it is still empty.
I could maybe work around this with an event dispatcher, but I’m wondering if there is a better way to do this. What is the best practice way of getting data generated by the server on server-owned actors replicated back to the clients? Is an event dispatcher my best bet, or is there a cleaner way to do this?
Any advice is greatly appreciated! Thanks!
First off replication works from Server → Clients. Never from Client → Server. For example a client (Autonomous proxy) cannot set a variable locally and expect it to automagically get to the server…or any other client. Regardless if that variable is set to replicate or rep_notify.
So with that, setting a variables value that you want to be replicated must happen on the server. The Variable MUST be set to Replicate or Rep_notify.
You want to add an item to your inventory? RPC the server [Run On Server] to do it for you.
Rep_Notify simply executes its OnRep_Function WHEN its value changes. Which needs to again, happen on the server, which will then replicate out to all other clients.
Actors Placed in the Level OR Spawned by the server are owned by the server. If a server spawned actor is set to Replicate (class settings), it will be replicated to all clients.
Actors Spawned by your autonomous proxy are owned by your autonomous proxy. They only exist on your client.
So, Inventory for Players and AI must be governed by the server. Only the server can add, edit, modify, delete inventory values.
Thus if you create an inventory array, set it to replicate, then have the server add, edit, delete values etc. Then those values will be replicated to all players.
In regards to AI, the server is its controller…in a sense. Blackboard/behavior tree dictate its actions. These run on the server, not on clients.
Edit … Here’s a quick vid that should help.
Hey, thanks for the reply!
I actually just figured out my issue. I was definitely changing the TArray on the server, which is why I was so confused. The server showed the proper value changes but no OnRep was being called.
I realized however that I had the TArray set to dolifetimereplicate_condition(CND_OwnerOnly). This explains why it worked on my character and things spawned by my character, because I was still the owner. On my AI however, the server is the owner so the repnotify never triggered.