Download

Multiplayer Rain

How would you set up a rain system for multiplayer?

Currently on my third person blueprint, I spawn 1 cloud per player. Each cloud follows the player from above. When isRaining is true, each cloud should rain above each player. However this never works properly. I always end up with the rain either raining on the clients but not on the server or on the clients and server but the rain is out of sync. The system needs to work where if its raining it should be raining for all players (server and clients), or if its not raining it should not be raining for all players.

What’s the proper way to set this up? Thanks!

I imagine you would have the server decide when it should rain, and send a message to both clients that causes their clouds to turn on or off. As for the sync, this isn’t really something you can reliably sync over the internet, due to the inherent latency differences to server. Usually people accept that no client will be in the same “reality”, and only send frequent position corrections for things like character movement. The server letting both clients know they should start raining at the same time should be enough. The clients won’t know the difference, since even if they are desynced from each other, to them they both start raining at the same.

If you have a reliable network clock in place you can have the server send an rpc to start rain at n clock time. This approach in general will have a very low ms discrepancy, but it’s highly dependent on your network clock being fairly accurate.

A similar approach is to have the server send an rpc telling the client to rain in 10 seconds. On receival you deduct 50% ping from the 10 secs and set a timer to call the rain start event.

You can get pretty close to universal synchronization, but in general you’re correct. No two sims are ever truly insync.

Good article on ue4 network clock sync

The rain doesn’t have to be precise. If there is a second or two delay between all rain turning on or off, that is ok. I doubt the players will notice that. My problem though is as Capsu mentioned the system wants to treat each player as being in their own reality. So, I’ll end up with situations where the rain is spawning on and off randomly on different players. I need a way to isolate the isRaining variable to just the server, so that it controls all the rain. How would I set this up?

I’ve tried using a switch has authority and connecting authority to multicast, and remote to to server yet this ends up with the rain spawning in each player’s reality (rain is not in sync). I’ve also tried using is locally controlled on start and then connecting that to an event which runs on server, yet that doesn’t work either. When I just run a multicast event, the rain only spawns on the clients.

How do I get the code to allow just the server to modify the isRaining variable, and not allow the clients access to the variable? (This is where my main problem is. Replicating the isRaining variable is allowing the clients to make their own copies of the variable creating their own realities. Whereas not replicating isRaining variable also leads to client’s creating their own realities.) Then after the server modifies the isRaining variable, I can use that variable to determine if it should or shouldn’t be raining for all players.

I apologize if I’m missing a simple concept, but I’m new to replication.

I appreciate all the help guys!

I figured out the solution!!!

Here’s how to make multiplayer rain! The solution was “Is Server”. With that conditional I can separate the server’s code from client’s code, and the server’s variables from the client’s variables. After this, it was quite simple.

Note: Before starting, make sure your clouds are replicated and spawned. They need to be on some type of loop such as a timer event or tick. Make sure you are moving your clouds prior to the “Is Server” conditional so that your clouds can update their positions regardless if its raining or not. You’ll also need some type of particle effect for rain. I’m using a listen-server set up.

In your loop create a sequence. For the first sequence create an event which runs on the server. The code after this should move your clouds to a spot above the character’s location. In the second sequence create an “Is Server” and connect that to a branch. When this is true, everything following will be run on the server. Create a bool which is randomly set to true or false to determine if its raining or not. Create a sequence allowing the bool to randomly be set to true or false. This simulates unpredictable weather. On the last sequence set up a branch determining if your bool is true (raining is true). If true, connect that to a Switch Has Authority. On the authority branch connect that to a multicast event and on the remote branch connect that to a server event. Finally, set up a multicast event and connect that to the code which spawns the rain. Presto! Rain spawns on the server and clients at the same time and quits at the same time.

I hope this helps someone! :slight_smile: