#Best way to go about SyncVar Hooks in host mode with sync direction "Client To Server"?

8 messages · Page 1 of 1 (latest)

jolly tendon
#

If a host client sets a SyncVar field then it calls the hook.
But if a pure client sets the SyncVar field then it doesn't call the hook.
For exemple this code below, if a host client crouches then everyone would run the hook.
But if pure client crouches then everyone except the client itself runs the hook. (Which is correct behavior)

bool _isCrouching;

void SetCrouchingState(bool oldValue, bool isCrouching)
{
  SetCrouchVisuals(isCrouching);
}


void Update
{
  if(!base.isLocalPlayer) return;
  if(wantToCrouch)
  {
    _isCrouching = true;
  }
}```
While this is easy fixable by just doing this.

void Update
{
if(!base.isLocalPlayer) return;
if(wantToCrouch)
{
_isCrouching = true
if (!base.isServer)
SetCrouchVisuals(isCrouching);
}
}```
But wondering if there is a better way of going about this? To try to avoid code that treats a pure client and host client differently. Or maybe at least a way to handle that inside the hook function instead. Like a way of checking if the hook got called server side or client side.

I appreciate your input.

rain bobcat
#

uh im not sure its needed, unless we have mirror behaving unexpectedly.
If script is set to client->server in inspector, you should be fine with your first code snippet.

#

Make sure the script direction is set, not network transform or another script.

#

By default its server-Client, meaning players have to send cmd's to request sync var change.

#

@jolly tendon

jolly tendon
#

@rain bobcat I did a minimal example here. https://pastebin.com/U46PYtYM
I'm 100% sure that sync direction is "Client To Server". And it acts the same irregardless if I run it in the editor or as a build.

Host Client changes field:
Calls hook on self(Is this intended?)
Calls hooks for others (Works as intended)

Pure Client changes field:
Does not call hook on self (Works as intended)
Calls hooks for others (Works as intended)

neat viper
jolly tendon
# neat viper With Client To Server SyncVars are set on client and sent to server and that's i...

Unless I'm missing something that doesn't seem to be the case. Take this example https://pastebin.com/ntUVe0tk

I start 3 instances, 1 host (server + client), and 2 clients (A and B).
Client A presses Y: SetMyField() gets called on Host and Client B
Client B presses Y: SetmyField() gets called on Host and Client A
Host presses Y: Setmyfield() gets called on Host, Client A, and Client B

Mirror version: 86.12.2 But I don't see anything in the lastest version that should affect this