<?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[[Unity] KeyNotFoundException]]></title><description><![CDATA[<p>I get an empty list when processing the &quot;Add&quot; operation. The Unity example shows how the X and Y value is received though. This is what my code looks like. What could I be missing?</p>
<p>client.</p>
<pre><code>room.Listen(&quot;players/:id&quot;, OnPlayerChange);

private void OnPlayerChange(DataChange change)
{
	Debug.Log($&quot;OnPlayerChange: {change.operation} &gt; {change.path[&quot;id&quot;]} &gt; {change.value}&quot;);

	if (change.operation == &quot;add&quot;)
	{
		// extract data
		IndexedDictionary&lt;string, object&gt; value = (IndexedDictionary&lt;string, object&gt;)change.value;

		// this prints (0) - no values in the dictionary
		Debug.LogWarning($&quot;There are {value.Count} entries&quot;);

		string id = change.path[&quot;id&quot;];
		Vector2 pos = new Vector3(Convert.ToSingle(value[&quot;x&quot;]), Convert.ToSingle(value[&quot;y&quot;]));
		int hp = Convert.ToInt32(value[&quot;hp&quot;]);

		// create and add to list of players
		gameController.CreateActor(id, pos, hp);
	}

	else if (change.operation == &quot;remove&quot;)
	{
		// remove player
		gameController.RemoveActor(change.path[&quot;id&quot;]);
	}
}
</code></pre>
<p>server</p>
<pre><code>import { Client, Room, nosync } from &quot;colyseus&quot;;

export class Player
{
    x: number;
    y: number;
    hp: number;

    @nosync client: Client;
    @nosync room: Room;
    
    constructor(client: Client, room: Room) 
    {
      this.client = client;
      this.room = room;
    }    
}

import { EntityMap, Client, Room } from &quot;colyseus&quot;;
import { Player } from &quot;./Player&quot;;

export class GameState
{
    players: EntityMap&lt;Player&gt; = {};
   
    addPlayer(client: Client, room: Room)
    {
        this.players[client.sessionId] = new Player(client, room);
    }
    
    removePlayer(client: Client)
    {
        delete this.players[client.sessionId];
    }
    
    performAction(client: Client, action: string)
    {
        if (action === &quot;left&quot;) 
        {
            this.players[client.sessionId].x -= 1;
        } 
        else if (action === &quot;right&quot;) 
        {
            this.players[client.sessionId].x += 1;
        }
    }    
}
</code></pre>
]]></description><link>http://discuss.colyseus.io/topic/93/unity-keynotfoundexception</link><generator>RSS for Node</generator><lastBuildDate>Sat, 14 Mar 2026 14:53:43 GMT</lastBuildDate><atom:link href="http://discuss.colyseus.io/topic/93.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 08 Jun 2018 10:20:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Unity] KeyNotFoundException on Fri, 08 Jun 2018 10:21:08 GMT]]></title><description><![CDATA[<p>I get an empty list when processing the &quot;Add&quot; operation. The Unity example shows how the X and Y value is received though. This is what my code looks like. What could I be missing?</p>
<p>client.</p>
<pre><code>room.Listen(&quot;players/:id&quot;, OnPlayerChange);

private void OnPlayerChange(DataChange change)
{
	Debug.Log($&quot;OnPlayerChange: {change.operation} &gt; {change.path[&quot;id&quot;]} &gt; {change.value}&quot;);

	if (change.operation == &quot;add&quot;)
	{
		// extract data
		IndexedDictionary&lt;string, object&gt; value = (IndexedDictionary&lt;string, object&gt;)change.value;

		// this prints (0) - no values in the dictionary
		Debug.LogWarning($&quot;There are {value.Count} entries&quot;);

		string id = change.path[&quot;id&quot;];
		Vector2 pos = new Vector3(Convert.ToSingle(value[&quot;x&quot;]), Convert.ToSingle(value[&quot;y&quot;]));
		int hp = Convert.ToInt32(value[&quot;hp&quot;]);

		// create and add to list of players
		gameController.CreateActor(id, pos, hp);
	}

	else if (change.operation == &quot;remove&quot;)
	{
		// remove player
		gameController.RemoveActor(change.path[&quot;id&quot;]);
	}
}
</code></pre>
<p>server</p>
<pre><code>import { Client, Room, nosync } from &quot;colyseus&quot;;

export class Player
{
    x: number;
    y: number;
    hp: number;

    @nosync client: Client;
    @nosync room: Room;
    
    constructor(client: Client, room: Room) 
    {
      this.client = client;
      this.room = room;
    }    
}

import { EntityMap, Client, Room } from &quot;colyseus&quot;;
import { Player } from &quot;./Player&quot;;

export class GameState
{
    players: EntityMap&lt;Player&gt; = {};
   
    addPlayer(client: Client, room: Room)
    {
        this.players[client.sessionId] = new Player(client, room);
    }
    
    removePlayer(client: Client)
    {
        delete this.players[client.sessionId];
    }
    
    performAction(client: Client, action: string)
    {
        if (action === &quot;left&quot;) 
        {
            this.players[client.sessionId].x -= 1;
        } 
        else if (action === &quot;right&quot;) 
        {
            this.players[client.sessionId].x += 1;
        }
    }    
}
</code></pre>
]]></description><link>http://discuss.colyseus.io/post/298</link><guid isPermaLink="true">http://discuss.colyseus.io/post/298</guid><dc:creator><![CDATA[plyoung]]></dc:creator><pubDate>Fri, 08 Jun 2018 10:21:08 GMT</pubDate></item><item><title><![CDATA[Reply to [Unity] KeyNotFoundException on Invalid Date]]></title><description><![CDATA[<p>Turns out I just needed to init the variables.</p>
<pre><code>export class Player
{
    x: number = 0;
    y: number = 0;
    hp: number = 0;
</code></pre>
]]></description><link>http://discuss.colyseus.io/post/299</link><guid isPermaLink="true">http://discuss.colyseus.io/post/299</guid><dc:creator><![CDATA[plyoung]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>