Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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: alt text The message "Executed on server!" only appears when the event is run by the host, and not when it's run by a client.

Product Version: UE 4.19
capture1.png (109.2 kB)
more ▼

asked Apr 23 '19 at 04:58 PM in Blueprint Scripting

avatar image

8 1 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

I think there are potentially two issues to look at here when diagnosing this issue.

Net Authority

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

alt text

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:

  1. Client -> Server: Can I do this action?

  2. Server -> Server (not an RPC, this is just a call inside the server): Handle request for action.

  3. Server -> Multicast to Everyone: This player performed this action, so Print String for everyone.

interact.png (30.5 kB)
more ▼

answered Apr 24 '19 at 01:50 AM

avatar image

136 2 3 2

avatar image NameP01NTER Apr 24 '19 at 06:40 AM

Thank you so much! I moved this code from the button blueprint to the player's blueprint and now it works! Thank you for taking so much time to write this answer, I learned a lot from it!

avatar image Deconstructor Apr 24 '19 at 05:11 PM

No problem, I'm glad it helped :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question