I wanted to be able to sync an Entity across all clients.
I have a Lamp Entity with the Ghost Authoring Component and have a LampData Component, set as a Ghost Component with PrefabType All, that have a bool LampState and a reference for the PointLight Entity (child of the Lamp).

Then I also have a InteractSystem, when the player click on the “E” key, will toggle the LampState on the LampData Component, and disable/enable the PointLight Entity. I wanted the InteractSystem only to run on the client side so I used the “[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]”.
I though setting the LampData Component as a Ghost Component with All on the PrefabType would synchronise that component on the Server and subsequently on all other Clients, but that was not the case…
How could I synchronise this Entity across all clients, so when the LampState changes on the Local Client this change, also changes the LampState on the Server and subsequently on all other Clients?
What you’re describing is client authority, where a client has the authority to impose their state on the server (and other clients). Netcode for Entities is a server authoritative package only. To do what you want, your client will need to send an input or an RPC to the server, have the server read that input/RPC, update that state and wait for it to be replicated to other clients. To make this more reactive (because that operation will be subject to internet lag) you can also use client prediction on your first client to predict that state change.
Don’t forget to add a [GhostField] attribute to any field you want to sync. GhostComponent is not enough.
5 Likes
So, would it be better if I don’t use a Client Authoritative flow for this case?
To change that I would need to make the InteractSystem to run also on the Server side, so that when the local client presses the key it would send that interaction to the server changing the Lamp state on the server first and only then the state would be propagated for the Clients… is that it?
yep that’d be it. The only way for state to propagate to other clients in Netcode for Entities right now is to have the server update that state (and that state to be marked with GhostFields).
So the timing would look like this:
- client sends inputs (and optionally predict that input’s result)
- internet lag
- server receives input and changes state
- internet lag
- all clients (including first one) receive resulting state
You can checkout some nice sequence diagrams that explains that flow more in details here Client prediction | Netcode for Entities | 1.3.0-pre.4
2 Likes
Sorry to bother again, just a quick question… using this flow, if I add/remove a component to the Ghost Entity on the server, will it add/remove it on the Client too?
Because now the light state will change on the server first and it will remove/add the disable component so it shows the lamp’s light turning on/off… but on the client the disable component is not being removed/added…
Is there a good way to synchronise the added/removed components of a Ghost Entity?
You can sync IEnableableComponent components
See the doc Ghost snapshots | Netcode for Entities | 1.3.0-pre.4