Question by
kostya05 · Oct 15, 2015 at 01:37 PM ·
c#networkingrpcclientclient-server
SyncVar works not always
In advance I apologize for my google translate
Why time is synchronized, and hp isn't synchronized?
public static class NetworkCommands
{
public enum TypeField { OnlyServer, OnlyClient, All };
static public void PostFloat(ref float obj, float value, NetworkBehaviour scr, TypeField type = TypeField.All)
{
if (scr.isServer && type != TypeField.OnlyClient)
RpcCommands.RpcFloat(out obj, value);
else if (type != TypeField.OnlyServer)
CmdCommands.CmdFloat(out obj, value);
}
static public void PostInt(ref int obj, int value, NetworkBehaviour scr, TypeField type = TypeField.All)
{
if (scr.isServer && type != TypeField.OnlyClient)
RpcCommands.RpcInt(out obj, value);
else if (type != TypeField.OnlyServer)
CmdCommands.CmdInt(out obj, value);
}
static class RpcCommands
{
[ClientRpc]
static public void RpcFloat(out float obj, float value)
{
obj = value;
}
[ClientRpc]
static public void RpcInt(out int obj, int value)
{
obj = value;
}
}
//Class with a bug
public class Game : NetworkBehaviour {
[SyncVar] public int hp;
[SyncVar] public float timer;
[ClientCallback]
public void Update()
{
if (!isStartGame)
return;
timer -= Time.deltaTime;
Energy = EnergyMax - MainScore * 0.02f;
}
[ServerCallback]
public void FixedUpdate()
{
if (timer <= 0 || NeedTimerUpdate)
{
NetworkCommands.PostFloat(ref hp, hp-1, this); //Work only for host, if client reconnect to game, then "hp" receives sync value
NetworkCommands.PostFloat(ref timer, Energy, this); //Work always for host and client
}
}
}
Comment