Will I fully understand Replication someday?

@zeaf and @Bojann a million thanks for this

1 Like

is it possible to tag both as solution?

So here is the example what I did in my game Barely Racing.

It is a combat racing game.

GameState would hold the rules - Number of laps, allowed weapons… etc

PlayerState would hold ammunitiion number for mines, machinegun and rocket. If muzzle particle effect would need to be shown when player fires tre weapon, I would use Multicast event for it, so it’s spawned in both places - server and client.

Player state would hold players health also… etc.

I hope it’s clear a bit for you.

2 Likes

ok…In my current user case would be a ‘restart race’ multicast so option would be gamestate here

just remember, if you want to multicast from the game state, only the server can do it :slight_smile:

2 Likes

Yes, restart race is something that is a “game rule”, and it should be part of GameState. At least I did it like that.

1 Like

ok…but multicasting straight from playerstate with no ‘run on server’ first is a bless

only if the server does it

go through this documentation, which should help clear up any confusion

it’s really important to understand net ownership when developing for multiplayer

Actor Owner and Owning Connection in Unreal Engine | Unreal Engine 5.4 Documentation | Epic Developer Community (epicgames.com)

1 Like

the only thing i’d add is IF you can use a RepNotify over a multicast you probably should,

multicasts can fail and can be missed, or worse if reliable can disconnect the game
not trying to scare you off though, they have their uses but a lot of what i see people do should be a notify.

1 Like

problem with repnot is variable must actually change to trigger…
I actually used as trigger by just flipping a repnot bool variable but dont feel
clean

For clarity…

On Join the Game Mode creates a controller and replicates it to the joiner. Server stores a copy.
It then Spawns the default pawn and possesses it with said controller. The pawn itself being set to replicate, will eventually replicate to the owner and all others.

Copies of controllers are not replicated to other players. Only the owning client and the server have copies of controllers.

Players States for all players are stored in the Game State PlayerStates array. The owning controller has a reference to its “owned” player state.

The GameState and its PlayerStates array is replicated to all clients.

For sanity sake you can use OnPostLogin in the GM to store a copy of all controllers for your usage. e.g. PlayerController array. Just be sure to CAST to your specific controller class before storing.


Replication only works if the Server makes the change. It’s always Server → Client.

RepNotifies are used for function processing upon variable state change. Thus the OnRep_function that’s automatically created. These are great for state persistence. Say a door state in which the onrep function controls the opening/closing action based on the variables value.

Remote Procedure Calls (RPC) are used to “Request” an action or “Notify” the server of an action.

e.g. Input Fire of a weapon.
You’d RPC the server notifying it you pressed Fire. It in turn validates if you can fire (has weapon, weapon has ammo etc). If Valid it spawns an authoritative projectile.

In a sense its a notification and a request. “Hey I did this, can you spawn a projectile?”

Multicast is a Server → Client response that is sent to all clients. MC’s target an event in the class it was called from on the server.

e.g. A MC executed in the character class on the server will call the character class on the clients.


If you need to execute logic on all client controllers there’s two approaches that are definitive.

  1. Loop the GM playercontroller array and call an event on each.
  2. MC from character class (on server) to call an event that calls a player controller event.

Do not forget to filter your called multicast events. Use Get Local Role to specify which proxy is allowed to execute the logic.

e.g.


3 Likes

Great post! Thanks!! :fist_right: :fist_left:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.