<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Trym Nilsen</title>
    <description>The latest articles on DEV Community by Trym Nilsen (@trymnilsen).</description>
    <link>https://dev.to/trymnilsen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1062421%2Fa4c0b6f8-f874-4913-88e2-5de1e8d12322.jpeg</url>
      <title>DEV Community: Trym Nilsen</title>
      <link>https://dev.to/trymnilsen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/trymnilsen"/>
    <language>en</language>
    <item>
      <title>Building a tiny type-safe typescript ECS (Entity-component-system)</title>
      <dc:creator>Trym Nilsen</dc:creator>
      <pubDate>Sun, 17 Nov 2024 15:52:39 +0000</pubDate>
      <link>https://dev.to/trymnilsen/building-a-tiny-type-safe-typescript-ecs-entity-component-system-dil</link>
      <guid>https://dev.to/trymnilsen/building-a-tiny-type-safe-typescript-ecs-entity-component-system-dil</guid>
      <description>&lt;p&gt;I have been down a "rewatching old game developer conference videos" sized rabbit hole lately and recently I &lt;a href="https://xkcd.com/356/" rel="noopener noreferrer"&gt;Nerd sniped&lt;/a&gt; myself thinking &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Could I make a small ECS in typescript to play around with?. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In my spare time I work on a medieval city builder named &lt;a href="https://github.com/trymnilsen/kingdomarchitect" rel="noopener noreferrer"&gt;Kingdom Architect&lt;/a&gt; and I have chosen to go for a Gameobjects-Component like architecture. This has worked fine, but I have also found it challenging to handle interactions between components and system level behaviour. Is it wise to completely change the architecture if I want to ever ship my project, probably not... Is it fun, heck yes!&lt;/p&gt;

&lt;p&gt;Inspired by Maxwell Forbes &lt;a href="https://maxwellforbes.com/posts/typescript-ecs-implementation/" rel="noopener noreferrer"&gt;blog post&lt;/a&gt; I set out on my adventures. Some of the things I wanted to improve and have in my own little playground was a strict query and access functionality. You only got what you asked for, but what you asked for would be there fully typed. I also wanted to avoid defining the set of components for a system multiple times or in multiple places.&lt;/p&gt;

&lt;p&gt;What I ended up with was an API that looked like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CounterComponent extends EcsComponent {
    currentValue: number = 0;
}

const counterSystem = new EcsSystem({
    counter: CounterComponent,
});

counterSystem.withUpdate(({counter}) =&amp;gt; {
    counter[0].currentValue += 42;
    console.log("Amount during update", counter[0].currentValue);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Based on the object provided in the constructor of a new system, I both know which components my system is interested in and I have a data structure and typing info for when I provide these components in the update loop.&lt;/p&gt;

&lt;p&gt;Turns out the magic sauce was in the &lt;code&gt;InstanceType&lt;/code&gt; utility type. With this I could take my "query object" with class prototypes and convert it to a an object that I could use as the argument for the update function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export type QueryData&amp;lt;T extends QueryObject = QueryObject&amp;gt; = {
    [P in keyof T]: InstanceType&amp;lt;T[P]&amp;gt;[];
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case you are wondering what the QueryObject is, it looks like this and is used in the System class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export type ComponentFn&amp;lt;T extends EcsComponent = EcsComponent&amp;gt; = new (
    ...args: any[]
) =&amp;gt; T;

export interface QueryObject&amp;lt;T extends ComponentFn = ComponentFn&amp;gt; {
    [componentName: string]: T;
}

export class EcsSystem&amp;lt;T extends QueryObject = QueryObject&amp;gt; {
    private onUpdate: UpdateFunction&amp;lt;T&amp;gt; | null = null;

    constructor(public query: Readonly&amp;lt;T&amp;gt;) {}

    runUpdate(components: QueryData&amp;lt;T&amp;gt;, gameTime: number) {
        if (this.onUpdate) {
            this.onUpdate(components, gameTime);
        }
    }

    withUpdate(updateFunction: UpdateFunction&amp;lt;T&amp;gt;): void {
        this.onUpdate = updateFunction;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step in making my ECS playground is hooking up my system with a mapping between entities (that I plan on letting be a number, like in Maxwell's system) and running the update loop/querying for components.&lt;/p&gt;

&lt;p&gt;If you want the full source, all 47 lines of it, I have made a gist &lt;a href="https://gist.github.com/trymnilsen/7cb029de15b51602cee5576bdc3c7d47" rel="noopener noreferrer"&gt;Here&lt;/a&gt;      &lt;/p&gt;

</description>
      <category>typescript</category>
      <category>gamedev</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
