Call a method on server spawned actor on all clients

Hello all!

Bit of an interesting question. I’ve got the following setup for an item use system (think Skyrim/Fallout style):

  1. Client controlling a Pawn opens their inventory (UI) and presses a button for an item.
  2. UI calls a method on the Pawn which is marked as “Server” in the UFUNCTION() which takes the item data as a parameter.
  3. Server receives the RPC call and uses the item data to spawn the item, and all clients have the item appear in their worlds.

So far, this works fine. The issue is that step 4 of this process requires the server to call a multicast method on the player entity which takes the spawned item as a parameter. However, if I do that, the newly spawned item will be null on all connected clients (likely because the replicated actor has yet to arrive at the client). Do you guys have a best practice/better approach for something like this? How can I execute a method from the server on all connected clients which takes a newly spawned actor as a parameter?

Is it the same function for any spawned item? Can you maybe call it from the Item itself on its BeginPlay() for a locally controlled character? By doing so, you can also avoid sending additional data through the network.

I was thinking about this more last night, I think the best solution might be.

  1. Client opens UI and selects item and makes call to server indicating it wants to use an item.
  2. Server spawns the item and calls “MulticastOnUsed()” on the item.
  3. All clients and the server execute the MulticastOnUsed() method once the item actor is replicated and that calls a method back on the using pawn. This ensures the item actor pointer is valid.
  4. Finally, this calls the OnUsed() method on the item.

It’s not the cleanest solution (ideally, I would have liked the item base class to not have to contain any RPC calls), but I think it might be the best approach.

What do you think? Anything seem too convoluted there?