@endel I was looking at this in a tired state, but im pretty sure i had fully updated client and server. Ill double check what i was seeing and if i still have an error I'll open an issue on the github. Thanks for the response.
Posts made by codrobin33
@endel hey sir im still struggling with initial sync, here is my setup.
Error i'm receiving:
Colyseus.Room.ParseMessage (System.Byte[] recv) (at Assets/Plugins/Colyseus/Room.cs:140)
Colyseus.Room.Recv () (at Assets/Plugins/Colyseus/Room.cs:73)
Colyseus.Client.Recv () (at Assets/Plugins/Colyseus/Client.cs:90)
RoomHandler+<Start>c__Iterator0.MoveNext () (at Assets/RoomHandler.cs:37)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
Here is my data from server:
Code section its dying on (Room.cs) in plugins:
} else if (code == Protocol.ROOM_STATE) {
byte[] encodedState = (byte[]) message [2];
// TODO:
// https://github.com/deniszykov/msgpack-unity3d/issues/8
// var remoteCurrentTime = (double) message [3];
// var remoteElapsedTime = (int) message [4];
// this.SetState (state, remoteCurrentTime, remoteElapsedTime);
this.SetState (encodedState, 0, 0);
}
And finally, my start function
IEnumerator Start ()
{
client = new Colyseus.Client ("ws://localhost:2657");
client.OnOpen += OnOpenHandler;
client.OnClose += (object sender, EventArgs e) => room.Leave();
yield return StartCoroutine(client.Connect());
room = client.Join("game");
room.OnReadyToConnect += (sender, e) => StartCoroutine ( room.Connect() );
room.Listen ("messages/:number", this.OnMessageAdded);
room.Listen ("players/:id", this.OnPlayerAdded);
room.Listen ("shared/:map/:bases/:id", this.OnBaseAdded);
room.OnJoin += OnRoomJoined;
room.OnUpdate += OnUpdateHandler;
room.OnData += (object sender, MessageEventArgs e) => Debug.Log(e.data);
while (true)
{
client.Recv();
// string reply = client.RecvString();
if (client.error != null)
{
Debug.LogError ("Error: " + client.error);
break;
}
yield return 0;
}
OnApplicationQuit();
}
Have any initial thoughts?
@endel what a quick turnaround! thank you!
I will try out my project and see if this fixes what i was struggling with.
@endel Thoughts on how you would implement this in Unity?
Also do you have a good example of a first state sync of a complex state structure in Unity?
Nevermind.......
As soon as i hit submit i see i dont have break
in my JS switch code on the server...
/facepalm.
Loving the library so far, great work!
Hey there,
This might just be a lack of me understanding so I apologize in advance if so, but i seem to be having some trouble understanding the state sync.
Also know the code is just thrown together for learning purposes and to see if i can get something working.
My Server Code:
export class Player {
id: string;
position: {
x: number,
y: number,
z: number
} = {
x: 0,
y: 0,
z: 0
};
updatePosition(key) {
switch(key) {
case 'A':
this.position.x -= 0.1;
case 'S':
this.position.z -= 0.1;
case 'D':
this.position.x += 0.1;
case 'W':
this.position.z += 0.1;
default:
break;
}
}
}
Then in the main Room code after a player has joined, i listen for a input message and call the above code with this:
onMessage (client, data) {
console.log(client.id, "sent", data)
if (data.action) {
if (data.action === 'Input') {
this.handleMovement(client.sessionId, data.key)
}
}
}
handleMovement (id, key) {
this.state.players[id].updatePosition(key);
}
In my unity client side i have this code.
IEnumerator Start () {
...
room.Listen ("players/:id/:position/:attribute", this.OnPlayerChange);
...
}
void HandlePlayerInput()
{
if (Input.GetKeyDown("a"))
{
room.Send(new { action = "Input", key = "A"});
}
if (Input.GetKeyDown("s"))
{
room.Send(new { action = "Input", key = "S"});
}
if (Input.GetKeyDown("d"))
{
room.Send(new { action = "Input", key = "D"});
}
if (Input.GetKeyDown("w"))
{
room.Send(new { action = "Input", key = "W"});
}
}
void OnPlayerChange(DataChange change)
{
GameObject curPlayer;
players.TryGetValue(change.path["id"], out curPlayer);
if (curPlayer)
{
switch (change.path["attribute"])
{
case "x":
curPlayer.transform.position = new Vector3(float.Parse(change.value.ToString()), curPlayer.transform.position.y, curPlayer.transform.position.z);
break;
case "y":
curPlayer.transform.position = new Vector3(curPlayer.transform.position.x, float.Parse(change.value.ToString()), curPlayer.transform.position.z);
break;
case "z":
curPlayer.transform.position = new Vector3(curPlayer.transform.position.x, curPlayer.transform.position.y, float.Parse(change.value.ToString()));
break;
default:
break;
}
}
}
All of this works Kinda great. I see the box spawn, i can kinda move it around, but the movement isn't perfect.
When i hit the "a" key nothing happens. I logged once on the server and it is setting my players x position to -0.1, but the message never seems to come to unity.
When i hit "w" all works as expected. (box moves up the z plane, camera is directly above box so x,z planes are the movements.)
When i hit "d" the box moves both up the x plane and the z plane in a diagonal direction?
And finally when i hit the "s" button nothing happens.
Any ideas as to what i might be doing wrong?
Thanks,
Cody