Trying to sync physics object location for clients that join games in progress

I have a small problem with physics object locations when players join my game in progress.

The physics objects are important in my level so I have them spawned on the level. They’re not being spawned from blueprints or scripts or something like that.

The problem only occurs if the physics object has been moved from its starting location when the client joins.

The problem is that if a physics object gets moved before a new client joins, the client will see the object at the original location, not the new one.
Ofcourse the client won’t be able to do anything to the object he sees, since the real object with collision is at the new location.

The objects are marked as “Replicated”, however the replication does not seem to trigger unless the object gets moved after being loaded on the clients.

As soon as the host (who sees the correct location) bumps the object, it’ll get synced for the clients and everything works fine from now on.

I tried adding a loop that gets triggered and runs server side that then gets the object location and triggers a client side event which uses the location and moves the object to it. However it does not seem to be that simple with objects that are loaded in the level.

Here’s a messy blueprint screenshot, it’s mainly the stuff marked inside the orange circle. I tried having the “client” event replicated as “run on owning client” with same results. although its not set to that in the screenshot.

On clients, nothing executes in that blueprint, and on the server it’ll loop and run through the authority switch. Even with the event set to “Run on owning client”, it’ll only execute on the server.

Maybe (probably) I’m missing something relatively simple.

TLDR

I need to find a way to constantly update the location of a physics object that spawns with the level for any client that joins once the game has started.

Currently I’m only able to run any “set location” nodes only on the server so it won’t get synced to clients. However it gets synced if the host moves the object.

The objects are marked as “Replicated”.

You might want to check out the CouchKnights example. I think they might be doing something like what you are looking for. Maybe.

Cheers,

Thanks for the tip. I tried looking around there but couldn’t quite figure it out.

The physics objects there are replicated, like mine. I didn’t find game instance or game state in the couch knight demo.

I’m hoping for a blueprint based solution to this, in case they solve this with c++ in the couch knight demo.

For some reason you have to override OnRep_ReplicatedMovement by yourself and set the location and rotation there.
This fixes the problem with join-in-progress players see physic actors on the wrong position if they have been moved away from their starting position before.

Here is the code:


void ASomeActor::OnRep_ReplicatedMovement()
{
	Super::OnRep_ReplicatedMovement();

	if (!GetActorLocation().Equals(ReplicatedMovement.Location, 1.0f)) {
		SetActorLocation(ReplicatedMovement.Location, false, nullptr, ETeleportType::TeleportPhysics); 
	}

	if (!GetActorRotation().Equals(ReplicatedMovement.Rotation, 1.0f)) {
		SetActorRotation(ReplicatedMovement.Rotation, ETeleportType::TeleportPhysics);
	}
}

I saw someone asked a similar question on reddit, but i have no reddit account. So if someone could send him this thread that would be great!
https://www.reddit.com/r/unrealengine/comments/4aleho/helptrying_to_sync_physics_object_for/

Greetings!

2 Likes

You sir, are a God! Atleast mine. Thank you!

Looking for that like a week now and finally found your post that helped me. In my case, this behaviour happens (in 4.19.3 atleast) when I possessed an AIControlled Pawn with a PlayerController and when the AIController possesses it back.