New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make an option to have SyncVar hooks called on Server #2230
Comments
This would be cool. |
Per the docs, hooks only fire on clients. If you need the server to do something when setting a syncvar do it right when you set it. |
people requested this a lot over the years. this also makes SyncField easier, where SyncField wouldn't have to depend on NetworkClient. let's leave this open for discussion. |
I think this is the way to go. For example: If anyone is interested, this is my current workaround:
|
This seems simpler... public UnityAction<int, int> OnValueChangedEvent;
[SyncVar(hook = nameof(OnValueChanged))]
int _value;
public int value
{
get
{
return _value;
}
set
{
OnValueChangedEvent?.Invoke(_value, value);
_value = value;
}
}
// SyncVar hook and event handler
void OnValueChanged(int oldValue, int newValue)
{
if (isServer) { /* do server stuff */ }
if (isClientOnly) { /* do client stuff */ }
}
public override void OnStartServer()
{
OnValueChangedEvent += OnValueChanged;
} |
This seems simpler-er...
|
Also, please take a look at this #2421 |
This should be a relatively easy change, mostly just needs someone who knows a bit of weaver stuff. This is how we did it in mirage: MirageNet/Mirage#1012 This should be the main part Getting an event to trigger instead of methods was a bit harder because it needs to have the null check on the event field. MirageNet/Mirage#991 |
That's it! That hook event also looks amazing. This enables a data driven architecture to be done much more easily. |
Still thinking this would be amazing :) |
This happens now, sort of. If the component has SyncDirection set to "Client To Server" the owner client can set the SyncVar directly instead of via Cmd, which sends it to server and other clients. Hook fires on server and the other clients, but not the owner (for now). |
This sounds like the exact same |
Please explain the suggested feature in detail.
[SyncVar(hook = nameof(HookFn))]
HookFn is never called on Server only mode so I have to either separate server and client logic or call the function manually, which will make me write more code and make the code less readable.
However, if there were an option to have SyncVar Hooks called on the server, it would speed up the development and make everything slightly more readable.
[SyncVar(hook = nameof(HookFn), callHookOnServer = true)]
How exactly does this keep you from releasing your game right now?
I made everything dependent on SyncVar Hooks on the server not knowing it would not work on server only mode. I have to rewrite most of the logic or call the function manually to get it to work now, which is extra work.
Can you suggest a possible solution?
Add something like callHookOnServer to SyncVar Hooks so we can have some hooks called on the server.
The text was updated successfully, but these errors were encountered: