I’m trying to get a Lightbulb toggle on a switch working on Client and Server Side of my Game.
I’ve watched the Intro to Blueprint Network-Tutorial and decided to setup a RepNotify to let the client know when the Light is switched on.
The script is working, but only from the ServerSide.
So if I use the switch in my Server the Light is toggling on both Client and Server, when using the toggle on my Client however only the clients light is toggling.
I’ve tried using a Custom Event “Run On Server” and then Multicast it to all other Clients, but that doesn’t seem to work either.
How can I tell the Client to Ask The Server to Switch the Light of every client connected?
I’ll attach my blueprints and two screenshots showing the issue.
Thanks in advance Max
I’ve changed the Blueprint so a PlayerController Custom Event is called whenever the button is pressed. The Custom vent gets the light as a parameter and toggles visibility. When I test it however the client cant see the light he triggers, but the server can. So I press the button on client side and it shows up on server but not on the client. Why is that? Here are the updated screens
There are two main concepts within networking in unreal. The first one is variable replication, mainly changing a variable and replicating it always from the server to a given client. The second one is function replication, calling a server function from the client or calling a client function from the server, this is also called a RPC (Remote Procedure Call).
What you want to achieve is a mix of both, the client toggles the switch and has to call a server replication function to change a variable. The variable must then replicated to to all clients via replication and when the event happens on each client you have to update the switch accordingly. The client that calls the replication function could just update the light bulb on his box so that you simulate the behavior and it will look behave better in net work saturation.
Just as a note, make sure to replicate to all clients. The variable won’t be replicated to the players that are hosted in the server itself though, there you have to update the bulb locally.
Hope I could help!
Hey Moss, first of all thanks for the reply. I’m aware of the methods you mentioned and tried to use these methods. Therefore I created a Custom Event on my PlayerController which is basically the server replication event, since it can only be executed when the “Switch Has Autohoriy” function is replying Authority. Then I try to set the targets visibility. But how can I manage the functionality of replicating that Light back to the clients, when it is updated by the server?
You have to create a boolean variable in your light bulb BP for example, that you will use to handle the on/off state. The trick is to use RepNotify within the replication section of that boolean, it will then create a function in your BP that will be called when the variable changes, and so you can updatethe bulb.
Here is a nice crash course about replication in BPs: A Crash Course in Blueprint Replication - Unreal Engine
Thanks so far, but I just don’t get it. I get the fact, that RPC are calling form client to server but how can I execute them then…I don’t get that:
The Multicast seem to be the ones I’m looking for, since I want to execute on every machine. however it says “Replicated to All (if server)”…well this isn’t very helpful since I want to execute the whole thing on a client and still let the server update every clients machine. Now it’s pretty much vice versa than before.
Before: Server Light is toggled, carless of whom used the trigger. Client Light is only triggered, when Server used the Button
Now: Server Light is toggled when Server is using the Button. Client Light is triggered regardless who is using the button.
Sorry for being such a pain in the … I just don’t get the concept…
Addition: When using “Run On Server” the client clearly doesn’t own the Trigger, so it doesn’t execute the trigger whatsoever, the server on the other hand is handling the trigger well then.
I will try to setup a test scenario this evening (I have no running engine right now). But what you Press E you should send the event to the server I guess. This is normally done from a PlayerController which the client owns.
The PlayerController has authority in the client over its own so you can send a server call from there, once on the server side you search for the bulb (a trace or even a simple radius check to see if the player controller is able to switch the bulb). If you found a light bulb (this is applicable to any BP if you build a usable system) on the server you then change the variable (is on) accordingly and switch the bulb itself on the server (so for those clients that are local to the server it is automatic). What you have done this way is to reuse the player controller to route the ‘use’ action to the server which in turn performs the ‘use’ action on the bulb resulting in the replication of the ‘is on’ variable to all clients.
To make the light switch instantly on the client that actually switched it I would switch the light bulb locally (without changing the variable locally though!) and then sending the server call from your PlayerController.
As I said, I will setup an example scenario on how to perform such a thingy ^^
Hey Moss I got it working…kinda. I can toggle the light with both Server and Client. However the server has to toggle the light a few times before the client can use it but idk why actually
Edit: Oh ok, there seems to be sth wrong with my Check Trace, since it’s always taking the servers trace instead of the clients, so I have to look at the Button with my server and then i can toggle it, with my client.
Edit2: OK, I just had to move my Check Trace before my “Run On Server”…of course^^
Now I’ve got a fully functioning Blueprint
For all the people out there having the same issue, here are my Blueprints:
Also you have to check the initial state of the variable. For example if Light On is TRUE by default it wont be replicated because there isn’t any change.
This will lead to the issue that you have to toggle it off and on to function correctly. To fix it you have to turn the light depending on the default state of the variable or in the constructions script or the begin play event.
Nice it worked!
Well I do in the yellow section Moss
this explanation is so helpful, thank you !