mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-19 19:40:32 +00:00
fc0e6f3abb
* document mirror data types * Fix syncvar docs * Update doc/articles/Concepts/Communications/RemoteActions.md Co-Authored-By: MrGadget <chris@clevertech.net> * Update doc/articles/Concepts/Communications/NetworkMessages.md Co-Authored-By: MrGadget <chris@clevertech.net> * Update doc/articles/Classes/SyncLists.md Co-Authored-By: MrGadget <chris@clevertech.net>
30 lines
1.3 KiB
Markdown
30 lines
1.3 KiB
Markdown
# SyncVars
|
|
|
|
SyncVars are variables of scripts 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, like this:
|
|
|
|
```cs
|
|
class Player : NetworkBehaviour
|
|
{
|
|
[SyncVar]
|
|
int health;
|
|
|
|
public void TakeDamage(int amount)
|
|
{
|
|
if (!isServer)
|
|
return;
|
|
|
|
health -= amount;
|
|
}
|
|
}
|
|
```
|
|
|
|
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](../Concepts/DataTypes.md). You can have up to 32 SyncVars on a single NetworkBehaviour script, including SyncLists (see next section, below).
|
|
|
|
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.
|
|
|
|
## SyncVar Hooks
|
|
|
|
The [SyncVar hook](SyncVarHook.md) attribute can be used to specify a method to be called when the SyncVar changes value on the client.
|