Run an event on the server from the client
Hi, I'm trying to understand UE4's Multiplayer system. I want a client to be able to execute an event on the server. How can I do that? Here's what I've tried: The message "Executed on server!" only appears when the event is run by the host, and not when it's run by a client.
asked Apr 23 '19 at 04:58 PM in Blueprint Scripting
I think there are potentially two issues to look at here when diagnosing this issue.
Firstly, looking at your Blueprint, I noticed the Server Side Event target is "Red Button", so I'm guessing from the name that this is probably an object placed in the map and probably owned by the server, rather than (for example) your character blueprint where the NetRole is ROLE_AutonomousProxy, and there's some chain of ownership that ultimately ends at a player controller somewhere.
If you look at the rules here, it's possible that your RPC (Remote Procedure Call) is getting dropped, because you're calling it from a client that doesn't have any kind of claim to ownership over that object:
If this is your problem, and assuming this is some kind of button in the world that you can press, the way I'd solve it is to have some kind of "Interact With Object" event in your character blueprint, where on that character's owning client sends an event to the server saying "I want to interact with this object". The server will then call the relevant function on the button, which will then Multicast the results to everyone else.
Hopefully the following UML sequence diagram makes it a bit clearer
The server is the true authority on who actually interacted with the button, in cases where multiple people try to interact with the button at the same time, if you have the kind of game where that's not allowed. In those kinds of games, you'd typically unicast a message back to the client that attempted to interact but failed, letting them know that the interaction didn't go through, so they can cancel animations, play a sound, or show a message on screen or something like that.
Be wary of making the code inside your character blueprint too-specific to the actual button. I'd probably create a Blueprint interface for an "InteractableObject", and have the button implement this in its own way.
Non-Replicated Side Effects on the Server
Another question to ask yourself whenever you're sending an RPC to the server to perform some kind of action is:
Is this action actually visible to other clients, or only to the host/server?
If you RPC to the server, and you change a property on an actor that's replicated, or call a multi-cast RPC. Then yes that change will be replicated to everyone else, because the property or function is replicated. It won't just happen automatically for everyone if you call something that doesn't replicate.
In the case of Print String off the top of my head that's not replicated at all.
So even if you successfully RPC to the server and it calls Print String, all that's going to do is print a message on the host's screen. No one else will know about it, because Print String is just something that happens locally for whoever calls it. If your Print String node is set up to print only to the screen and not to the log, then if the game host happens to be a dedicated server it probably won't even do that (this is just off the top of my head, I can't remember what Print String's exact behavior is)
In these kinds of cases where you want the behavior to happen for everyone, and that behavior isn't already replicated, you're going to need to create another RPC from the server that Multicasts to everyone to tell them to perform that action.
So the sequence of events is:
answered Apr 24 '19 at 01:50 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here