<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Adam Ramberg on Medium]]></title>
        <description><![CDATA[Stories by Adam Ramberg on Medium]]></description>
        <link>https://medium.com/@adamramberg?source=rss-8834103955f4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*o8faNSTP-iRktkUwpCwgyg.jpeg</url>
            <title>Stories by Adam Ramberg on Medium</title>
            <link>https://medium.com/@adamramberg?source=rss-8834103955f4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 23 May 2026 06:34:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adamramberg/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Unity Atoms v4 is out!]]></title>
            <link>https://medium.com/@adamramberg/unity-atoms-v4-is-out-b15a37da49da?source=rss-8834103955f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/b15a37da49da</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Adam Ramberg]]></dc:creator>
            <pubDate>Sun, 22 Mar 2020 22:45:42 GMT</pubDate>
            <atom:updated>2020-03-22T22:45:42.001Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UBGiYo_-lc6YXphWpRDMBg.png" /><figcaption>Check out <a href="https://github.com/AdamRamberg/unity-atoms">Unity Atoms on GitHub</a>.</figcaption></figure><p>Before starting my next Unity project there were a couple of new features I wanted to add to my open source library Unity Atoms — a library that utilizes <a href="https://docs.unity3d.com/Manual/class-ScriptableObject.html">ScriptableObjects</a> to make your code more modular, testable and easier to work with and make changes to. If you are not familiar with Unity Atoms you should before continuing read my previous posts about it:</p><ul><li><a href="https://medium.com/@adamramberg/unity-atoms-tiny-modular-pieces-utilizing-the-power-of-scriptable-objects-e8add1b95201">Unity Atoms — Tiny modular pieces utilizing the power of Scriptable Objects</a></li><li><a href="https://medium.com/@adamramberg/announcing-unity-atoms-v2-1719ef3e587e">Announcing Unity Atoms v2</a></li></ul><p>Even though that Unity Atoms in previous versions has been a solid solution for state management in Unity there have been a couple of workflows and situations that have not had a clear cut solution, for example there have not been a great solution for state associated with prefabs getting dynamically spawned at runtime. Being able to use the same code and design patterns, regardless if the asset is instantiated at runtime or statically baked into the scene, is of course preferable. I’m therefore <strong>REALLY</strong> excited to say that this has finally been addressed in Unity Atoms v4 🎉 Version 4 also implements some other cool features like Event Replays (same as <a href="http://reactivex.io/">Rx</a>’s <a href="http://introtorx.com/Content/v1.0.10621.0/02_KeyTypes.html#ReplaySubject">ReplaySubject</a>), Finite State Machines and Pre Change Transformers. Below I will discuss the new features implemented in Unity Atoms, how they work and some uses cases for them. Let’s dive into it!</p><p><em>(For those who are wondering what happened to v3 of Unity Atoms, the answer is that I basically did a rewrite of Unity Atoms two times. I rewrote and released the library a first time (version 3), but soon after release I realized a few flaws in the design, which led me to rewrite it a second time. Creating a new major for the second rewrite made sense to me. So here we are with Unity Atoms v4!)</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FxT77XKxcPqxIZqUrwk%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fexcited-corgi-xT77XKxcPqxIZqUrwk&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FxT77XKxcPqxIZqUrwk%2Fgiphy-downsized-large.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d3b116476e4518b2fb5d4a82791c933/href">https://medium.com/media/4d3b116476e4518b2fb5d4a82791c933/href</a></iframe><h3>💿 Collections &amp; Lists</h3><p>Collections and Lists are ScriptableObjects grouping Variables and Constants. Think of a Collection as a <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.8">C# Dictionary</a>, but as a ScriptableObject with strings (StringReferences) as keys and Variables or Constants as values. You can think of a List as, if you have not already guessed it, a <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.8">C# List</a> containing Variables and Constants. The values stored in both Collections and Lists are not type specific, so you can store an Int Variable in the same Collection / List as a String Constant. Another nifty thing is that Collections and Lists are in themselves also Variables and they can therefore be nested, eg. a Collection can contain a List that contains a Collection.</p><p>There are three Events associated with both Collections and Lists and they are:</p><ul><li><em>Added</em> — Triggered when an Atom is added to the Collection / List.</li><li><em>Removed</em> — Triggered when an Atom is removed from the Collection / List.</li><li><em>Cleared</em> — Triggered when the Collection / List is cleared of all Atoms.</li></ul><p>Furthermore, the Value of a Collection or a List is a reference to an actual List or Collection, eg:</p><p>_collection.Value and _list.Value</p><p>List’s Value is actually deriving from the C#’s generic <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.8">List</a> and the Collection’s Value implements C#’s <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.idictionary-2?view=netframework-4.8">IDictionary</a>, <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1?view=netframework-4.8">IEnumerable</a> and <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.icollection-1?view=netframework-4.8">ICollection</a>. This means that the you can for example can use it in a foreach loop and use it together with <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/">LINQ</a>.</p><p>One last thing to note is that Lists were actually a thing in v2 of Unity Atoms. They have however in version 4 changed name to (Atom) Value Lists, and the new List described above have taking its former name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L7Ga7S8a10usHfa_ndYt6Q.png" /><figcaption>This is how a Collection (left side) and a List (right side) looks in the inspector.</figcaption></figure><h3>🧬 Variable &amp; Event Instancers</h3><p>Variable and Event Instancers are MonoBehaviours that takes a base, a Variable or an Event of a specific type, and creates an in memory copy of that base on <a href="https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html">OnEnable</a> at runtime. And what good is this for you might ask? This is the basis of enabling using Unity Atoms in prefabs instantiated at runtime. We will take a closer look at that workflow further down in this post. Note that there are also Instancers for Lists and Collections (enabled by the fact that they are themselves Variables).</p><p>Sync Variable Instancer To Collection is a MonoBehaviour class related to Instancers. It takes a Variable Instancer as input and adds it to a List or a Collection on <a href="https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html">OnEnable</a> and removes it on <a href="https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDestroy.html">OnDestroy</a>. The List or Collection could be a regular one or a reference to one instantiated at runtime by a Collection or List Instancer. This script is also essential for the dynamic workflow explained later in this post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*bKZbXZepRuGcoEFK1yn8GQ.png" /><figcaption>An example of how an Int Variable Instancer and a Sync Int Variable Instancer to Collection component looks and could be combined.</figcaption></figure><h3>🗒️ Event References</h3><p>If you have used Unity Atoms before then you will recognize that Event References are the same thing as References, but for Events. An Event Reference is a regular Serializable C# class that has a reference to an Event, and you can from the Inspector choose one of three sources:</p><ul><li><em>Event</em> —Uses a regular Unity Atoms Event.</li><li><em>Event Instancer </em>— Uses the in memory Event created by the Event Instancer.</li><li><em>Variable</em> — Uses the Changed / Changed With History Event associated with the provided Variable.</li><li><em>Variable Instancer</em> — Uses the Changed / ChangedWithHistory Event associated with the in memory Variable created by the Instancer.</li></ul><p>Furthermore, Listeners are now using Event References instead of regular Events.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*7eYiZf5rnUjeanm-Q6MFdQ.png" /><figcaption>Listeners are now using Event References.</figcaption></figure><h3>📹 Replay Atom Events</h3><p>All Events in Unity Atoms v4 have a property called Replay Buffer Size, which defines the size of a buffer that stores Event values emitted by the Event. For example, if an Int Event raises the values [1, 2, 3, 4, 5], in that order, and the replay buffer size is 3, then the buffer will contain [3, 4, 5]. Next time someone registers to the Event, then the Event will emit the last 3 values, [3, 4, 5], to that listener. Like mentioned in the introduction, this is the same behaviour as <a href="http://introtorx.com/Content/v1.0.10621.0/02_KeyTypes.html#ReplaySubject">ReplaySubjects</a> in <a href="http://reactivex.io/">ReactiveX</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*wmPVL0QuSCkc8PGpmx2x1w.png" /><figcaption>Replay Buffers for all Events! 🎉</figcaption></figure><h3>🏋️‍♂️ Pre Change Transformers</h3><p>Pre Change Transformers are Functions that are called and applied to Variables before its value changes. Pre Change Transformers takes the new value as a parameter, performs its logic, and then returns a value of the same type. This enables for example clamping of Int and Float Variables, which is provided by default in Unity Atoms v4. However, this functionality could be used and extended for other features, for example capitalizing a String Variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*rNtPTMmI-dJAJAfrvnv2sQ.png" /><figcaption>Add Pre Change Transformers to your Variables.</figcaption></figure><h3>🤖 New Package — FSM</h3><p>Unity Atoms now have a light weight implementation of a Finite State Machine (FSM). The state machine is made up of a list of states (strings) and a list of transitions expressing how the state machine can change from one state to another. In order to go from one state to another you need to dispatch a command. The FSM in Unity Atoms is actually an extension of the String Variable, so it will have all the functionality that a String Variable posses. Unity Atoms FSM was inspired by <a href="https://github.com/dubit/unity-fsm">this</a> package and the following example is taken from there:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*1JKH6H2vqwp9PWPpzUVFzw.png" /></figure><p>This FSM represents states and transitions of a door. It has the following states:</p><ul><li>OPEN</li><li>CLOSED</li><li>LOCKED.</li></ul><p>It also has 4 transitions:</p><ul><li>From OPEN to CLOSED when dispatching the command CLOSE.</li><li>From CLOSED to LOCKED when dispatching the command LOCK.</li><li>From LOCKED to CLOSED when dispatching the command UNLOCK.</li><li>From CLOSED to OPEN when dispatching the command OPEN.</li></ul><p>This how the above FSM would be expressed in Unity Atoms:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*diw83IVtlePbQVtQTWRYyA.png" /><figcaption>A door represented as a FSM in Unity Atoms.</figcaption></figure><h4>State explained</h4><ul><li><em>Id</em> — the state identifier as a string. The first state’s Id will be the default state of the FSM.</li><li><em>Cooldown</em> — if set to a number above 0 the state machine will loop on this state and re set the value with the duration specified by the cooldown (seconds). If for example cooldown is set to 1 and the state id is set to EXAMPLE, then the state machine will be set to EXAMPLE every second. This means that the FSM&#39;s Changed Event will be triggered every second as long as the FSM is in the state called EXAMPLE.</li><li><em>Sub Machine</em> — FSMs can be nested. If a sub machine is specified for a state it will take over when resolving the state value for that particular state.</li></ul><h4>Transitions explained</h4><ul><li><em>From State</em> — The state that we transition from.</li><li><em>To State </em>— The state that we transition to.</li><li><em>Command</em> — The command that needs to be dispatched in order to begin this transition.</li><li><em>Test Condition</em> — If specified this condition must be true in order to start the transition.</li><li><em>Raise Event To Complete Transition</em> — If set to true the transition will not complete automatically, Instead you will need to manually Raise the Complete Current Transition Event (specified above all states).</li></ul><h3>🦸‍♂️ Leveraging Your Newly Gained Powers</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*coayXkK9sEfT86ZlV3zTrg.gif" /><figcaption>Demo where enemies and corresponding health bars that are dynamically instantiated at runtime.</figcaption></figure><p>Imagine that you have enemies that you want to spawn dynamically at runtime and that you also would like to create health bars that are displayed above each enemy when they get instantiated. You would start by creating a new List called Enemies that will hold all the data about all the enemies in our scene. Using the new tools at our disposal you would then create an enemy prefab with the following:</p><ul><li><em>Collection Instancer</em> — Creates a Collection at runtime that will hold all the data / Variables associated with this enemy.</li><li><em>Sync Collection Instancer To Collection</em> — Adds the Collection created in the above script to the Enemies List.</li><li><em>Int Variable Instancer</em> — Creates an in memory copy Variable representing the enemy’s health.</li><li><em>Sync Int Variable Instancer to Collection</em> — Adds the health Variable to the enemy data Collection created by the Collection Instancer.</li><li>Vector 3 Instancer — Creates an in memory copy Variable representing the enemy’s position.</li><li><em>Sync Vector3 Variable Instancer to Collection</em> — Adds the position Variable to the enemy data Collection created by the Collection Instancer.</li><li>A custom script for syncing the enemy’s position with the Variable created above.</li></ul><p>After setting up the above we now have a List of enemy data that gets populated at runtime. A list of 3 enemies could be visualized like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/205/1*pcQqyCErTQy3pLqHZd4jsQ.png" /></figure><p>We can now use the Enemies List’s Added and Removed Events to, in a data driven manner, add, remove and position health bars above the enemies in the scene. The code for that would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5756bdcb59320b168b1d907caddc6ea5/href">https://medium.com/media/5756bdcb59320b168b1d907caddc6ea5/href</a></iframe><p>For the full-fledged version of the above you should checkout the InfiniteWaves demo scene in the <a href="https://github.com/AdamRamberg/unity-atoms/tree/master/Examples">Examples project</a> on Github.</p><h3>🏠 Minor and Internal Changes</h3><p>Here follows some minor and internal changes made in v4 of Unity Atoms.</p><h4>🎧 Listeners</h4><p>Most Listeners are now referred to as Event Reference Listeners for the simple reason that they are utilizing Event References instead of just regular Events. This means that you can drag in an Event, Event Instancer, Variable or Variable Instancer as the Listener’s source.</p><h4>👫 Pairs</h4><p>The Changed With History Event is no longer raising an Event x 2 / Action&lt;T,T&gt;, Unity Atoms is instead generating Pair structs and is emitting those pairs in a regular Event / Action&lt;T&gt;. The most important aspect of this change is that when you are using Listeners (for pairs / Changed With History), your Responses will have one Pair parameter instead of two parameters of the value type. The reason for using Pairs is to make the code base for Unity Atoms more flexible, clean and DRY.</p><h4>📦 New package — Base Atoms</h4><p>Base atoms (eg. Int Variables, String Variables, etc.) are moved from Core into a new package called Base Atoms. This enables faster experimentation and development of Unity Atoms. This means that you are required to drag in both Core and Base Atoms in order to leverage Unity Atoms in your project.</p><h3>👀 A Look Into The Future</h3><p>The main focus going forward will be to create more and better documentation around Unity Atoms and the ecosystem. This means that the API references will be polished and the website documentation refined. Furthermore, I believe that my blog posts have helped a lot of people getting into Unity Atoms, but there might be some other channels going forward, for example it might make sense to create a video tutorial series making a simple game using Unity Atoms (let me know if you would be interested in this).</p><p>A huge thing coming up in Unity 2020 is serialization of generics. This will greatly reduce the code size of Unity Atoms, improve the readability of the code and enable faster iterations of new features added to the library.</p><p>Would love feedback on the new version of Unity Atoms, so take it for a spin and let me know what you think about it. <a href="https://discord.gg/W4yd7E7">Join the Discord</a> if you want to chat and sign up for my mailing list to get future updates on Unity Atoms and other development related news. If you liked this post give it some 👏 and follow me on <a href="https://twitter.com/adamramberg1">twitter</a>. Thanks for reading 💜</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b15a37da49da" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Announcing Unity Atoms v2]]></title>
            <link>https://medium.com/@adamramberg/announcing-unity-atoms-v2-1719ef3e587e?source=rss-8834103955f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1719ef3e587e</guid>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity]]></category>
            <dc:creator><![CDATA[Adam Ramberg]]></dc:creator>
            <pubDate>Thu, 24 Oct 2019 06:34:52 GMT</pubDate>
            <atom:updated>2019-10-24T07:56:17.905Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_gacEPFI51_E8TUDlitGIg.png" /><figcaption>Check out <a href="https://github.com/AdamRamberg/unity-atoms">Unity Atoms on GitHub</a>.</figcaption></figure><p>I’m the author of an open source library for the game engine Unity called <a href="https://github.com/AdamRamberg/unity-atoms">Unity Atoms</a>. The library is inspired by <a href="https://www.youtube.com/watch?v=raQ3iHhE_Kk&amp;t=2787s">this talk</a> by <a href="https://twitter.com/roboryantron">Ryan Hipple</a> from Unite 2017. I started working on the project in October 2018 and back then it was meant for internal use in my current game side project. Since then it has, after many late nights of work, grown and become a full fleshed solution for working with Scriptable Objects within Unity. Today I’m proud to finally announce that version 2 of Unity Atoms is released 🎉</p><h3>❓ <strong>So what is it and how do I use it?</strong></h3><p>At its core, Unity Atoms makes Variables, Events, Listeners, Actions and Functions into modular reusable pieces of code by leveraging the power of Scriptable Objects. Together with Unity’s inspector this makes your code much more modular and testable. Below follows a simple example on how this looks in practice.</p><p>First create a MonoBehaviour with an IntVariable called Health describing the health of our game’s player:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f2aebdf1e33580a1a733a8966a381d4/href">https://medium.com/media/2f2aebdf1e33580a1a733a8966a381d4/href</a></iframe><p>The IntVariable is a Scriptable Object (.asset file) that could be injected via Unity’s inspector:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*gY3mqJTapHLAGdmPXYWfPA.png" /></figure><p>The IntVariable will look like this when inspecting it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*0a-ZI_8cwdg9Rxm_xxzrcA.png" /></figure><p>We can now use and reference the Player’s health in other scripts by just injecting the IntVariable via the Inspector:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fb8012254767d951fa4b6f69aec338b/href">https://medium.com/media/6fb8012254767d951fa4b6f69aec338b/href</a></iframe><p>No more direct dependencies between your MonoBehaviours, eg. doing this in your health bar script: player.GetComponent&lt;PlayerHealth&gt;().Health -= 10f;. Hurray! 🥳</p><p>For a more detailed description of how to use the library, read my <a href="https://medium.com/@adamramberg/unity-atoms-tiny-modular-pieces-utilizing-the-power-of-scriptable-objects-e8add1b95201">Unity Atoms announcement blog post</a>. When you are done reading the blog post you can head over to the <a href="https://adamramberg.github.io/unity-atoms/">brand new website</a> for more tips and tricks.</p><h3>🚀 New features</h3><p>The goal of Unity Atoms version 2 has been to improve the overall developer experience and to polish the APIs making it easier to work with. Here are some of the highlights included in version 2:</p><ul><li><strong>Mono repo:</strong> The project has been made into a mono repo and the code has been split up into 6 different packages: core, mono-hooks, tags, mobile, scene-mgmt and ui. You can now pick and choose exactly what you need for your project.</li><li><strong>Code generator:</strong> Unity Atoms version 2 includes a powerful code generator that can be accessed from the top menu bar: Tools/Unity Atoms/Generator. This makes it a breeze to create your own Atoms specific to your project.</li><li><strong>UniRx support:</strong> Events (OnEvent), Variable Events (Changed and ChangedWithHistory) and List Events (Cleared, Added and Removed) can now be turned into <a href="https://docs.microsoft.com/en-us/dotnet/api/system.iobservable-1?view=netframework-4.8">IObservables&lt;T&gt;</a> making Unity Atoms work great with <a href="https://github.com/neuecc/UniRx">UniRx</a>.</li><li><strong>Custom PropertyDrawers:</strong> All Atoms now have custom <a href="https://docs.unity3d.com/ScriptReference/PropertyDrawer.html">PropertyDrawers</a>.</li><li><strong>Custom editor icons: </strong>All Atoms now have custom editor icons.</li><li><strong>New website:</strong> As part of making Unity Atoms more available to developers we have launched a <a href="https://adamramberg.github.io/unity-atoms/">new website</a> full of examples and documentation.</li></ul><h3>🔮<strong> What’s next?</strong></h3><p>Version 2 is a great step forward for the library, but we want to take things even further. Here follows a list of things that we are currently looking into:</p><ul><li>Polishing up the Editors and PropertyDrawers even more.</li><li>Investigate how Unity Atoms could integrate with <a href="https://unity.com/dots">DOTS</a>.</li><li>Variable post processors, eg. clamp value (int, float, etc.).</li><li>Improve debugging.</li><li>More generator options, eg. generation of multi value Atoms.</li></ul><p>We also want to listen to the community and implement features that you find valuable. If you got an enhancement that you want to add to Unity Atoms then head over to the <a href="https://github.com/AdamRamberg/unity-atoms/issues">issues pages</a> on Github.</p><p>We are also looking for more contributors and maintainers for Unity Atoms. Please contact us if you to help the project by becoming one.</p><p>Thanks for reading! If you liked what you just read and want to see more of this in the future, then give the post some 👏 and follow me on <a href="https://twitter.com/adamramberg1">twitter</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1719ef3e587e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Translate your React app with ease using Facebook’s own framework (FBT)]]></title>
            <link>https://medium.com/@adamramberg/translate-your-react-app-with-ease-using-facebooks-own-framework-fbt-e8f6af04d3d9?source=rss-8834103955f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8f6af04d3d9</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[facebook]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[i18n]]></category>
            <dc:creator><![CDATA[Adam Ramberg]]></dc:creator>
            <pubDate>Wed, 28 Aug 2019 20:20:23 GMT</pubDate>
            <atom:updated>2019-08-28T20:20:23.735Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*73-h3VkS9mjNnZV5X8T4cw.png" /></figure><p><em>Checkout </em><a href="https://adamramberg.github.io/fbt-easy-setup/"><em>this demo app</em></a><em> using the concepts from this blog post.</em></p><p>This spring (2019) I went to <a href="https://www.react-europe.org/">React Europe</a>, a really great conference dedicated to <a href="https://reactjs.org/">React</a> development. <a href="https://twitter.com/JRWats0n">John Watson</a> had a talk there where he introduced an internationalization (i18n) framework called <a href="https://github.com/facebookincubator/fbt">FBT</a>, which is used over at Facebook. I only had prior experience using <a href="https://github.com/formatjs/react-intl">react-intl</a>, so when the time came to translate my <a href="https://mambojambostudios.com/">companies’ website</a> I wanted to give FBT a go. This article is going to go through my findings using FBT for the first time and will be show casing examples that you can follow in order to add FBT to your own React application.</p><h3>🤔 Why should I use FBT?</h3><p>FBT has a couple of features that stands out compared to other similar frameworks (for example react-intl):</p><ul><li>Translatable text resides inline with the rest of your JSX. This means that you can search for English text strings in your text editor and directly find the React component where it’s used.</li><li>It supports <a href="https://facebookincubator.github.io/fbt/docs/plurals">plural variations</a> out of the box.</li><li>It supports <a href="https://facebookincubator.github.io/fbt/docs/enums">enums</a>!</li><li>Adjust translations based on <a href="https://facebookincubator.github.io/fbt/docs/pronouns">pronouns</a>.</li></ul><h3>🍖 The meat</h3><p><em>Note: the steps below assumes your is using </em><a href="https://babeljs.io/"><em>Babel</em></a><em>, </em><a href="https://webpack.js.org/"><em>Webpack</em></a><em> and </em><a href="https://yarnpkg.com/lang/en/"><em>yarn</em></a><em>. It also assumes that you are developing in an UNIX environment. If you don’t have an existing app and instead want to start a new one, you can use the template app created in my other blog post </em><a href="https://medium.com/@adamramberg/setting-up-a-react-app-from-scratch-42521a118b10">Setting up a React app from scratch</a>.</p><h4>Install Dependencies</h4><p>Start by installing <a href="https://www.npmjs.com/package/fbt">fbt</a> and <a href="https://github.com/AdamRamberg/fbt-easy-setup">fbt-easy-setup</a> to your project. Open your terminal, navigate to the root of your project (where your package.json resides) and type the following:</p><pre>yarn add fbt fbt-easy-setup --save</pre><p>fbt-easy-setup is a package that I have created to make life easier adding fbt to a React app. It provides you with an init function, a LocaleProvider and a LocaleConsumer (see <a href="https://reactjs.org/docs/context.html">React’s documentation on Context</a>). Check out the docs for fbt-easy-setup <a href="https://github.com/AdamRamberg/fbt-easy-setup">here</a>.</p><p>Continue by installing the following dev dependencies:</p><pre>yarn add babel-plugin-fbt babel-plugin-fbt-runtime @babel/node @babel/core fbt-generate-translations --dev</pre><p>babel-plugin-fbt and babel-plugin-fbt-runtime are two Babel plugins that are required in order to run FBT. See more info <a href="https://facebookincubator.github.io/fbt/docs/transform">here</a> regarding those two plugins. Both @babel/node @babel/core are needed in order for the plugins to run properly. fbt-generate-translations is a script that I have made in order to generate translations from the output from FBT’s <a href="https://github.com/facebookincubator/fbt/blob/master/transform/babel-plugin-fbt/bin/collectFBT.js">collect script</a> and consumed by FBT’s <a href="https://github.com/facebookincubator/fbt/blob/master/transform/babel-plugin-fbt/bin/translate.js">translate script</a>. This step would otherwise be manual (ugh!). More on the different scripts and steps later in this post.</p><h4><strong>Configure Babel</strong></h4><p>Add the babel plugins installed above to your babel config file. If you are going to use enums the babel-plugin-fbt should be fed a path to the generated enums manifest (more on what this is later in the post). You also might need to add <a href="https://babeljs.io/docs/en/options#sourcetype">sourceType</a> to be unambiguous (at least I did) in order for the mix between commonjs modules and ES modules to transpile correctly. Here is an example of what to you need to add to your Babel config:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76fde59b311ea7cb6bd9aa2e1de4c043/href">https://medium.com/media/76fde59b311ea7cb6bd9aa2e1de4c043/href</a></iframe><h4>Add locales</h4><p>Let’s add the locales that you want your app to support! Start by creating a file defining the locales and add it to a folder called i18n:</p><pre>mkdir i18n &amp;&amp; touch ./i18n/locales.js</pre><p>Open up locales.js in your favourite text editor and add your locales of choice using the following format:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1207fb838345f02cd48b6916f0b43c43/href">https://medium.com/media/1207fb838345f02cd48b6916f0b43c43/href</a></iframe><h4>Add scripts</h4><p>When first getting into the <a href="https://github.com/facebookincubator/fbt/tree/master/demo-app">FBT demo</a>, <a href="https://github.com/facebookincubator/fbt/blob/master/demo-app/package.json">the scripts</a> was the most confusing part to me. There are 3 scripts in the FBT demo app, and I have added another one in order to reduce the manual work needed:</p><ul><li><em>manifest-fbts</em> — Creates two json files, the first one (.src_manifest.json) keeps track of every file that imports FBT (and therefore might contain translations) and the other one (.enum_manifest.json) keeps track of all FBT enums used in the project.</li><li><em>collect-fbts</em> — Given the output from the last step, it collects all the texts to translate and generate corresponding hashes. See <a href="https://facebookincubator.github.io/fbt/docs/collection">this</a> for more info.</li><li><em>fbt-generate-translations / fbt-generate-translations-single-file</em> — Given the output from collect-fbts, it generates translation files (could also just be a single file) that could be consumed by translate-fbts (see below). The file / files generated are then supposed to be manually edited with translations for each locale.</li><li><em>translate-fbts / translate-fbts-single-file</em> — Takes the output from the step above and creates the translation object that should be passed to FBT’s init function at runtime.</li></ul><p>It is also useful to add scripts to clean the generated files (see clean-fbts below) and to do all the steps of above in one go (see prepare-fbts below). If you got scripts to build and start the app it might also be useful to add pre-scripts that runs all the steps above one go (see prestart and prebuild below). My scripts ended up like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd597aa84b2ec9777578c18e29217a1b/href">https://medium.com/media/cd597aa84b2ec9777578c18e29217a1b/href</a></iframe><p><em>As a side note: FBT supports having all translations in one file (using the *-single-file script above) or dividing the translations by locale into several files.</em></p><h4>Initialize FBT</h4><p>In order for FBT to work you need to provide translations. This needs to happen before any of the React components containing translations load. You also need to pass your locales and default locale to the this function. Mine ended up like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a98d70016ce2dbb15d9a4e43a9d70d69/href">https://medium.com/media/a98d70016ce2dbb15d9a4e43a9d70d69/href</a></iframe><p>Since initialization needs to be done before the translations are used in any React component, add it to your Webpack config’s entry:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/011c5b8c975a8396a1f3a6b30a1e81f1/href">https://medium.com/media/011c5b8c975a8396a1f3a6b30a1e81f1/href</a></iframe><h4>Add the Provider and the Consumer</h4><p>The next step is to add the LocaleProvider and theLocaleConsumer to your app. Start by wrapping your app with the LocaleProvider:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/250c34f5fae9de4c292223e29fdde89d/href">https://medium.com/media/250c34f5fae9de4c292223e29fdde89d/href</a></iframe><p>Now you can use the LocaleConsumer where applicable. For example you can create a component that toggles between English and Swedish like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/642af7fa6d00d10adc66ca8728157921/href">https://medium.com/media/642af7fa6d00d10adc66ca8728157921/href</a></iframe><h4>Add your text</h4><p>You are now all setup to add translations to you app! Hurray!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FEXFAJtutz5Ig8%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FEXFAJtutz5Ig8%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FEXFAJtutz5Ig8%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/56e844375722861727425fbd9ccadefb/href">https://medium.com/media/56e844375722861727425fbd9ccadefb/href</a></iframe><p>You can now for example add a fancy translatable paragraph component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eef5251626431d5af0bfa77b040071d7/href">https://medium.com/media/eef5251626431d5af0bfa77b040071d7/href</a></iframe><p>FBT’s docs for how to add translatable text to your JSX is actually really good. Check it out <a href="https://facebookincubator.github.io/fbt/docs/api_intro">here</a>!</p><h4>Git ignore</h4><p>All git users out there should also add the following to.gitgnore:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b66864769263817a4a614514eefff01/href">https://medium.com/media/1b66864769263817a4a614514eefff01/href</a></iframe><h4>Workflow</h4><p>When you have added all the translatable text you want to your JSX you should run yarn prepare-fbts. After that you can add the translations for each locale to the generated file(s) (either in translations_input.json or in the files located in the translations folder). The typical workflow for translations (using the setup from this blog post) will look like this:</p><ul><li>Add FBT JSX to your app.</li><li>Run your scripts to generate translation files.</li><li>Add and modify translations in the generated files from the above step.</li><li>Build and run your app that now contains the new translations.</li></ul><p>However, there are endless possibilities on how to improve this process. For example it is possible (with the help of some custom scripts) to fetch translations from a remote location (eg. a translations API) and merge them with your translation file(s). This process could for example be a part of your build pipe. This is actually how Facebook is using it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kyE89iDx9ucF38iiYwnNxg.png" /><figcaption>Architectural image posted by John Watson in <a href="https://github.com/facebookincubator/fbt/issues/55#issuecomment-473142744">this</a> issue.</figcaption></figure><h4>Some notes on shared enumerations</h4><p>Something that is not documented when it comes to shared enums is that you need to add the suffix $FbtEnum.js to the filename of the module that defines it.</p><p>Another thing to note is that due to some issues regarding the babel plugin (eg. see <a href="https://github.com/facebookincubator/fbt/issues/46">this</a> issue for example), you need to use require and not ES <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import">imports</a> when using the module that defines the shared enum:</p><pre>const SharedEnum = require(&#39;Shared$FbtEnum&#39;);</pre><p>You also might notice that the shared enum above is required using an absolute path. Due to a bug in the babel plugin this is required, otherwise it throws. In order for the absolute path to work you will need to add a resolve entry to your Webpack config:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69bea5b7e5075f2110dae38fec7900e9/href">https://medium.com/media/69bea5b7e5075f2110dae38fec7900e9/href</a></iframe><h3>😝 Still stuck?</h3><p>If you have followed this post you should have a working app with translations. However, if you for some reason is stuck you can check out the <a href="https://github.com/AdamRamberg/fbt-easy-setup/tree/master/demo">source</a> on Github of <a href="https://adamramberg.github.io/fbt-easy-setup/">this demo app</a>.</p><h3>🌦️ Conclussions</h3><p>FBT promises some really awesome features like inlining translatable JSX text (which makes the text searchable in your text editor), support for plural variations, enums and pronouns. For the most part this works really well (after the initial setup has been made). However, the problems with enums shines light on some FBT’s rough edges. It feels like FBT needs some more users and maintainers in order to get some of these issues discovered and fixed. So if you are not afraid to dig in to some open source work, I definitely think that FBT is great candidate for managing i18n in your next React app.</p><p>Let me know what you think about this post in the comments below and hit that 👏 button if you liked what you read. You can also find me on <a href="https://twitter.com/adamramberg1">Twitter</a> if you got any further questions or comments on what you just read.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8f6af04d3d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create better code using delegates and extension methods in Unity]]></title>
            <link>https://medium.com/@adamramberg/create-better-code-using-delegates-and-extension-methods-in-unity-6d14256ca4d5?source=rss-8834103955f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d14256ca4d5</guid>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unicorns]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[unity3d]]></category>
            <dc:creator><![CDATA[Adam Ramberg]]></dc:creator>
            <pubDate>Sat, 16 Feb 2019 16:38:29 GMT</pubDate>
            <atom:updated>2019-02-16T16:56:15.223Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kDueSHQGO3vxnJ4qx68CUA.png" /></figure><p>Delegates and extension methods are tools in your C# toolbox that, if used right, can greatly enhance the quality of your code, improve its reusability and readability. But what are delegates and extensions? Here is an explanation of what delegates are from one of the first hits on Google:</p><blockquote>A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. — <a href="https://www.akadia.com/services/dotnet_delegates_and_events.html">akadia.com</a></blockquote><p>In other words, a delegate declaration describes a method signature (a method’s return type and its argument types), while an instance of that delegate can be assigned, referenced and invoke a method matching that signature. You have probably encountered <a href="https://docs.microsoft.com/en-us/dotnet/api/system.action">Action</a>, <a href="https://docs.microsoft.com/en-us/dotnet/api/system.func-1">Func</a> and <a href="https://docs.microsoft.com/en-us/dotnet/api/system.eventhandler">EventHandler</a> before, they are all examples of delegates. If you have ever used functions like <a href="https://docs.microsoft.com/en-us/dotnet/api/system.array.find">Array.Find</a> or <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.foreach">List.ForEach</a>, then you have also used delegates. Here is an example of a delegate that takes an int as a parameter and returns a bool:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ce312496bbd0da9221f52e406ba1bd9/href">https://medium.com/media/3ce312496bbd0da9221f52e406ba1bd9/href</a></iframe><p>Extension methods are explained like this in the <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods">official .NET C# documentation</a>:</p><blockquote>Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.</blockquote><p>The most commonly used extension methods are the ones from <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/">LINQ</a>. However, there is nothing stopping you from adding your own extension methods. Below follows an example of adding a chainable add method to the generic <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1">List</a> class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/762184e2625ba56a4eef72c1d5e4905f/href">https://medium.com/media/762184e2625ba56a4eef72c1d5e4905f/href</a></iframe><h3>🦄 The good</h3><p>Great, so now you know what delegates and extensions are! But why should you use them in your code? Here is a quick list of advantages of using delegates and extensions:</p><ul><li>Encapsulation and code reusability</li><li>Extensibility of your functions and built in C# types and classes</li><li>Code that is easier to understand, maintain and read</li></ul><p>Let’s show this in an example. I usually find myself in my games iterating over a collection of items and perform an action or change its state depending on a condition. Imagine that you have an Unicorn that likes to give hugs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5621f1d6e6e5d0e63e2566ca1b70b707/href">https://medium.com/media/5621f1d6e6e5d0e63e2566ca1b70b707/href</a></iframe><p>And let’s say that you also have an unicorn controller class (shame on you) that iterates over the unicorns and makes them hug your player if it comes too close:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a32d4571159643d84c1e926cede3a85/href">https://medium.com/media/9a32d4571159643d84c1e926cede3a85/href</a></iframe><p>It works and all, but an approach like this soon becomes really messy, especially when we need to expand on the logic. An alternative to this would be to create an extension method that is using delegates for iterating a <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1">List</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a972540343217c6789f3123b4d332f4f/href">https://medium.com/media/a972540343217c6789f3123b4d332f4f/href</a></iframe><p>You can now rewrite your previous unicorn controller class to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c74574a992689fe0dd79022642352aa/href">https://medium.com/media/1c74574a992689fe0dd79022642352aa/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F26hityqUtkQfLoWQM%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2FOriginals-meme-look-26hityqUtkQfLoWQM&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F26hityqUtkQfLoWQM%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="245" frameborder="0" scrolling="no"><a href="https://medium.com/media/46f0a3191698a859fb49a80a7648e2bc/href">https://medium.com/media/46f0a3191698a859fb49a80a7648e2bc/href</a></iframe><p>From my experience these kind of patterns are used over and over again and writing those extension methods are worth while your time. The extension methods could also be reused, not only in this project, but in your other projects as well (maybe by creating your own Unity package), which will save you a lot of time in the long run. Having the implementation for the ForEach method only defined once makes your code base also more maintainable. Furthermore, I find the second approach much easier to read and grasp at first glance.</p><h3>👾 The bad</h3><p>That is all great! However, there are also some potential pitfalls using delegates and extensions like this in Unity:</p><ul><li>Learning curve</li><li>Unintentional creation of garbage (GC)</li></ul><p>Many people finds it hard to wrap their head around delegates and why they are sometimes preferred over regular function calls. There is an initial learning curve, but once you get passed it they are easy to grasp. If you are creating a library of extension methods using delegates it can also become hard for new developer to understand the code right away (good names for your extension methods are really key here). You can always point to this blog post in order to get rid off the learning curve problem 😊 However, the unintentional creation of garbage is worse of a problem and could potentially create a world of hurt…</p><h3>💩 The ugly</h3><p>I came across <a href="https://jacksondunstan.com/articles/3765">this</a> blog post by Jackson Dunstan the other day. It explains really well how easy it is to unintentionally create garbage while using delegates when coding C# in Unity. Head over to <a href="https://jacksondunstan.com/articles/3765">Jackson’s blog</a> post and read it while I will wait here for you.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FtXL4FHPSnVJ0A%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fkim-novak-tXL4FHPSnVJ0A&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FtXL4FHPSnVJ0A%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="287" frameborder="0" scrolling="no"><a href="https://medium.com/media/417ec13e55809e9bc5429dd206f8450e/href">https://medium.com/media/417ec13e55809e9bc5429dd206f8450e/href</a></iframe><p>When reading the blog post I was surprised by two things. First, that passing an instance method to a method that takes a delegate actually creates garbage every time you call that method. Second, I was expecting that a <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions">lambda</a> function passed as a delegate would create garbage every time it was used, but the C# compiler actually caches the lambda function for you. However, if the lambda function (or an anonymous method) is using variables or data from the outside scope, it won’t be cached and will instead create garbage each call, just like the use of instance methods. Here follows some tips for what to avoid when using delegates in Unity:</p><ul><li>Don’t pass instance methods directly to methods taking a delegate as a parameter. There are two alternatives 1) use lambdas, static or anonymous methods or 2) you can cache the instance method in a variable and pass that variable.</li><li>When relying on the C# compiler to cache your delegate (when passing lambdas or anonymous functions directly), then never reach for variables that are outside the method’s own scope. Think of those methods as static (or pure / functional).</li></ul><p>Jackson is further talking about the expense of branching. There is a difference between if you pass a method directly to a method taking a delegate versus if you cache your delegate in a variable and pass that variable instead. In the former there will be a check against the C# compiler’s own cache and we will always do a cache check (that according to Jackson can hurt performance) while in the latter the compiler will still keep its cache field meaning that there are duplicate cache fields (yours and the compiler’s). I would argue that the performance difference between these are in general too small to care about. When I was benchmarking this on my computer I could not see a big difference in execution time. So I will personally in most cases just not cache my delegates until it actually becomes a real problem.</p><h3>🤔 Conclusion</h3><p>There are some great benefits of using delegates and extension methods in this way, which makes your code reusable, readable and more concise. Just keep in mind how to avoid creating unintentional garbage. Let me know what you think in the comments below and hit that 👏 button if you found what you just read interesting. You can also hit me up on <a href="https://twitter.com/adamramberg1">Twitter</a> if you got any questions or comments on what you just read.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d14256ca4d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unity Atoms — Tiny modular pieces utilizing the power of Scriptable Objects]]></title>
            <link>https://medium.com/@adamramberg/unity-atoms-tiny-modular-pieces-utilizing-the-power-of-scriptable-objects-e8add1b95201?source=rss-8834103955f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8add1b95201</guid>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Adam Ramberg]]></dc:creator>
            <pubDate>Thu, 15 Nov 2018 06:57:25 GMT</pubDate>
            <atom:updated>2020-02-16T11:10:56.987Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*egcBVPFiruu4_0LWkdZkVA.png" /><figcaption><em>Unity Atoms is an open source library that aims to make your Unity game code modular, editable and debuggable. The library can be found </em><a href="https://github.com/AdamRamberg/unity-atoms"><em>here</em></a><em> on GitHub.</em></figcaption></figure><p>I’ve been working with <a href="https://unity3d.com/">Unity</a> for several years, creating mobile games for iOS and Android as well as using it during several <a href="https://ldjam.com/">Ludum Dare</a> game jams. I generally really like Unity and everything that you get for free using it. However, seeing myself as a programmer first, I have always struggled finding a good way of creating a modular and maintainable code base. The general approach of building scripts in Unity (shown in most tutorials online) often yields very tightly coupled and monolithic scripts. This makes the code cumbersome to test, hard to debug and tricky to understand. It gets even worse when many developers are working together on a larger project. The problems described above have always bothered me and made me less motivated when working on my games and has sometimes even made me a sad developer 😢</p><h3>The Revelation of ScriptableObject</h3><p>Seeing <a href="https://www.youtube.com/watch?v=6vmRwLYWNRo&amp;t=738s">this</a> talk by Richard Fine during Unite 2016 really opened up my eyes to the use of Scriptable Objects in Unity. Ryan Hipple’s <a href="https://www.youtube.com/watch?v=raQ3iHhE_Kk&amp;t=2787s">talk</a> from Unite 2017 took the use of Scriptable Objects further and introduced the audience to the concept of shared state variables as Scriptable Objects and a nifty event-listener pattern. I really recommend watching both talks above as well as reading <a href="https://unity3d.com/how-to/architect-with-scriptable-objects">this</a> article about game architecture and Scriptable Objects.</p><p>Even though I really believe that you should watch the videos above and wrap your head around Scriptable Objects before continuing, I understand that it is a big commitment. Therefore, I will try to explain and summarize why Scriptable Objects is a great tool in the Unity toolbox by listing some of its properties that makes it powerful, but also what makes it different from MonoBehaviours:</p><ul><li>Unlike MonoBehaviours, Scriptable Objects do not live in a scene, are not attached to GameObjects and do not have a transform component.</li><li>Can be saved as .asset files, but can also be instantiated at runtime and live in memory.</li><li>Gets serialized in the Unity editor and are therefore viewable in the Unity Inspector.</li><li>Do not have most lifecycle callbacks that you are used to on MonoBehaviours, besides OnEnable and OnDisable.</li><li>Excellent for data storage.</li></ul><p>All these features of Scriptable Objects make them extremely suited for replacing state and functionality that otherwise would live in MonoBehaviours. By defining small pieces of state (eg. just a variable), breaking out tiny reusable functions to Scriptable Objects and at the same time viewing Unity’s Inspector as a way of inject dependency, we can build modular pieces of code and state that can be reused and injected to our MonoBehaviours.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FZw97g5eB1mBji%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Faziz-ansari-tom-haverford-relax-Zw97g5eB1mBji&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FZw97g5eB1mBji%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/42029be783ae7bd8dfaeb133cd8e36f1/href">https://medium.com/media/42029be783ae7bd8dfaeb133cd8e36f1/href</a></iframe><h3>⚛️ Unity Atoms</h3><p><a href="https://github.com/AdamRamberg/unity-atoms">Unity Atoms</a> is an open source library that I have started to develop during the development of my current game project. It is based and derived from Ryan Hipple’s <a href="https://www.youtube.com/watch?v=raQ3iHhE_Kk&amp;t=2787s">talk</a> from Unite 2017 mentioned above. The library is still in its infancy and implementations are therefore due to change over the course of the game’s development. The main idea is to create a set of reusable building blocks that could be reused between projects and shared with others in the Unity community. My philosophy is that ideas and concepts thrives when they are shared and worked on together, so hit me up if you get excited about Unity Atoms and want to contribute or discuss new ideas to implement.</p><p>But enough about that. Lets dig into Unity Atoms! There are four fundamental building blocks of Unity Atoms that are essential to understand when working with the library: <em>Variables, Game Events, Game Event Listeners </em>and<em> Responses</em>. Below we will unfold what each of them do and how they interact with each other.</p><h3>📦 Variables</h3><p>Variables are storing data, for example primitives, reference types or structs as Scriptable Objects. Because Variables are stored as Scriptable Objects they are not part of any scene, but could instead be seen as part of the game’s global shared state. Variables are designed to make it easy to inject them (via the Unity Inspector) and share them between your MonoBehaviours. Lets see an example!</p><p>Imagine you have a PlayerHealth.cs script that contains the health of the game’s player. We will attach the script to a GameObject with a SpriteRenderer, BoxCollider2D and a Rigidbody2D called Player. The health is represented by an int, which corresponds to an IntVariable in Unity Atoms. The script will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf66a656fa53ad914cc83a5bf273f1da/href">https://medium.com/media/bf66a656fa53ad914cc83a5bf273f1da/href</a></iframe><p>In the game the player’s health will decrease when hitting something harmful. We will attach this Harmful.cs script to a GameObject called Harmful that also has a SpriteRenderer and a BoxCollider2D (as a trigger):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0694f2ea5996b6de5202e05f72972a3c/href">https://medium.com/media/0694f2ea5996b6de5202e05f72972a3c/href</a></iframe><p>Finally we will add an UI HealthBar.cs script that we attach to a GameObject (inside a UI Canvas) with a RectTransforn, CanvasRenderer and UI Image component. The HealthBar.cs script will update the Image representing the HealthBar when the player’s health is changing:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0bd84776f2598f10e7fa8c53e0285ce/href">https://medium.com/media/b0bd84776f2598f10e7fa8c53e0285ce/href</a></iframe><p>Both Health and MaxHealth are global variables stored as .assets files that are (or could be) shared between scripts. To create these .assets files we can right click somewhere in the Project window, and go Create / Unity Atoms / Int / Variable. Variables look like this in the Unity Inspector:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*S6mLp7nZp0Ct6uF7Po_bzA.png" /></figure><p>The <em>Developer Description</em> is a text describing the Variable in order to document it, the <em>Value</em> is the actual value of the Variable, and <em>Old Value</em> is the last value the Variable had after it was changed via code. <em>Changed</em> and <em>Changed With History</em> will be explained in the Game Events section further down in this post.</p><p>We name the IntVariables created to Health and MaxHealth and set both their initial value (in this case 100). After they are created we can drop them on the PlayerHealth and HealthBar components via Unity’s inspector like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*OJ_j4SvpY76sPpIOQ94vGA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/678/1*fO-ua68kqpd-SVHbsA0iag.png" /></figure><p>Variables gives us a way of separating our game’s shared state from the actual implementation. It also makes our code less coupled since we do not need to reference other MonoBehaviours in our scripts, eg. we do not need to reference the PlayerHealth.cs script in our HealthBar.cs script like this:</p><pre>[SerializeField]<br>private Player player;</pre><p>Hurray for less coupled code! 🎉</p><h3>❗️Game Events</h3><p>Game Events are things that happens in our game that other scripts or entities could listen and subscribe to. Game Events are (like Variables) also Scriptable Objects that lives outside of a specific scene. In Unity Atoms Game Events can be of different types and thereby pass a long data to listeners. Variables do by default have the possibility to raise two specific Game Events:</p><ul><li><em>Changed</em> — raised every time a Variable’s value is changed. The Game Event contains the new value.</li><li><em>Changed With History</em> — also raised every time a Variable’s value is changed. However, this Game Event contains both the new and the old value.</li></ul><p>This makes it easier to make our game more data driven than just using Variables. Lets take a look at how that looks in our last example. We can create a new IntEvent as a .asset file by right clicking and go Create / Unity Atoms / Int / Event and name it HealthChangedEvent:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*J64yHlITwQ0afQvfGFPrcg.png" /></figure><p>And then drop it on our IntVariable for the player’s health like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*Dxhr4JxPNVAmHvuW3Z2Zmw.png" /></figure><p>We can then modify our HealthBar.cs script to look like this (do not worry about the IGameEventListener&lt;int&gt; stuff for now):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9d524df2fbce73adb8ae9285028c911/href">https://medium.com/media/b9d524df2fbce73adb8ae9285028c911/href</a></iframe><p>And then inject the HealthChangedEvent to our HealthBar component:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*pstDMvRIYUXRtTnSQErCtw.png" /></figure><p>We now react to global state changes instead of checking the Variable value each Update tick. In other words we only update our Image component when we actually need to. That is pretty sweet!</p><h3>👂Game Event Listeners</h3><p>There is still an issue that the HealthBar.cs script is in charge of registering itself as a listener and at the same time defining what happens when a Game Event is raised. We need to seperate its concerns! This brings us to the third concept of Unity Atoms, Game Event Listeners. A Game Event Listener listens (sometimes also referred to as observes or subscribes) to a Game Event and responds by firing off zero to many responses. Game Event Listeners are MonoBehaviours and therefore lives in a scene. They can be seen as the glue between Game Events and Responses (see the next section of this post).</p><p>The HealthBar.cs script from our last example is actually a Game Event Listener, but a very specific implementation of it. We can do better than that! Lets create a GameObject in our scene and call it HealthListener. Unity Atoms comes with some predefined Game Event Listeners. In this case we want to listen to an IntEvent so we will press the Add Component button on our HealthListener, create an IntListener and drop in the HealthChangedEvent:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*48gpUIpdCwXLqaaNwYdphg.png" /></figure><p>We can now shave off some of the code in our HealthBar.cs script to look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db6e7352e0077df1a2670ebbbabe40fd/href">https://medium.com/media/db6e7352e0077df1a2670ebbbabe40fd/href</a></iframe><p>And then go back to our HealthListener’s IntListener component, press the + to add an <em>Unity Event Response</em>, drop in the HealthBar component (from the scene) and under the dynamic dropdown section select the HealthChanged function defined above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*Mo9wfkvMO1iOsc1trnoLbg.png" /></figure><p>The HealthBar.cs script is now only responsible for what happens when our player’s health is changing. Pretty great, huh?</p><h3>💌 Responses</h3><p>The HealthChanged function created above is actually a Response of type <a href="https://docs.unity3d.com/ScriptReference/Events.UnityEvent.html">Unity Event</a>. However, in Unity Atoms there is also the possibility to create Responses as Scriptable Objects called Game Actions. A Game Action is a function as a Scriptable Object that does <strong>not</strong> return a value. As a side note there are also Game Functions in Unity Atoms, which are exactly like Game Actions but <strong>does</strong> return a value. To demonstrate the concept of a Game Action as a Response lets create a Game Action called HealthLogger.cs that gives some love to the console and logs the player’s health whenever it changes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4b217fe6dfe31e16c03ae065f19e90f/href">https://medium.com/media/c4b217fe6dfe31e16c03ae065f19e90f/href</a></iframe><p>It is possible to create the HealthLogger by right clicking and go Create / Unity Atoms / Examples / Intro / Game Actions / Health Logger (this is available due to the call to CreateAssetMenu). When created HealthLogger we can add it as a <em>Game Action Response</em> to the HealthListener:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*V7uwaAzo6pXQwTYoTYzJFw.png" /></figure><p>Every time the player’s health is changed we now log out the player’s health. This particular example is pretty simple, but I’m sure you can come up with lots of other use cases for it (for example play a sound or emit some particles).</p><p>That is it! We have covered the four most fundamental core pieces of Unity Atoms. Next we will dive into some other handy tools included in the library.</p><h3>🎣 Mono Hooks</h3><p>When you start thinking about this event-listener-response pattern you will soon realize that <strong>everything</strong> in your game can be expressed this way. The native <a href="https://docs.unity3d.com/Manual/ExecutionOrder.html">Unity lifecycle methods</a> can actually be thought of as events that we listen and react to. The bridge between Unity’s lifecycle methods and Game Events is called MonoHooks.</p><p>In our example we can use the MonoHook OnTriggerEnter2DHook to replace the Harmful.cs script completely. We will start by removing the Harmful.cs script from our project and remove its component on the Harmful GameObject. Next we will add two new components to the Harmful GameObject, OnTriggerEnter2DHook and Collider2DListener (both predefined in Unity Atoms). Then we will create a Collider2DEvent (Create / Unity Atoms / Collider2D / Event) and call it HarmfulTriggerEvent. We will drop the HarmfulTriggerEvent on the <em>Event</em> for both the OnTriggerEnter2DHook and Collider2DListener that we just created. Lastly we will create a new Collider2DAction called DecreasePlayersHealth.cs that looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f296acac6ce6f7e1a00fdd093d4b595b/href">https://medium.com/media/f296acac6ce6f7e1a00fdd093d4b595b/href</a></iframe><p>We can then create a DecreasePlayersHealth.asset file (Create / Unity Atoms / Examples / Intro / Game Actions / Decrease Players Health) and inject it as a <em>Game Action Response</em> on our Harmful GameObject:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*SYqP4tyuUdxiNmgJNprUlA.png" /></figure><p>We can also add the MonoHook OnStartHook to set the initial value of our Health variable when the scene starts. To do so we will start by creating a new GameObject in our scene and add a OnStartHook component and a VoidListener component to it (both included by default in Unity Atoms). Then we will create a Game Event of type void and add it to <em>Event</em> for both of the components. Finally we can create a predefined Game Action called SetIntVariable (Create / Unity Atoms / Int / Set Variable) that will actually set the value on our Health variable and name it SetPlayerHealth:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*xzPtynsaVnn0HMuQr9ckiQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*GTdagAZx2q-olI-1tk6Wdg.png" /></figure><p>This will properly set the player’s health to 100 when the scene starts.</p><p>Some further notes about MonoHooks: <em>Event With GO Ref</em> is a Game Event that will be raised just like the regular <em>Event</em>, but with a reference to the GameObject that has the OnStartHook component on it (by default). If <em>Select GO Ref</em> is defined we can select what GameObject will be referenced by the <em>Event With GO Ref</em>. This could be useful when we for example want to reference a parent or a child of the GameObject with the MonoHook component.</p><h3>©️ Constants</h3><p>A Constant is a stripped version of a Variable. It doesn’t contain any Game Events for when the value is changed neither the <em>Old Value</em> field. It is not possible to change a Constant’s value via code. The thought is to use Constants for global shared values that are not due to change, eg. tags, layer names, multipliers, etc. In our example it would be preferable for MaxHealth to be a constant:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aee10b499cd6d0edae0b7920a967b5d2/href">https://medium.com/media/aee10b499cd6d0edae0b7920a967b5d2/href</a></iframe><p>And to change the player tag in our DecreasePlayersHealth.cs script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98a3dc925537582e7192691829f4eb25/href">https://medium.com/media/98a3dc925537582e7192691829f4eb25/href</a></iframe><h3>®️ References</h3><p>References can be toggled between <em>use constant</em> or <em>use variable</em> via the Unity Inspector. When a reference is set to <em>use constant</em> it functions exactly like a regular serialized variable in a MonoBehaviour script. However, when it is set to <em>use variable</em> it functions exactly like a Variable. This is something that is copied directly from Ryan Hipple’s Unite 2017 talk.</p><h3>📋 Lists</h3><p>A list is an array of values that is stored as a Scriptable Object. There is a possibility to add Game Events to a List when the following happens:</p><ul><li>An item is added to the list.</li><li>An item is removed from the list.</li><li>The list is cleared.</li></ul><p>Lists are perfect for storing data or references to Game Objects that need to get tracked and created at runtime.</p><h3>📇 Some Final Words</h3><p>Unity Atoms is open source and can be downloaded from Github and used in your project by going <a href="https://github.com/AdamRamberg/unity-atoms/releases">here</a>. The project contains the final version of the <a href="https://github.com/AdamRamberg/unity-atoms/tree/master/Assets/UnityAtoms/Examples/Intro">example</a> used throughout this post.</p><p>The idea from here on is to add more features and more general Game Actions and Game Functions that could be reused in not only my current game, but in future ones or yours as well. If it is possible to reuse well tested modular pieces of code proven to work instead of writing all functionality from scratch in your game it will result in less bugs (less code = less bugs). Some examples of reusable snippets that I have already created are:</p><ul><li><a href="https://github.com/AdamRamberg/unity-atoms/blob/master/Assets/UnityAtoms/GameFunctions/GetUnusedGameObject/GetUnusedGameObject.cs">GetUnusedGameObject</a> — A Game Function that queries a GameObject List for an unused GameObject. If it finds one it returns it, otherwise it <a href="https://docs.unity3d.com/ScriptReference/Object.Instantiate.html">Instantiate</a> a new one using a given prefab and adds it to the list.</li><li><a href="https://github.com/AdamRamberg/unity-atoms/blob/master/Assets/UnityAtoms/GameActions/ChangeScene/ChangeScene.cs">ChangeScene</a> — A Game Action that loads a scene by string name.</li></ul><p>I hope that you have enjoyed reading this blog post and that you after reading it have realized all the great things that are possible using Scriptable Objects. Would love to hear what you think about this post in the comments below. Thanks for reading! 💚 Give me some 👏 and follow me on <a href="https://twitter.com/adamramberg1">twitter</a> if you liked what you just read. Cheers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8add1b95201" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up a React app from scratch]]></title>
            <link>https://medium.com/@adamramberg/setting-up-a-react-app-from-scratch-42521a118b10?source=rss-8834103955f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/42521a118b10</guid>
            <category><![CDATA[babel]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[eslint]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Adam Ramberg]]></dc:creator>
            <pubDate>Sun, 22 Jul 2018 09:01:44 GMT</pubDate>
            <atom:updated>2018-10-19T06:22:44.185Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O4tiUQhYBrQ9l_XpRl9Gbw.png" /></figure><p><em>Before starting, the final repo can be found </em><a href="https://github.com/AdamRamberg/react-from-scratch"><em>here</em></a><em> on Github.</em></p><p>For the creation of my new website I wanted to skip using one of the many great boilerplate repos out there (for example <a href="https://github.com/react-boilerplate/react-boilerplate">React Boilerplate</a> or <a href="https://github.com/facebook/create-react-app">Create React App</a>) and instead setting everything up myself. The main reason was that I wanted to dive deeper and get a better understanding of all the parts needed. Learning how frameworks and libraries works and being able to put them in a larger context is something that I really enjoy and is one of the things that gets me motivated and makes me grow as a developer.</p><p>There are many different choices of what techniques to use when setting up an app like this, and its therefore also a large number of different personal preferences of what to use out there. For that reason I will show how to setup a bare-boned base using <a href="https://reactjs.org/">React</a>, <a href="https://webpack.js.org/">webpack</a>, <a href="https://babeljs.io/">Babel</a>, <a href="https://eslint.org/">ESLint</a> and <a href="https://prettier.io/">Prettier</a>. From there it is easy to add the libraries you want to work with. But enough with the mumbo jumbo, lets jump into the steps to setup a React app from scratch!</p><p><em>Prerequisites: You need to install yarn and node on your machine in order to follow along this post. You will also need a code editor (for example VSCode).</em></p><h3>Let there be light</h3><p>The first thing we need to do is to create a directory for your project. Open your terminal and change directory to where you want your project to reside. Then create your project directory (changing “react-from-scratch” to the name of your project) and initialize a new package.json:</p><pre>mkdir react-from-scratch &amp;&amp; cd react-from-scratch &amp;&amp; yarn init -y</pre><h3>Linting and code formatting</h3><p>Linting and code formatting is a great way to improve the quality of the code you write, keep a common code style throughout the project and to prevent unnecessary bugs. It is a good idea to add linting and code formatting the first thing you do, so that everything created afterwards is styled and formatted correctly right away. We will use <a href="https://eslint.org/">ESLint</a>, <a href="https://prettier.io/">Prettier</a> and <a href="https://editorconfig.org/">EditorConfig</a> to aid us with this. ESLint is the most popular linter for javascript while Prettier is an opinionated code formatter. Prettier helps us with code style while ESLint will mainly help with errors in the code (but can also check for code style). Since both Prettier and ESLint can check for code styles it is important to be aware that it is possible to configure them with conflicting rules. Lastly, the EditorConfig file will define configuration for the editor itself. <a href="https://stackoverflow.com/questions/48363647/editorconfig-vs-eslint-vs-prettier-is-it-worthwhile-to-use-them-all">Here</a> is a great post on StackOverflow explaining the differences between ESLint, Prettier and EditorConfig and how they work together.</p><p>Start by creating an .editorconfig file in the root folder:</p><pre>touch .editorconfig</pre><p>Note that some of the settings in this file will be used by Prettier and will take precedence over Prettier’s own config, for example indent_style and indent_size. Add the configurations that you want to your .editorconfig file using the <a href="https://editorconfig.org/">docs</a>. My config ended up like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/baa9934670226e717f5ed665003d62b8/href">https://medium.com/media/baa9934670226e717f5ed665003d62b8/href</a></iframe><p>Continue by installing ESLint, Prettier and some useful plugins / configs (like Airbnb’s ESLint rules) as dev dependencies to the project:</p><pre>yarn add eslint prettier eslint-config-airbnb eslint-config-prettier eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-prettier eslint-plugin-react babel-eslint --dev</pre><p>Next we will add a prettier config file:</p><pre>touch prettier.config.js</pre><p>Add the options that you want by looking at the prettier <a href="https://prettier.io/docs/en/options.html">docs</a>. Export the options as an object in the prettier.config.js file. I used these settings:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e3ac81f5a9c7df7438cb5e1da590945/href">https://medium.com/media/6e3ac81f5a9c7df7438cb5e1da590945/href</a></iframe><p>Continue by adding an ESLint config file:</p><pre>touch .eslintrc.js</pre><p>This file will define all the linting rules in the project. The config file will also define that we will let ESLint run Prettier for us. Prettier’s <a href="https://prettier.io/docs/en/eslint.html">website</a> contains great information about integrating with ESLint. The important thing here is to add the Prettier plugin, import the prettier config created above and pass it to the rules section of the config. It is also a great idea to extend the eslint-prettier-config in order to disable conflicting rules between Prettier and ESLint. Continue by extending the Airbnb and React configs, add the React and jsx-a11y plugins, and add any other ESLint rules you want to use. I created my .eslintrc.js file by basing it of <a href="https://github.com/react-boilerplate/react-boilerplate/blob/master/.eslintrc.js">react-boilerplate’s eslint config</a>. It ended up like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3054b0117dc2c7d606e3cec4c6de95d/href">https://medium.com/media/c3054b0117dc2c7d606e3cec4c6de95d/href</a></iframe><p><em>Tip: If you are using VSCode and are having troubles getting it setup then </em><a href="https://www.youtube.com/watch?v=YIvjKId9m2c"><em>this</em></a><em> is a great video to help you get it working properly.</em></p><h3>Bundling with webpack</h3><p>I chose to use webpack for bundling my React app, mainly because it is one of the most widely used out there. Start by installing the following dev dependencies:</p><pre>yarn add webpack webpack-cli --dev</pre><p><em>Side note: For a smaller web app a great alternative to webpack is </em><a href="https://github.com/parcel-bundler/parcel"><em>Parcel</em></a><em>. It has gained a lot of attention for its zero-configuration, something that webpack later adopted in webpack 4.</em></p><p>To test that bundling works create a folder called src/ and add a file called index.js. This file will serve as the entry point for the app.</p><pre>mkdir src &amp;&amp; touch ./src/index.js</pre><p>Add a simple console.log statement to index.js and save the file. If you run one of the following commands webpack will now bundle your app using its default configuration for either development or production mode:</p><pre>webpack --mode development<br>webpack --mode production</pre><p>Go to the generated dist/ folder in the root of your project to take a look at the result of the bundling. Neat, we have now setup basic bundling with webpack!</p><h3>Add React</h3><p>Lets add React and React Dom as dependencies to your project:</p><pre>yarn add react react-dom --save</pre><p>In the index.js created in the last section remove the console.log statement and create a minimal React component that looks something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c91b78b9c0abfaa10ee8d8ad6890417d/href">https://medium.com/media/c91b78b9c0abfaa10ee8d8ad6890417d/href</a></iframe><p>Continue by creating an index.html file in the src/ folder.</p><pre>touch ./src/index.html</pre><p>Add the following markup to the index.html file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da7d9312854b834dfbe182dcb7e08f6e/href">https://medium.com/media/da7d9312854b834dfbe182dcb7e08f6e/href</a></iframe><p>Great you have created an entry point for your app that will render a super simple React component! However, before we can test the component we will need to setup Babel and use it together with webpack.</p><h3>Setup Babel</h3><p>Babel is used to transpile your javascript to syntax the target browsers will understand. For this setup we will use the new. Lets add the following dev dependencies:</p><pre>yarn add @babel/core babel-loader @babel/preset-env @babel/preset-react --dev</pre><p>…and babel-polyfill as a regular dependency :</p><pre>yarn add @babel/polyfill --save</pre><ul><li><strong><em>@babel/core</em></strong>: Is doing the actual transformation of the code.</li><li><strong><em>babel-loader</em></strong>: Transpiling javascript files using babel-core and webpack.</li><li><strong><em>@babel/preset-env</em></strong>: A preset that lets you only include the polyfills and transforms needed for the browsers your app will support.</li><li><strong><em>@babel/preset-react</em></strong>: A preset for React plugins that for example transform JSX to function calls.</li><li><strong><em>@babel/polyfill</em></strong>: A polyfill that can be used to emulate a full ES2015+ environment.</li></ul><p><em>If you are interested checkout </em><a href="https://babeljs.io/docs/en/next/plugins"><em>this</em></a><em> for further information about Babel plugins / presets.</em></p><p>Next we will create a configuration file for Babel. The configuration file will include all presets and plugins that we want to use, as well as options for the presets and plugins. Note that Babel 7 adds support for using javascript for the configuration file instead of JSON.</p><pre>touch babel.config.js</pre><p>Add the following to the configuration file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/648da6b437c47a0bcd90fad28b52ec3c/href">https://medium.com/media/648da6b437c47a0bcd90fad28b52ec3c/href</a></iframe><p>The call to api.cache.invalidate tells Babel how we want to cache the configuration and will recreate plugins and discard the previous instance whenever something changes. We then specify the presets that we want to use (preset-env and preset-react). For preset-env we set useBuiltIns to usage, which means that Babel will polyfill features (using babel-polyfill) if they are used in the code and if they are not supported by the target environments (see below).</p><p>We also want to specify the browsers that we want to support. This can be done in the babel.config.js file, but it is recommended to use a .browserlistrc since it could be used by other packages. Lets create it:</p><pre>touch .browserlistrc</pre><p>Then add the following query to the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7332fa19b066878d92dbfb005678bc85/href">https://medium.com/media/7332fa19b066878d92dbfb005678bc85/href</a></iframe><p>The query says that we want to support browser with a global usage percent greater than 0.25% and that are not dead. Here dead means that a browser has been without official support or updates for at least 24 months. <a href="http://browserl.ist/">Browserl.ist</a> is a great resources for figuring out queries for your target browsers.</p><h3>Custom webpack config</h3><p>Even though webpack 4 does not require a config by default it gives one much more control over what is going on, like defining that we want to use Babel together with webpack. Start by installing the dev dependency <a href="https://github.com/jantimon/html-webpack-plugin">html-webpack-plugin</a>, which will be used to inject our javascript bundles to our index.html template created before.</p><pre>yarn add html-webpack-plugin --dev</pre><p>Lets continue my actually making the config files. In the root of your project, create a folder called internals and then a subfolder called webpack.</p><pre>mkdir internals &amp;&amp; cd internals &amp;&amp; mkdir webpack &amp;&amp; cd webpack</pre><p>We will then create 3 files: webpack.base.js, webpack.dev.js and webpack.prod.js.</p><pre>touch webpack.base.js webpack.dev.js webpack.prod.js</pre><p>The reason behind creating 3 different configs is that we want to create different configurations for production and development, but also share some configuration that are common between the two (webpack.base.js). First of, the base config will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5b3ca5efa1322b9e233d7b7b29b250e/href">https://medium.com/media/d5b3ca5efa1322b9e233d7b7b29b250e/href</a></iframe><p>The config exports a function that takes options as a parameter (here we will later pass our development or production config) and returns a webpack config object. Here is an explanation of the different configuration options:</p><ul><li><strong><em>mode</em></strong>: Tells webpack to optimize for development or production (default).</li><li><strong><em>entry</em></strong>: The entry point of the application.</li><li><strong><em>module</em></strong>: Defines how different modules in the project will be treated. Here we specify that we want to run and transform all javascript files in the project, excluding files in node modules, through Babel.</li><li><strong>devServer</strong>: Options used by <a href="https://github.com/webpack/webpack-dev-server">webpack-dev-server</a>.</li><li><strong><em>plugins</em></strong>: List of plugins that customize the webpack build process. Here we add html-webpack-plugin, which will inject a script tag referencing the main bundle to index.html.</li></ul><p>Continuing, the webpack.dev.js will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c59e6e0e4b46197033749c8d35d950e/href">https://medium.com/media/5c59e6e0e4b46197033749c8d35d950e/href</a></iframe><p>Here we use the base config file and specify mode to development, pass options to the dev server (see below) that we want Hot Module Replacement (HMR) and add the actual plugin for hot module replacement. HMR exchanges, adds and removes modules while an application is running without a full reload, which speeds up development vastly. More info on HMR can be found <a href="https://webpack.js.org/concepts/hot-module-replacement/">here</a>.</p><p>The initial setup for the production config looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e471704140751d84b0c7828a60520ff8/href">https://medium.com/media/e471704140751d84b0c7828a60520ff8/href</a></iframe><p>Here we change mode to production, remove HMR from the dev server and add dist/ as the content base for the dev server, which means that the server will serve the static files from there. When defining content base like this a recompile will trigger when files in the src/ folders are modified and saved. At this point the advantage of the split between the development and production configs might not be crystal clear, but it will become useful while the project grows and we want to add new webpack plugins to the configs.</p><p>It is now possible to use the configs when bundling by running the following commands (from the root of the project):</p><pre>webpack --config internals/webpack/webpack.dev.js<br>webpack --config internals/webpack/webpack.prod.js</pre><h3>Server</h3><p>We need to setup a development server when developing the app. webpack provides a development server called <a href="https://github.com/webpack/webpack-dev-server">webpack-dev-server</a> that provides live reloading. Lets install it:</p><pre>yarn add webpack-dev-server --dev</pre><p>It is now possible to run the app on port 3000 with HMR (specified in the webpack.dev.js) by issuing the following command:</p><pre>webpack-dev-server --config internals/webpack/webpack.dev.js</pre><p>Or run the app in production mode:</p><pre>webpack-dev-server --config internals/webpack/webpack.prod.js</pre><h3>Add scripts to package.json</h3><p>It is a good idea to add the commands mentioned above to the script tag in your package.json. This makes it possible to run them using yarn &lt;commmand&gt;, eg. run yarn start to run the app in development mode. I added scripts for start, start:prod and build to my package.json.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc02d8db278b1d345acfa77b09d2fbeb/href">https://medium.com/media/bc02d8db278b1d345acfa77b09d2fbeb/href</a></iframe><h3>Where to go from here</h3><p>Hurray! 🎉 We have successfully created a very basic setup for a React app using webpack, Babel, Prettier and ESLint. This is of course a very bare-boned setup, but this is a good base to start from. Below I will provide some suggestion on where to go from here.</p><h4>Styling</h4><p>The most straight forward way of adding styling to your app is to install <a href="https://github.com/webpack-contrib/css-loader">css-loader</a> and <a href="https://github.com/webpack-contrib/style-loader">style-loader</a> as dev dependencies and add them to your webpack base config. I did left this out since I prefer using <a href="https://github.com/styled-components/styled-components">Styled Components</a> for styling, which is a popular CSS in JS framework.</p><h4>Routing</h4><p>I would suggest using <a href="https://github.com/ReactTraining/react-router">react-router</a> for routing in your app. Its using a concept called “Dynamic Routing” instead of the more classic “Static Routing”. More info can be found <a href="https://reacttraining.com/react-router/core/guides/philosophy">here</a>.</p><h4>State management</h4><p>When it comes to a global state container <a href="https://redux.js.org/">Redux</a> is one of the most solid choices. My suggestion is to wait using Redux until you actually need it instead of throwing it in your app right away.</p><h4>Unit testing</h4><p><a href="https://jestjs.io/">Jest</a> is a great choice for unit testing. It is developed by Facebook so it is working well together with React. <a href="https://jestjs.io/docs/en/tutorial-react">Here</a> is a guide on how to set it up in a React app.</p><p><em>Like mentioned in the beginning of this post, the full project can be found </em><a href="https://github.com/AdamRamberg/react-from-scratch"><em>here</em></a><em> on Github.</em></p><p>That’s it! Slap that clap button if you found this post useful. Thanks for reading ❤</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42521a118b10" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>