Replication issue

Hello, I have an replication issue on my my multiplayer online game. So, when someone join the game the server spawn a character, the player controller possess it and it should be fine. But, when another player (not the host) join the game, the server create the character, the player controller possess it and I can’t access my variable with the pawn reference on the pc, it says there’s nothing on it. Thanks in advance for your help ! And sorry if you need further information, I don’t know what to say else so just tell me ^^

I think it would be wise to just set the default pawn in the game mode to whatever you want and let Unreal handle the rest for you. The issue itself is probably related to how the network architecture is built. On clients, you have a lot less information than on the server.

I want to get different characters so no. There’s a way to do that, I did it before, I’m just moving everything control based into the player controller so it doesn’t work like before

No one? Up !

Nothing comes out of my pawn ref node, but if the variable type isn’t a base controller, but a string, it works… Help please

Hi, do you call this directly when a client joins the game? If so consider making this a replicated or repnotify variable instead of using a RPC to set it.

Else maybe when you spawn it on the server, then the actor has not replicated to the client and does not exist on the client when you try to sent a reference to it (don’t know just a wild guess). If so, then either try using a delay to give the client time to catch up, or again try using repnotify instead of a RPC.

Hello, I call this when after a client connect yeah. RPC? Gonna try this thanks ^^

[quote=“chrudimer, post:6, topic:148637”]

Hi, do you call this directly when a client joins the game? If so consider making this a replicated or repnotify variable instead of using a RPC to set it.

Never used a replicated variable/repnotify, do you have a starting point?

Sure, basically if you set a variable to “replicated”, that means that if the server thinks that this variable has a different value on a client, then the server will send an update to this client. So for example if the “Controlled Character” variable would be set to replicate, then if it changes on the server, it will also automatically change on all relevant clients (in this case on the owning client only).
So that would mean you only need to set this on the server and it will be automatically set on the client and you would not need to use a RPC to set it.

But if you want to execute some logic as soon as the variable changes, then you would use “repnotify”. If you set a variable to be “repnotify”, than that is the same as if you would set it to be “replicated” plus a function gets called on the server and the client every time the server issues an update.
So if you set a variable to be “repnotify” than that will automatically create a new function. So for example if you would set the “Controlled Character” variable to be “repnotify” and it changes on the server then the server will also execute the function. Also as soon as the update arrives on the client, this function will also get executed on the client.
[HR][/HR]
I suggest you watch this video here https://www.youtube.com/watch?v=a8ukx6nPub0

Further you could take a look at the content examples for networking (under the learn tab of the epic launcher you can download the project), here’s the doc to that Content Examples Sample Project for Unreal Engine | Unreal Engine 5.3 Documentation

And as always (this is/might be too much at the beginning though) Multiplayer Network Compendium | An Unreal Engine Blog by Cedric Neukirchen
[HR][/HR]
And if you’re doing a multiplayer game, then you will need to make use of replicated variables and repnotify =)

Thanks you :smiley: I remember doing this video, very interesting ! But the point is that I can’t use a replicated variable, since I want every Player Controller to get the reference of his own Pawn (or maybe it’s not the good way to do it?)

So, when my pawn is created I set the variable (replicated) controlled character to the pawn ref, it got a value out of the spawning fonction, but when I’m into the PC and not the GM there’s nothing… (same problem as before)

The problem is that you do not give the client time to catch up with the server and execute the “RespawnPlayer” RPC too early.

Every client only knows about his own player controller. All the other player controller do not exist there and only the server knows about all of them. Therefore if you would set a variable to be replicated it would only be between the server and the owning client.

That’s the expected behavior from the code =)

The problem is that you spawn the actor on the server and then directly use a RPC (RPCs get send the moment you call them, stuff that’s replicated only updates at the earliest at the end of the frame). Problem is that when you execute the logic on the client the actor you’ve spawned has not replicated to the client and does not exist there. Therefore the reference you send through the RPC “Pawn Ref” will be None.

Further the RPC might/will execute *before *the replicated variable “Controlled Character” has updated on the client, and therefore that will also be None if you access it there.

If you would put in a delay before you call the RPC, then the actor you’ve spawned would have time to replicate to the client. But generally speaking using a delay is not reliable since the client may have a slow connection, or slow loading times and therefore the delay you’ve used may be too low.
[HR][/HR]
If you want to keep variables in sync between the server and clients, then you could set those to be replicated.

If you also want to execute some logic as soon as the variable changes you could use “repnotify” instead of “replicated”.

If you want to access some variables from an actor on the client as soon as the actor has been spawned, then you could do that inside the “EventBeginPlay” of that actor. If you only want the owning client to do so, then you can compare the controller of the pawn “GetController” with the player controller executing this logic (“GetPlayerController” with index 0, that will always be the local controller) and only execute the logic if both are the same.

Isn’t that the point of the nodes? I mean, that’s weird because if I plug the RPC after the pawn creation, it should perfectly work since the pawn is created and after that the RPC is called.

Well, I don’t know if the delay wasn’t high enough, but I think it was good because when I tried this, I see the client waiting before spawning, but I got the same result.

As I said, I don’t really care about the server knowing that this player play with this pawn (actually, maybe later but now no)

Also, I tried to pull it back in the PC, and nothing work again, when a client join the game the variable isn’t updated, but that was working great before…

You call the RPC after it has been spawned on the *server, but you execute the logic on the client. *The pawn that has been created on the server needs time till it replicates to the client. Before that happens it does not exist on the client.

If that is the cause of the problem, then it will be solved if you would put a (long enough, but generally the default 0.2 sec should be enough) delay at this position here


[HR][/HR]

Then you don’t need any replication or RPCs, you could just access all the information locally either in the event begin play of the pawn, by using “GetController” and then calling an event inside the controller (first check whether the owning controller is executing this (“IsLocallyControlled” should return true) and only continue then, since every client is going to execute the event begin play but you only want the owning controller to execute this logic), or via “GetConrolledPawn” inside the controller.

If you would use “GetControlledPawn” you would again need to wait until the pawn has replicated from the server to the client, therefore I would rather do this inside the event begin play of the pawn, since the client will execute this as soon as the pawn has replicated from the server and therefore has been created on this client.
[HR][/HR]

In the two images you have shown you’re executing all the logic on the server only. Therefore you only set the “Controlled Character” variable on the server version of this player controller.

I tought I was placing the delay correctly, apparently it was here, thanks you :slight_smile: