#Set active sync

14 messages · Page 1 of 1 (latest)

eager basalt
#

hello! i was wondering how to synchronize the active state of a spawn object on the NetworkServer, using an rpc client is one solution but doesn't seem the most optimized, is there another way?
thanks !!

[TargetRpc] public void RpcSetTargetCursor(NetworkConnection target, GameObject cursorObject){ this.targetCursor = cursorObject; this.targetCursor.transform.GetChild(0).gameObject.SetActive(false); }

sinful jewel
#

if only one client should active/deactive it, there is not another solution instead of targetrpc afaik

eager basalt
eager basalt
sinful jewel
#

you are welcome

#

did you try sending network message instead of rpc, it may be better solution (i didnt try it yet)

eager basalt
#

I've never sent a network message, how do I do that?

sinful jewel
#

example:

public struct SetActiveMessage : NetworkMessage {
    public uint netId;
    public bool active;
}

server:

var msg = new SetActiveMessage {
    netId = objToUpdate.netId,
    active = false
};
NetworkServer.SendToAll(msg);

client:

NetworkClient.RegisterHandler<SetActiveMessage>(msg => {
    if (NetworkIdentity.spawned.TryGetValue(msg.netId, out var identity)) {
        identity.gameObject.transform.GetChild(0).gameObject.SetActive(msg.active);
    }
});
eager basalt
#

thanks a lot, I'll try! But I can't see where to put the server code.

#

since cursor activation is managed by the client and in a client script

sinful jewel
#

if you want to use a network message to send all connected players,
call Network Server.SendToAll(msg); method instead of targetrpc on server-side and create a method to check received message on client-side like:


//client-side
 public override void OnStartLocalPlayer()
    {
        NetworkClient.RegisterHandler<SetActiveMessage>(OnActiveChanged);
    }
    public void OnActiveChanged(SetActiveMessage msg)
    {
        
    }
#

as doc said