- Home /
SyncVar is not changing on server
Hello,
I'm trying to build game with new Unet. I have player with NIdentity, NTtransform etc.
In my script is var dirIndex for direction of player sprite (up, down, right, left). When is dirIndex changed it will be change sprite of SpriteRenderer. When is dirIndex changed on Host, changes will be conducted on Client, conversely, when Client change his dirIndex, server not register that.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class Control : NetworkBehaviour {
[SyncVar(hook="OnDirIndex")]
public int dirIndex;
// Update is called once per frame
void Update () {
if (!isLocalPlayer) {
Debug.Log(dirIndex.ToString());
return;
}
position = rb.position;
if (Input.GetKey(KeyCode.W)) {
newPosition = position + Vector2.up * speed * Time.deltaTime;
sr.sprite = playerSprites[0];
dir = Direction.Up;
dirIndex = 0;
}
if (Input.GetKey(KeyCode.S)) {
newPosition = position - Vector2.up * speed * Time.deltaTime;
sr.sprite = playerSprites[2];
dir = Direction.Down;
dirIndex = 2;
}
if (Input.GetKey(KeyCode.D)) {
newPosition = position + Vector2.right * speed * Time.deltaTime;
sr.sprite = playerSprites[1];
dir = Direction.Right;
dirIndex = 1;
}
if (Input.GetKey(KeyCode.A)) {
newPosition = position - Vector2.right * speed * Time.deltaTime;
sr.sprite = playerSprites[3];
dir = Direction.Left;
dirIndex = 3;
}
rb.MovePosition(newPosition);
//handle shooting
if (Input.GetKeyDown(KeyCode.Space)) {
combat.CmdFire (GetDirection());
}
// center camera.. only if this is MY player!
Vector3 pos = transform.position;
pos.z = -50;
Camera.main.transform.position = pos;
}
//will be called on clients when var changed on server
private void OnDirIndex (int i) {
dirIndex = i;
switch (dirIndex) {
case 0:
sr.sprite = playerSprites[0];
break;
case 2:
sr.sprite = playerSprites[2];
break;
case 1:
sr.sprite = playerSprites[1];
break;
case 3:
sr.sprite = playerSprites[3];
break;
default:
break;
}
}
}
Why client not changed hi dirIndex on server?
Answer by n1gth · Jul 03, 2015 at 06:45 AM
The SyncVar will only work from server to client. It doesn't go the other way. For that, you could try using a Command function. See here.
So how exactly would that be done? I'm doing something fairly similar, trying to update a state of the player (crouching, sprinting, etc) based on booleans.
For above, trying to change dirIndex
on the client, and then sending that to the server, so it gets updated on all the other clients; what would that look like?
Would it be something simply like:
void Update() {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W) {
CmdSetDirIndex(0);
}
}
[Command]
void CmdSetDirIndex(int index) {
dirIndex = index;
}
If so man I lost a lot of time with messing with SyncVars... :)
Hmm, that didn't seem to work. How can I use a Command function to update a state, and update all the client's states as well? I might spin up a new question.
Well, it should actually work with SyncVars and Command-Functions. Here is a example code with booleans:
[SyncVar]
bool var01 = false; // will be synced on all clients
[SyncVar (hook="OnHook")]
bool var02 = false; // OnHook will be called on clients, when var02 gets altered on the server
[Command]
void Cmd_SetVar01(bool value){
var01 = value;
}
[Command]
void Cmd_SetVar02(bool value){
var02 = value;
}
void OnHook(bool value){
var02 = value;
// other stuff
}
Your answer
Follow this Question
Related Questions
Sync Animations through Network (Mecanim) 0 Answers
HLAPI SyncVars property setter function 1 Answer
Unity networking tutorial? 6 Answers