Mirror/doc/Articles/Guides/Sync/SyncVars.md

79 lines
3.0 KiB
Markdown
Raw Normal View History

2019-03-30 13:36:27 +00:00
# SyncVars
2019-09-11 18:49:34 +00:00
SyncVars are properties of classes that inherit from NetworkBehaviour, which are synchronized from the server to clients. When a game object is spawned, or a new player joins a game in progress, they are sent the latest state of all SyncVars on networked objects that are visible to them. Use the `SyncVar` custom attribute to specify which variables in your script you want to synchronize.
2019-03-30 13:36:27 +00:00
2019-09-11 18:49:34 +00:00
The state of SyncVars is applied to game objects on clients before `OnStartClient()` is called, so the state of the object is always up-to-date inside `OnStartClient()`.
SyncVars can use any [type supported by Mirror](../DataTypes.md). You can have up to 64 SyncVars on a single NetworkBehaviour script, including SyncLists (see next section, below).
2019-09-11 18:49:34 +00:00
2020-02-23 07:48:21 +00:00
The server automatically sends SyncVar updates when the value of a SyncVar changes, so you do not need to track when they change or send information about the changes yourself. Changing a value in the inspector will not trigger an update.
2019-09-11 18:49:34 +00:00
> The [SyncVar hook](SyncVarHook.md) attribute can be used to specify a method to be called when the SyncVar changes value on the client.
## SyncVar Example
Let's say we have a networked object with a script called Enemy:
``` cs
public class Enemy : NetworkBehaviour
2019-03-30 13:36:27 +00:00
{
[SyncVar]
2019-09-11 18:49:34 +00:00
public int health = 100;
2019-03-30 13:36:27 +00:00
2019-09-11 19:07:08 +00:00
void OnMouseUp()
2019-03-30 13:36:27 +00:00
{
2019-10-02 17:48:58 +00:00
NetworkIdentity ni = NetworkClient.connection.identity;
2019-09-11 19:07:08 +00:00
PlayerController pc = ni.GetComponent<PlayerController>();
2019-09-11 18:49:34 +00:00
pc.currentTarget = gameObject;
2019-03-30 13:36:27 +00:00
}
}
```
2019-09-11 18:49:34 +00:00
The `PlayerController` might look like this:
2019-03-30 13:36:27 +00:00
2019-09-11 18:49:34 +00:00
``` cs
public class PlayerController : NetworkBehaviour
{
public GameObject currentTarget;
2019-03-30 13:36:27 +00:00
2019-09-11 18:49:34 +00:00
void Update()
{
2019-10-02 17:48:58 +00:00
if (isLocalPlayer)
if (currentTarget != null)
if (currentTarget.tag == "Enemy")
if (Input.GetKeyDown(KeyCode.X))
CmdShoot(currentTarget);
2019-09-11 18:49:34 +00:00
}
2019-03-30 13:36:27 +00:00
2019-09-11 18:49:34 +00:00
[Command]
public void CmdShoot(GameObject enemy)
{
2019-10-02 17:48:58 +00:00
// Do your own shot validation here because this runs on the server
2019-09-11 18:49:34 +00:00
enemy.GetComponent<Enemy>().health -= 5;
}
}
```
2019-03-30 14:20:02 +00:00
2019-11-28 02:48:41 +00:00
In this example, when a Player clicks on an Enemy, the networked enemy game object is assigned to `PlayerController.currentTarget`. When the player presses X, with a correct target selected, that target is passed through a Command, which runs on the server, to decrement the `health` SyncVar. All clients will be updated with that new value. You can then have a UI on the enemy to show the current value.
## Class inheritance
2019-11-28 02:48:41 +00:00
SyncVars work with class inheritance. Consider this example:
```cs
class Pet : NetworkBehaviour
{
[SyncVar]
String name;
}
class Cat : Pet
{
[SyncVar]
public Color32 color;
}
```
You can attach the Cat component to your cat prefab, and it will synchronize both it's `name` and `color`.
2019-11-28 02:48:41 +00:00
> **Warning** Both `Cat` and `Pet` should be in the same assembly. If they are in separate assemblies, make sure not to change `name` from inside `Cat` directly, add a method to `Pet` instead.