Simple VR object replication

Hey, I am trying to replicate on all clients a simple function for an object to be gripped and dropped. Is it necessary to also add “switch has authority?” node here or will it also work if the server executes the “execute on server” events? Also, is it necessary to pass in the New Parameter through the events or can I just connect it from the “Event Pickup” interface?

The “Event Pickup” interface most likely only executes on client as a result of pressing a keybind, so you need to route the call through server to inform everyone, as you are doing.

However this most likely won’t work properly because calling events on server from client only works when client owns that actor. In the case of an actor (book) placed in the world that anybody can pick up, this approach is not gonna hold up.

To properly call the server event you need to route it through a class that the client owns, such as the character or playercontroller. In EventPickup you have an “Attach To” parameter. I assume that would be the character. In your character class add a custom event such as “Server Grab This” set to run-on-server, with a parameter of type Book or Actor. Now from the EventPickup you can call AttachTo->ServerGrabThis(Self). Now your function is properly called on server side. From the ServerGrabThis event on server, you can do the multicast to inform everyone else and attach the thing.

To answer your other questions, “execute on server” will always execute if called from server, and will never execute on client machines, so you don’t need any authority switches. And you shouldn’t ever connect pins across different execution paths unless you really know what you are doing. In this specific case, connecting the AttachTo pin directly to its uses after server+multicast replication will lead to issues for everyone involved except for the host in case of listen server or standalone.

Kind of complex to wrap my head around, but conceptually this works. May I ask to also post a screenshot of how you would do that. If you can’t thanks a lot anyway!

BP_Book

BP_Character

This is far from ideal, but at least it should be a working starting point.

Later down the line you might want to consider a few things :

  • If you have different actors that need to attach themselves in different ways, you might want to create a new interface so you can call an attach function on the target actor, and let the actor do the attachment, rather than doing it in character.

  • While the action of grabbing is an event, the attachment of an actor is a state (unless it’s really temporary). State should generally not be replicated with multicast events. Consider player A grabs a book, holds it, then player B joins the game. The multicast events will not run for player B, so the book will not be attached from his pov. For such situation we use replicated variables. If players can only ever grab one thing at a time, that variable could be in BP_Character, ie. add a variable of type Actor to hold a reference to the current grabbed actor, make it replicated. If players can grab multiple things simultaneously, you can put variable in the actor instead, to hold a reference to the character currently grabbing it. Use replication=RepNotify on the variable to execute code whenever that variable gets updated (the function OnRep_VariableName is created automatically by editor). This way when player B joins the game, he receives replicated value of the variable that tells him book is grabbed by player A, and the function OnRep_Variable is immediately called on his machine so you can execute attach code.

2 Likes

Thanks a lot, that really helped me. Is there a certain way I can give you a badge or something?