46 lines
795 B
C#
Raw Normal View History

2021-11-26 19:33:56 +00:00
using QuantumUNET.Transport;
2021-11-26 22:31:52 +00:00
using System;
2021-11-26 19:33:56 +00:00
namespace QSB.Utility.VariableSync
{
public class BoolVariableSyncer : BaseVariableSyncer
{
2021-12-01 09:48:11 +00:00
public VariableReference<bool> ValueToSync { get; private set; }
2021-11-26 19:33:56 +00:00
2021-11-26 22:31:52 +00:00
public void Init(Func<bool> getter, Action<bool> setter)
2021-11-26 19:33:56 +00:00
{
2021-12-01 09:48:11 +00:00
ValueToSync = new(this);
2021-11-26 22:31:52 +00:00
ValueToSync.Getter = getter;
ValueToSync.Setter = setter;
_ready = true;
2021-11-26 19:33:56 +00:00
}
2021-11-26 22:31:52 +00:00
public void OnDestroy()
=> _ready = false;
2021-11-26 19:33:56 +00:00
2021-11-26 22:31:52 +00:00
public override void WriteData(QNetworkWriter writer)
2021-12-01 09:48:11 +00:00
{
if (Ready)
{
writer.Write(ValueToSync.Value);
}
else
{
writer.Write(default(bool));
}
}
2021-11-26 22:31:52 +00:00
2021-12-01 09:48:11 +00:00
public override void ReadData(QNetworkReader reader)
{
if (Ready)
{
ValueToSync.Value = reader.ReadBoolean();
}
else
{
reader.ReadBoolean();
}
}
2021-11-26 19:33:56 +00:00
}
}