- Home /
HLAPI SyncVars property setter function
Hello,
I was reading through the HLAPI manual section on Unity's website and got to this section about State Synchronization
Note that setting a SyncVar member variable inside a property setter function does not cause it to be dirtied. Trying to do this will cause a compile time warning. Because SyncVars use properties internally to mark themselves as dirty, setting them dirty inside property functions could lead to recursion problems.
Scenario: Lets say a server-side script wants to change the health of an object from 100 to 300. What is the proper way to do this? I am using a getter/setter and having this compiler warning.
UNetWeaver warning: SyncVar [curHealth] set from within property function [System.Void Health::set_CurrentHp(System.Int32)]. This will not set dirty flags.
public int CurrentHp{
get { return curHealth; }
set { curHealth = value;}
}
Should I be using getters/setters for variables in the HLAPI or leaving all variables public? I'm not setting the dirty flag in the 'setter' portion because the mentioned recursion problems above.
Are getter/setter methods even the correct thing to do with a game that is networked? ClientRpcs and Commands don't seem right to use for this.
Answer by seanr · Jul 05, 2015 at 08:06 PM
No. Dont use setters for SyncVars, they must be regular member variables.
Your answer
Follow this Question
Related Questions
problems with setters and getters. 1 Answer
State synchronization non-functional after mode change? 0 Answers
getter and setter not working 1 Answer
Networking: How to get server values from clients to keep a chronometer in sync 0 Answers
Accessing class properties through getters & setters in editor mode 1 Answer