SyncedProperty - maceeikodev/WreckMP_Ref GitHub Wiki
Definition
public abstract class SyncedProperty<T>
Base class for synced properties. This class is abstract. For syncing custom types inherit this class to implement your own synced property. Can be used in singleplayer without issues.
Fields
public bool sendOverTCP;
- Whether to send the event primarily over TCP. Default is false. Set to true if you only send the update once in a while and isn't very time sensitive.
protected T _value;
- Backing field for the Value property. Use this in child class to change the value without firing update event.
Properties
public string UniqueName { get; }
- The unique name of the game event used to send updates.
public T Value { get; set; }
- The value of the synced property.
public float UpdateCooldown { get; set; }
- Cooldown for sending update packets in seconds. Default is 0.
Events
public event Action ReceivedUpdate;
- Fired when received an update over the network and the value changes.
Constructor
protected SyncedProperty(string uid, bool registerTimer);
Create an instance.
uid
- The unique name of the game event used to send updates.
registerTimer
- Set to true if you're going to use UpdateCooldown, otherwise keep on false to improve performance. Only use UpdateCooldown if you change the value often.
Methods
public void SendUpdate(ulong target = 0, bool? safe = null, bool initial = false);
Force send update event.
target
- Target user. 0 sends to everyone
safe
- If null, sendOverTCP field is used
initial
- True if initial sync. This argument is only passed to the Write method
protected abstract bool IsChanged(T oldVal, T newVal);
This function is called when the property Value is set to see whether the change should be sent to others.
oldVal
- Old value
newVal
- New value
Returns: If true, the event is sent.
protected abstract void Write(WreckAPI.GameEventWriter p, bool initial);
Serialize the update data.
p
- The writer to write to.
initial
- Whether this is the initial sync (passed from SendUpdate)
protected abstract void Read(WreckAPI.GameEventReader p);
Deserialize the update data. WARNING!!! When writing to Value, write to _value instead to prevent another event being sent!
p
- The reader to read from.