<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[State Sync Question]]></title><description><![CDATA[<p>Hey there,</p>
<p>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.</p>
<p>Also know the code is just thrown together for learning purposes and to see if i can get something working.</p>
<p>My Server Code:</p>
<pre><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;
        }
    }
}
</code></pre>
<p>Then in the main Room code after a player has joined, i listen for a input message and call the above code with this:</p>
<pre><code>onMessage (client, data) {
    console.log(client.id, &quot;sent&quot;, data)

    if (data.action) {
      if (data.action === 'Input') {
        this.handleMovement(client.sessionId, data.key)
      }
    }
  }

handleMovement (id, key) {
    this.state.players[id].updatePosition(key);
  }
</code></pre>
<p>In my unity client side i have this code.</p>
<pre><code>IEnumerator Start () {
        ...
        room.Listen (&quot;players/:id/:position/:attribute&quot;, this.OnPlayerChange);
        ...
}

void HandlePlayerInput() 
{
        if (Input.GetKeyDown(&quot;a&quot;))
        {
            room.Send(new { action = &quot;Input&quot;, key = &quot;A&quot;});
        }
        if (Input.GetKeyDown(&quot;s&quot;))
        {
            room.Send(new { action = &quot;Input&quot;, key = &quot;S&quot;});
        }
        if (Input.GetKeyDown(&quot;d&quot;))
        {
            room.Send(new { action = &quot;Input&quot;, key = &quot;D&quot;});
        }
        if (Input.GetKeyDown(&quot;w&quot;))
        {
            room.Send(new { action = &quot;Input&quot;, key = &quot;W&quot;});
        }
}

void OnPlayerChange(DataChange change) 
{
        GameObject curPlayer;
        players.TryGetValue(change.path[&quot;id&quot;], out curPlayer);

        if (curPlayer)
        {
            switch (change.path[&quot;attribute&quot;])
            {
                case &quot;x&quot;:
                    curPlayer.transform.position = new Vector3(float.Parse(change.value.ToString()), curPlayer.transform.position.y, curPlayer.transform.position.z);
                    break;
                case &quot;y&quot;:
                    curPlayer.transform.position = new Vector3(curPlayer.transform.position.x, float.Parse(change.value.ToString()), curPlayer.transform.position.z);
                    break;
                case &quot;z&quot;:
                    curPlayer.transform.position = new Vector3(curPlayer.transform.position.x, curPlayer.transform.position.y, float.Parse(change.value.ToString()));
                    break;
                default:
                    break;
            }

        }
}
</code></pre>
<p>All of this works <em>Kinda</em> great. I see the box spawn, i can kinda move it around, but the movement isn't perfect.</p>
<p>When i hit the &quot;a&quot; 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.</p>
<p>When i hit &quot;w&quot; all works as expected. (box moves up the z plane, camera is directly above box so x,z planes are the movements.)</p>
<p>When i hit &quot;d&quot; the box moves both up the x plane and the z plane in a diagonal direction?</p>
<p>And finally when i hit the &quot;s&quot; button nothing happens.</p>
<p>Any ideas as to what i might be doing wrong?</p>
<p>Thanks,<br />
Cody</p>
]]></description><link>http://discuss.colyseus.io/topic/9/state-sync-question</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Apr 2026 20:23:44 GMT</lastBuildDate><atom:link href="http://discuss.colyseus.io/topic/9.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 30 Oct 2017 15:30:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to State Sync Question on Mon, 30 Oct 2017 15:31:56 GMT]]></title><description><![CDATA[<p>Hey there,</p>
<p>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.</p>
<p>Also know the code is just thrown together for learning purposes and to see if i can get something working.</p>
<p>My Server Code:</p>
<pre><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;
        }
    }
}
</code></pre>
<p>Then in the main Room code after a player has joined, i listen for a input message and call the above code with this:</p>
<pre><code>onMessage (client, data) {
    console.log(client.id, &quot;sent&quot;, data)

    if (data.action) {
      if (data.action === 'Input') {
        this.handleMovement(client.sessionId, data.key)
      }
    }
  }

handleMovement (id, key) {
    this.state.players[id].updatePosition(key);
  }
</code></pre>
<p>In my unity client side i have this code.</p>
<pre><code>IEnumerator Start () {
        ...
        room.Listen (&quot;players/:id/:position/:attribute&quot;, this.OnPlayerChange);
        ...
}

void HandlePlayerInput() 
{
        if (Input.GetKeyDown(&quot;a&quot;))
        {
            room.Send(new { action = &quot;Input&quot;, key = &quot;A&quot;});
        }
        if (Input.GetKeyDown(&quot;s&quot;))
        {
            room.Send(new { action = &quot;Input&quot;, key = &quot;S&quot;});
        }
        if (Input.GetKeyDown(&quot;d&quot;))
        {
            room.Send(new { action = &quot;Input&quot;, key = &quot;D&quot;});
        }
        if (Input.GetKeyDown(&quot;w&quot;))
        {
            room.Send(new { action = &quot;Input&quot;, key = &quot;W&quot;});
        }
}

void OnPlayerChange(DataChange change) 
{
        GameObject curPlayer;
        players.TryGetValue(change.path[&quot;id&quot;], out curPlayer);

        if (curPlayer)
        {
            switch (change.path[&quot;attribute&quot;])
            {
                case &quot;x&quot;:
                    curPlayer.transform.position = new Vector3(float.Parse(change.value.ToString()), curPlayer.transform.position.y, curPlayer.transform.position.z);
                    break;
                case &quot;y&quot;:
                    curPlayer.transform.position = new Vector3(curPlayer.transform.position.x, float.Parse(change.value.ToString()), curPlayer.transform.position.z);
                    break;
                case &quot;z&quot;:
                    curPlayer.transform.position = new Vector3(curPlayer.transform.position.x, curPlayer.transform.position.y, float.Parse(change.value.ToString()));
                    break;
                default:
                    break;
            }

        }
}
</code></pre>
<p>All of this works <em>Kinda</em> great. I see the box spawn, i can kinda move it around, but the movement isn't perfect.</p>
<p>When i hit the &quot;a&quot; 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.</p>
<p>When i hit &quot;w&quot; all works as expected. (box moves up the z plane, camera is directly above box so x,z planes are the movements.)</p>
<p>When i hit &quot;d&quot; the box moves both up the x plane and the z plane in a diagonal direction?</p>
<p>And finally when i hit the &quot;s&quot; button nothing happens.</p>
<p>Any ideas as to what i might be doing wrong?</p>
<p>Thanks,<br />
Cody</p>
]]></description><link>http://discuss.colyseus.io/post/10</link><guid isPermaLink="true">http://discuss.colyseus.io/post/10</guid><dc:creator><![CDATA[codrobin33]]></dc:creator><pubDate>Mon, 30 Oct 2017 15:31:56 GMT</pubDate></item><item><title><![CDATA[Reply to State Sync Question on Mon, 30 Oct 2017 15:48:05 GMT]]></title><description><![CDATA[<p>Nevermind.......</p>
<p>As soon as i hit submit i see i dont have <code>break</code> in my JS switch code on the server...</p>
<p>/facepalm.</p>
<p>Loving the library so far, great work!</p>
]]></description><link>http://discuss.colyseus.io/post/11</link><guid isPermaLink="true">http://discuss.colyseus.io/post/11</guid><dc:creator><![CDATA[codrobin33]]></dc:creator><pubDate>Mon, 30 Oct 2017 15:48:05 GMT</pubDate></item></channel></rss>