Can do.
Below is the code if I move a unit:
public void Move(Tile targetTile)
{
Vector3 unitPos = transform.position;
Vector3 targetPos = targetTile.transform.position;
long opCode = (long) MultiplayerManager.Code.MoveUnit;
string state = GetJsonPositionAndAbilityInd(unitPos, targetPos,0);
}
public string GetJsonPositionAndAbilityInd (Vector3 selectedUnit, Vector3 targetTile, int abilityIndex)
{
var values = new Dictionary<string, string>
{
{"SelectedUnitPos.x", selectedUnit.x.ToString() },
{"SelectedUnitPos.y", selectedUnit.y.ToString() },
{"SelectedUnitPos.z", selectedUnit.z.ToString() },
{"TargetTilePos.x", targetTile.x.ToString() },
{"TargetTilePos.y", targetTile.y.ToString() },
{"TargetTilePos.z", targetTile.z.ToString() },
{"AbilityIndex", abilityIndex.ToString()}
};
return values.ToJson();
}
This is the first part of the client code when receiving the match state:
private void OnReceivedMatchState(IMatchState matchState = null)
{
Debug.Log("Received OpCode: "+matchState.OpCode);
string state = Encoding.UTF8.GetString(matchState.State);
Debug.Log("Received State: "+state);
In the client console I get:
Received OpCode: 1
Received State: “{"SelectedUnitPos.x":"-3.75","SelectedUnitPos.y":"0.1","SelectedUnitPos.z":"6.4875","TargetTilePos.x":"-3.75","TargetTilePos.y":"0.1","TargetTilePos.z":"0","AbilityIndex":"0"}”
This is the client code that is meant to perform the unit movement action when receiving this match state:
if (matchState.OpCode == (long)Code.MoveUnit)
{
var stateDictionary = GetStateDictionary(matchState.State);
Vector3 unitPos = new Vector3(float.Parse(stateDictionary["SelectedUnitPos.x"]), float.Parse(stateDictionary["SelectedUnitPos.y"]), float.Parse(stateDictionary["SelectedUnitPos.z"]));
Vector3 targetPos = new Vector3(float.Parse(stateDictionary["TargetTilePos.x"]), float.Parse(stateDictionary["TargetTilePos.y"]), float.Parse(stateDictionary["TargetTilePos.z"]));
UnityMainThreadDispatcher.Instance().AddActionToQueue(MoveUnit(unitPos, targetPos));
}
private IDictionary<string,string> GetStateDictionary(byte[] state)
{
return Encoding.UTF8.GetString(state).FromJson<Dictionary<string, string>>();
}
The error I get in the client console is:
System.NullReferenceException: Object reference not set to an instance of an object
at Nakama.Helpers.MultiplayerManager.OnReceivedMatchState (Nakama.IMatchState matchState) [0x00089] in C:\Unity Projects\Projects\Valorous\Assets\Nakama\Scripts\MultiplayerManager.cs:253
at Nakama.Socket.ProcessMessage (System.ArraySegment1[T] buffer) [0x001ed] in <127981172e4e4a2b9b7ef01979f29de9>:0 UnityEngine.Debug:LogError (object) Nakama.Socket:ProcessMessage (System.ArraySegment
1)
Note: this is what I used when I had it setup to be client to client. But seems to not work with server authorative. Unless I am just completely missing something here!
Thanks!