<?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[Best way to represent inheritance and optionals together in Colyseus]]></title><description><![CDATA[<p>Hi all,</p>
<p>First, some code to explain what I want:</p>
<pre><code>class Character extends Schema {
    @type([ Item ])
    items: new ArraySchema&lt;Item&gt;();;
}
</code></pre>
<p>Imagine we have this character class. I want a character to be able to hold 0+ <code>Item</code>s. An item would ideally be a superclass, with some subclasses, like this:</p>
<pre><code>class Item extends Schema {
}

class Bucket extends Item {
}

class Hose extends Item {
}
</code></pre>
<p>What is the recommended way to do this? The problem with this approach is I would need some kind of client side check to determine which kind of item I'm holding. I'm also not sure this is possible at all, since the docs say &quot;You can't mix types inside arrays.&quot; (<a href="https://docs.colyseus.io/state/schema/#arrayschema" rel="nofollow">here</a>). If this was Rust for example, I'd probably have a bunch of items that impl an Item trait.</p>
<p>Another thing I would reach for in another language is enums. In Rust (again) for example you can have enums that have different values for each enum variant. This would be good for this case, as well as for representing optionals.</p>
<p>How have you managed to do this kind of thing? Would you just have n properties, one for each type of item?</p>
<p>As a related point, imagine also I want it to be possible for it to just be 0 or 1. I see there are no optionals, how would I do this? Do you just use an ArraySchema for this?</p>
<p>Thanks!</p>
]]></description><link>http://discuss.colyseus.io/topic/462/best-way-to-represent-inheritance-and-optionals-together-in-colyseus</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Jul 2026 16:05:05 GMT</lastBuildDate><atom:link href="http://discuss.colyseus.io/topic/462.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 01 May 2021 18:31:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Best way to represent inheritance and optionals together in Colyseus on Sat, 01 May 2021 18:36:48 GMT]]></title><description><![CDATA[<p>Hi all,</p>
<p>First, some code to explain what I want:</p>
<pre><code>class Character extends Schema {
    @type([ Item ])
    items: new ArraySchema&lt;Item&gt;();;
}
</code></pre>
<p>Imagine we have this character class. I want a character to be able to hold 0+ <code>Item</code>s. An item would ideally be a superclass, with some subclasses, like this:</p>
<pre><code>class Item extends Schema {
}

class Bucket extends Item {
}

class Hose extends Item {
}
</code></pre>
<p>What is the recommended way to do this? The problem with this approach is I would need some kind of client side check to determine which kind of item I'm holding. I'm also not sure this is possible at all, since the docs say &quot;You can't mix types inside arrays.&quot; (<a href="https://docs.colyseus.io/state/schema/#arrayschema" rel="nofollow">here</a>). If this was Rust for example, I'd probably have a bunch of items that impl an Item trait.</p>
<p>Another thing I would reach for in another language is enums. In Rust (again) for example you can have enums that have different values for each enum variant. This would be good for this case, as well as for representing optionals.</p>
<p>How have you managed to do this kind of thing? Would you just have n properties, one for each type of item?</p>
<p>As a related point, imagine also I want it to be possible for it to just be 0 or 1. I see there are no optionals, how would I do this? Do you just use an ArraySchema for this?</p>
<p>Thanks!</p>
]]></description><link>http://discuss.colyseus.io/post/1489</link><guid isPermaLink="true">http://discuss.colyseus.io/post/1489</guid><dc:creator><![CDATA[banool]]></dc:creator><pubDate>Sat, 01 May 2021 18:36:48 GMT</pubDate></item><item><title><![CDATA[Reply to Best way to represent inheritance and optionals together in Colyseus on Invalid Date]]></title><description><![CDATA[<p>Hi <a class="plugin-mentions-user plugin-mentions-a" href="http://discuss.colyseus.io/uid/792">@banool</a>, sorry for the delayed response!</p>
<p>Inheritance is supported on <code>ArraySchema</code> and <code>MapSchema</code>. So the approach of having an <code>Item</code> as base class works! (I'm using a <code>MapSchema</code> for this, to be able to add/remove directly by key rather than a moving index, <a href="https://github.com/endel/mazmorra/blob/b6fef8bd884261b948e4a8c816a0fac93fc543c7/server/core/Inventory.ts#L11-L12" rel="nofollow">see mazmorra.io sources</a>)</p>
<p>In order to be able to differentiate the instances on the client-side - if you are using JavaScript/TypeScript, you can share the actual <code>Schema</code> implementation with the client, and provide its concrete implementation as a third parameter for the matchmaking method:</p>
<pre><code>// this is the same RoomState as you have in the server
class RoomState extends Schema {}

const room = await client.joinOrCreate(&quot;my_room&quot;, {}, RoomState);
</code></pre>
<p>This way, the actual instances of <code>Bucket</code>, <code>Hose</code>, and whatnot are going to be created during decoding time.</p>
<p>Hope this helps, let me know if you have any questions!</p>
<p>Cheers,<br />
Endel</p>
]]></description><link>http://discuss.colyseus.io/post/1493</link><guid isPermaLink="true">http://discuss.colyseus.io/post/1493</guid><dc:creator><![CDATA[endel]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Best way to represent inheritance and optionals together in Colyseus on Invalid Date]]></title><description><![CDATA[<p>Hi <a class="plugin-mentions-user plugin-mentions-a" href="http://discuss.colyseus.io/uid/1">@endel</a>,</p>
<p>Thanks that's great to hear! My client side is in lua (Defold) but I assume there is something I can check at runtime to differentiate them, I'll just take a look at the messages I receive.</p>
<p>Thanks!</p>
]]></description><link>http://discuss.colyseus.io/post/1496</link><guid isPermaLink="true">http://discuss.colyseus.io/post/1496</guid><dc:creator><![CDATA[banool]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>