How do I stop simulating physics and attach to actor at the same time in a networked game?

I have an “item” actor that has a skeletal mesh root with a collision sphere attached to it. When a player drops the item, I want it to fall to the ground using simulated physics.

I know physics doesn’t play well with networked games where the location of the ragdoll is important to gameplay, so I plan to enable physics on the item if it is falling and toggle physics simulation off after it hits the ground. That way, the location of the item is relatively consistent between all players. I am trying to prototype this in blueprints but I’m having some trouble.

I have a repnotify variable, bIsFalling, which I set to true when the player drops the item and false when it hits the ground or it is being picked up. When the repnotify fires, all clients simulate or stop simulating physics depending on whether bIsFalling.

In one scenario, the player drops the item, causing bIsFalling to be set to true, which makes the item start simulating physics. While it is falling, the player picks up the item again, setting bIsFalling to false, stopping physics simulation and attaching the item to the player’s hand. The problem is, attachment is replicated, and when bIsFalling and attachment is replicated on the same frame, the clients may not have stopped simulating physics, which makes attachment fail. This results in the item attaching to the player on the listen server, but not on clients.

That is the base of the problem and I’m wondering if anyone has had this issue before and how they fixed it, because this problem also causes numerous other issues I’m having. Basically, I need to ensure clients have stopped simulating physics before setting up an attachment on the server/authority.

One way I’ve fixed this is by adding a 0.1 second delay. I can see why this works because as far as I know, replicated variables are sent to clients in a random order on a frame-by-frame basis, so as long as I tell clients to stop simulating physics on frame 1 and set up the attachment on frame 2, it should theoretically process in the right order on clients, but I also tried this with a “Delay Until Next Tick” node, which does NOT work. I also tried using Event Tick to process an attachment after I set physics simulation off, which also doesn’t work. Why exactly does a 0.1 second delay work but “waiting till next frame” does not? It makes me worried. I plan on moving this code to C++ and I can’t really use a Delay node there in the same way.

Thanks, and let me know if I need to add more details!

Why not set attachment from the RepNotify event ?
Change the boolean to a variable of type pawn/character whom the item should be attached to.
On server, set the variable to the desired character, or null for detachment.
On repnotify (which also triggers on server), if variable is null then detach and simulate physics, else stop simulating physics and attach.

I was not setting attachment from a RepNotify event because I figured that since AttachParent is a replicated variable inside of USceneComponent, I should only attach on the server. However, I also realized I forgot to tick bComponentReplicates on my skeletal mesh, which seems to fix the issue I was having with clients not attaching properly.

This leaves me a few questions, like, why does attachment ever replicate when my component is not replicating? I would expect that attaching a non-replicated component on the server would not attach on clients. I’ll need to do some digging in the code…

To replicate movement in Unreal Engine, you need to set the “Replicate Movements” option to true in the “Replication Settings” category of the actor’s class defaults. This will ensure that the root component of the actor is properly replicated across all clients.

To rotate the item, you can add a “Rotating” movement component to the item actor and set its “Rotational Rate” property to the desired value. This will cause the item to rotate continuously.

To set up proper interaction with the item, you can use a setup interaction widget function to define the behavior of the item when it is interacted with. For example, you can use the “Page Label” event to detect when the item is picked up and then use the “Set World Location” function to move the item to the player’s hand.

To ensure that the item’s location is properly replicated across all clients, you can use the “Rep Notify” event to notify clients of any changes to the item’s location. You can also use the “Rep Notify” event to notify clients of any changes to the item’s rotation.

To prevent the item from vibrating or oscillating when physics simulation is turned on and off, you can use the “Rep Notify” event to notify clients of any changes to the item’s location and rotation, and then use the “Set World Location” and “Set World Rotation” functions to update the item’s location and rotation on the client-side. This will ensure that the item’s location and rotation are always consistent across all clients, even when physics simulation is turned on and off.