Hello guys,
I got a weird problem, I have no idea what causes it.
Game: Multiplayer, Steam API, Blueprints Only.
Problem: Client sometimes does not spawn correctly. Means, he can´t move (essentially can´t do actions from PlayerController –> Pawn), some actions (PlayerController –> PlayerState –> Pawn) still work and input is recognized,
although it seems, that only the Multicast Events fire and only locally.Can´t see server or other clients move. Sees shooting correctly, bullets flying and hitting things, UI-Indicators still update (Are getting removed if someone gets an objective etc.). Only the movement appears to be the problem. Only error I get is a NullRef from the PawnRef getting called from an input action event in the PlayerController. No failed casts or invalid actors. Sometimes the client syncs up, sometimes he doesn´t (also quite random).
Also a weird thing, PawnRef is replicated and getting set on OwningClient aswell OnServer in the PlayerController. Now: After dying multiple times, local PawnRef has a different PawnName than the Serversided, but does not seem to be a problem, since
everything runs fine. This problem occurs randomly and I couldn´t replicate it (Only by dying over and over again, amount and or cause, or time, of deaths don´t matter.)
Tried everything I could, debugged everywhere something could go wrong, found no similar problems on google (Maybe PlayerState problem). Also disabled ragdoll, but didn´t make a difference.
When working with multiplayer, you often want to add delays to see if your problems come from replication delays. If adding delays does not fix them, it usually means you have a logic issue.
Once your problem is isolated (replication delay vs logic problem), you can work around not using delays.
I think I know what’s wrong.
You’re spawning the new character on Server and then you’re sending its direct reference to a client event “OnRespawn”. In this case, if you have a replication delay, the reference will be NULL.
Time 00 -> Server creates Character
Time 00 -> Server sends Character reference to Client (null reference since the character has not been replicated yet)
Time 01 -> Character is replicated to Client and appear at that moment (but your Event has already been fired so it’s too late!)
There are many things you can do to fix it.
Here is one solution:
Go on your Character Blueprint and search for the “OnPossessed” event. That’s an event the character receives when he gets possessed.
Create a client custom event that does everything your current “OnRespawn” client event does.
Plug it into the “OnPossessed” event (which is arriving with a server authority).
You should be fine.
To make sure I identified the problem don’t change anything to your code. First add a delay (3 seconds) after POSSESSING the character and see if you still experience your issue. If not, I think you got your problem.
I tried both of your suggestions, the problem that the local playercontroller had a different name than the serverside got solved.
The main problem that the client does sometimes not spawn correctly is still there (In build and in PIE).
What seems to cause a faulty spawn is when the client goes out of “sync”, means he does not see the server move and is in a different location on the server,
but he still sees grenades and bullets flying correctly.
If the client is in that state and is killed, he will not spawn correctly. All respawn events fire and playercontroller appears to have a valid reference.
Pawn can cast to playercontroller and vice versa.
He receives direct ActionInput events.
Gets casts from the PlayerState.
OnPossess and all respawn events fire.
But all events seem to only fire locally, even if they´re RunOnServer.
I can´t track down the bug, everything runs as it should.
They run locally because of “run on server IF OWNER”. you should run a server event on pawn. and from there -as you have authroity -call on player state. You do not OWN player state. the server does. so you CANT run “on server” stuff directly from client. Try it.
Try setting the owner of the spawned character right after you spawn it. (“SetOwner” to the controller you want).
Maybe just make one more try with delays by adding one in between “Spawn Actor from Class” and “Possess”.
I see we have clearly two ways of debugging and mine is far less efficient / useful in the long run. I’m debugging like the prototyper I am, adding / removing stuff that seem relevant and seeing what happens.
EDIT:
I definitely think there’s something going on with "Owner"s.
I commented the blueprints screenshots you gave us: