<?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 Micha Davis on Medium]]></title>
        <description><![CDATA[Stories by Micha Davis on Medium]]></description>
        <link>https://medium.com/@micha-l-davis?source=rss-8649ae2216b1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1TI7zIXSxXs8oQ-nAj5WNg.jpeg</url>
            <title>Stories by Micha Davis on Medium</title>
            <link>https://medium.com/@micha-l-davis?source=rss-8649ae2216b1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 03:15:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@micha-l-davis/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[Building a Linked List System From Scratch in C#, Part 2]]></title>
            <link>https://medium.com/geekculture/building-a-linked-list-system-from-scratch-in-c-part-2-a5eaf83cf9cc?source=rss-8649ae2216b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5eaf83cf9cc</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Micha Davis]]></dc:creator>
            <pubDate>Wed, 22 Sep 2021 21:02:59 GMT</pubDate>
            <atom:updated>2021-09-23T00:25:30.217Z</atom:updated>
            <content:encoded><![CDATA[<h4>Inserting Values Mid-Chain and Finding Specific Values</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*2Lg6Wf5aZNmDkFt6" /><figcaption>=[[Value]]==[[Value]]==[[Value]]==[[Value]]==[[Value]]=</figcaption></figure><p><a href="https://medium.com/geekculture/building-a-linked-list-system-from-scratch-in-c-part-1-51aa6c68ea19">Last time</a> we covered establishing the foundations for a doubly linked list class: namely, the ability to create nodes of data with references to previous or successive nodes to form a series. Right now we can add to either end of the chain with our <em>AddFirst(T t) </em>and <em>AddLast(T t) </em>methods. Today we’ll look at inserting data into the middle of the chain based on some reference point.</p><p><strong>A quick reminder of two things.</strong> First: I’m using Unity to develop and test this system. If you’re following along in a different development environment, you may need to adjust how errors are handled and tests are run. Second: this functionality all exists in C# already. We’re reverse-engineering it for educational purposes, not for practical purposes.</p><h4>The <em>AddBefore</em> and AddAfter Methods</h4><p>This next part can get a bit dizzying. We’re going to be using the words ‘next’ and ‘previous’ in varying combinations, and by the end it can get difficult to track. Just keep in mind that four values need to change to add a node mid-chain (two next and two previous). In the diagrams below I add a new arrow each step to show how variables are being assigned — hopefully that helps.</p><p>We’ll start with <em>AddAfter(Node node, T t)</em>. To add a node after a specified node, we have to do the following:</p><ul><li>Null check the specified node, and return an error if it is null. We can’t do anything with a null reference.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xLCyzAG3JlTfPo2C.jpg" /><figcaption>Today you get paper diagrams instead of digital diagrams because I’m away from my drawing tablet.</figcaption></figure><ul><li>Create a new instance of the <em>Node(T t)</em> class and increase the list’s <em>Count </em>property.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Hr0YCIHtxpeMaepk.jpg" /><figcaption>A new node appears!</figcaption></figure><ul><li>Set the new node’s <em>Next</em> property to the node referenced by the specified node’s <em>Next </em>property.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*92NS-mwV4wpqhAFx.jpg" /></figure><ul><li>Set the specified node’s <em>Next</em> property to the new node.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TAoqO5cBtX4p9i0J.jpg" /></figure><ul><li>Set the new node’s <em>Previous</em> property to the specified node.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-589RpraMMu-UJ4y.jpg" /></figure><ul><li>If the new node’s new <em>Next </em>property isn’t null (meaning we didn’t just add a tail node) we set the <em>Previous </em>property of the node referenced by the new node’s <em>Next</em> property to the new node.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vOGtmcXRKc1hqt2y.jpg" /></figure><p>All that reference juggling should result in the new node now resting right after the specified node in the chain, fully connected to its new neighbors. If we were instead trying to <em>AddBefore(Node node, T t)</em>, we would reverse the properties in the last four steps:</p><ul><li>Set the new node’s <em>Previous</em> property to the node referenced by the specified node’s <em>Previous</em> property.</li><li>Set the specified node’s <em>Previous</em> property to the new node.</li><li>Set the new node’s <em>Next</em> property to the specified node.</li><li>If the new node’s new <em>Previous </em>property isn’t null (meaning we didn’t just add a head node) we set the <em>Next </em>property of the node referenced by the new node’s <em>Previous </em>property to the new node. Otherwise, we assign the new node to the <em>_head</em> variable.</li></ul><p>In code, these methods look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0thDC8K0QKenbVCyPuODXg.png" /><figcaption>I know “newNode.Next.Previous” is cumbersome. If you have an easier way to reference this, drop a comment!</figcaption></figure><h4>The Contains, Find, and FindLast Methods</h4><p>The next step is to build some methods that allow us to query the contents of our list. Each of these methods works on the same general idea, but with slightly different results.</p><p><em>Contains(T t)</em> is a bool return method. It starts with the first node, and then steps through each node until it reaches the end of the chain. Each step along the way, it checks to see if the value of the node equals the argument supplied in the method call. If it does, it returns true. Otherwise, false.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*34RBQS-ui-EWnUqaVGCNow.png" /></figure><p><em>Find(T t)</em> is a <em>Node </em>object return method. It has exactly the same code as Contains, except when it finds a matching value it returns the node containing that value. This will only ever find the first instance of a given value; successive duplicate instances will be ignored.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yNPrJSvFq-NzDbMxrWthng.png" /></figure><p><em>FindLast(T t) </em>is a <em>Node</em> object return method. It begins its search at the last node, and then steps backward through the chain until it finds a node with a matching value, which it returns. This will only ever find the last instance of a given value; preceding duplicate instances will be ignored.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pp4CA4M5Cn9AUMQ-wjkG9Q.png" /></figure><h4><strong>Testing the New Methods</strong></h4><p>Before we can test the <em>AddBefore </em>and <em>AddAfter </em>methods, we’ll need to make sure the search methods work.</p><p>To test <em>Contains(T t)</em> we’ll create a test data array containing the numbers 0–9 and search for three values: the first value, the last value, and a value not on the list (just in case).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hnP7RI7i9hECdsIB_eZI8A.png" /></figure><p>The test for <em>Find(T t) </em>will need a unique test data array so that we can make sure we find the first instance of multiple matching results. First we’ll make sure we’re finding any valid number at all. Then, to make sure we are finding the correct value, we’ll compare the value in the found node to the value next in the chain. In this case, it should be 5. We’ll also make sure we can’t find a value that isn’t in the array (just in case).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VpcdwbkBIGnS5CEhPlTkPg.png" /></figure><p>And for <em>FindLast(T t)</em> we simply invert the search terms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1w-UPIFy5OmSbUS86RL-tg.png" /></figure><p>Let’s confirm those operate as expected:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*_cHp850l8e_KMVxeOZrVag.png" /></figure><p>Splendid!</p><p>Now, for <em>AddBefore</em> we want to check that we can <em>AddBefore </em>the first node, and it will replace the <em>_head</em>, and then we’ll check that we can add a node prior to the last node, and we’ll find a node in the middle and check that we can insert there as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zTSO2GvO_yEYEUhsnFqguA.png" /><figcaption>NOW there’s a 10 in there.</figcaption></figure><p>Finally, let’s run a similar confirmation of <em>AddAfter</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f78sf5Y18U9bGvnirhgRbA.png" /></figure><p>And the results?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*tdZFjqULGCse9EA0sbnEkg.png" /></figure><p>We did it!</p><p>That completes our goals for today. In part three we’ll cover removing values from the list, and we will consider some extra features Microsoft didn’t see fit to include in their implementation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5eaf83cf9cc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/building-a-linked-list-system-from-scratch-in-c-part-2-a5eaf83cf9cc">Building a Linked List System From Scratch in C#, Part 2</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Linked List System From Scratch in C#, Part 1]]></title>
            <link>https://medium.com/geekculture/building-a-linked-list-system-from-scratch-in-c-part-1-51aa6c68ea19?source=rss-8649ae2216b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/51aa6c68ea19</guid>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Micha Davis]]></dc:creator>
            <pubDate>Fri, 17 Sep 2021 19:33:24 GMT</pubDate>
            <atom:updated>2021-09-17T19:33:24.850Z</atom:updated>
            <content:encoded><![CDATA[<h4>A fun exercise for practice juggling variable references</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yXCvVVXGoJaJyoa4qllo7Q.png" /></figure><p>I have written about the <a href="https://medium.com/geekculture/roll-initiative-part-2-d7ab2d82ef27">usefulness of Linked Lists</a> in the past. Today, I want to get into the mechanics of how a linked list works by creating my own implementation of linked list functionality.</p><p>NOTE: Linked Lists <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.linkedlist-1?view=net-5.0">are already implemented in C#</a>. This exercise will duplicate that functionality, but it is not at all necessary for you to implement your own version of this for your work. I did this as an exercise in understanding how some features might work, for educational purposes not for practical purposes.</p><h4>What is a Linked List?</h4><p>The title image gives the gist of how a linked list operates. Rather than a simple list of values, a linked list maintains references to other values. This forms a chain of “links” between the values. The image above represents a “doubly linked list,” in that each value node references the nodes before AND after it, making traversal in either direction simple. Singly linked lists (which point only to the next node in the chain), and circular linked lists (in which the last node points to the first node, forming a circle) are two other kinds of linked lists.</p><p>Because of the chain of references, it is easy to insert or remove values from the middle of the list with no added complexity. Similar operations on traditional lists would require iterating through the entire list, but with a linked list we can accomplish the same result without any iteration at all.</p><h4>The Doubly Linked List</h4><p>For the next several articles we’ll be creating a DoublyLinkedList&lt;T&gt; class. The goal is to reverse engineer each of the the public methods and properties of C#’s <em>LinkedList&lt;T&gt;()</em> class. Namely:</p><ul><li><em>First</em> and <em>Last</em> properties pointing to the respective ends of the chain.</li><li><em>Count</em> property to reference the number of links in the chain.</li><li><em>AddFirst(T t)</em> and <em>AddLast(T t)</em> methods, to add new nodes at the ends of the chain.</li><li><em>AddBefore(Node node)</em> and <em>AddAfter(Node node)</em> methods, to add nodes somewhere between the ends of the chain.</li><li><em>Remove(Node node)</em>,<em> RemoveFirst()</em>, and <em>RemoveLast()</em> methods, which nullify the references to a specific node in the chain and close the gap in the chain, if necessary.</li><li><em>Contains(T t)</em> bool return method to find if a value exists in the chain.</li><li><em>Find(T t)</em> and <em>FindLast(T t)</em> methods for searching from either end of the chain.</li><li><em>CopyTo(T[] array, int index)</em> method to copy the values from each node into an array at a specific index of the array.</li></ul><h4>The Node Class</h4><p>We start with the <em>Node</em> class. This class defines the necessary properties of an element in our linked list, and provides the constructor for creating new nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1hrjwSbmBSwKCIKfDNx7zw.png" /></figure><p>This class goes inside the <em>DoublyLinkedList&lt;T&gt;</em> class, for easy reference. The constructor accepts a single argument representing the intended value of the node. The adjacent node reference properties (<em>Previous</em> and <em>Next</em>) begin as null.</p><h4>The DoublyLinkedList&lt;T&gt; Class</h4><p>Now that we have nodes defined, we can create some properties and fields within the <em>DoublyLinkedList&lt;T&gt;</em> class itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KtUvY14PX_iV_fn_954sLg.png" /></figure><p>Here we first define the front of the list to be a Node variable called <em>_head. </em>The <em>First</em> property naturally needs to point to this field. To find the <em>Last</em> node requires some logic, which we will get to in the next section (for now, we can tolerate some red squiggles).</p><p>The <em>Next </em>and <em>Previous </em>properties in this class are then set to point at the head node’s <em>Next </em>and <em>Previous </em>properties.</p><p>Finally, we define a <em>_count </em>field with a counterpart property containing logic to constrain the count to above 0. We only want this class instance to set the value of its count, so we make the setter private.</p><h4><strong>The GetLastNode() Method</strong></h4><p>In order to get the <em>Last</em> property working, let’s start with the <em>GetLastNode()</em> method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uv3eH1N_o35_Lzr7ZQQfmA.png" /></figure><p>Here we simply start at the _head and move forward one node at a time until the Next node is null (meaning there are no more nodes in the chain). Once we have that, we return the node. Easy!</p><h4><strong>The AddFirst(T t) and AddLast(T t) Methods</strong></h4><p>Now, how about we actually create some nodes?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aWGiufCavReXrM-x2AUAEA.png" /></figure><p>To add a node, we pass the method’s argument into the <em>Node</em> constructor and increment the <em>Count </em>property.</p><p>If this node is meant to be the new head, we first check if the current head is null. If it isn’t, we point our new node’s <em>Next</em> property at the head node, and the head node’s <em>Previous </em>property at the new node. Finally, whether the head was null or not, we make the new node the head.</p><p>If this node is meant to be the new tail, we first check and see if the head is null — if it is, then the list is empty and we only need to designate the new node as the head. Otherwise, we’ll need to run the<em> GetLastNode()</em> method. Once we have the last node, we can assign the new node to the last node’s <em>Next </em>property and then assign the last node to the new node’s <em>Previous</em> property. This effectively makes the new node the last node.</p><h4>Testing the Class So Far</h4><p>We now have enough basic functionality to run a few unit tests and make sure everything is working. If you need to know how to set up unit testing in Unity, be sure to check out <a href="https://medium.com/geekculture/c-unit-testing-in-unity-80d8bbfc62bb">my article on the topic</a>.</p><p>Since I know I’ll be testing all of my Doubly Linked List methods I’ll start by creating a helper method to build testing data. This method will create an array of 10 integers with values from 0–9.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xYY1jDWSdWt8k2S7dEACwQ.png" /></figure><p>For the first test method we will test <em>AddLast(T t)</em>, <em>Count, </em>and <em>Next </em>since they all come into play when creating a new list. I’ll create some test data and then attempt to <em>AddLast(T t)</em> until all the data from the array is in the linked list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X6Y6V6n4fP7D71MxUgj_UA.png" /></figure><p>Along the way, we will check that the value added matches the expected value. We will also compare the Count property to the length of the array to make sure we’re properly tracking nodes as they are added.</p><p>Finally, we’ll call one more helper method to compare the test data array to the test list. We’ll end up using this method for <em>almost </em>all of our future tests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-DDHRMOLzKztTIs5yE8a5w.png" /></figure><p>As we iterate through the indices we’ll check if we’ve reached an unexpected null node before the end of the array. If that goes smoothly, we’ll then compare the test data at the current index to the current node. If they match, we move to the next node and iterate the index. Otherwise the <em>Assert()</em> method will tell us where the data doesn’t match.</p><p>We’ll also make a similar test method to check that the <em>AddFirst(T t)</em> class and <em>Previous</em> property work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VfIra6VYhMimwqeEtvrfew.png" /></figure><p>Once again, we build the test data and use it to populate a list. We make sure as we do, that the data matches what we expect. Rather than use the forward progressing helper we created above, we’ll need to progress backwards through this list to confirm the data matches. While we are iterating through indexes, we make sure the list is also iterating apace. Finally, we confirm the values of the nodes match the respective indices of the array and that the <em>Previous </em>property works as intended.</p><p>Now, let’s see how those tests turn out:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*xX0JBs0FcUkzq399EuFtvA.png" /><figcaption>Hooray!</figcaption></figure><p>That’s it for this first part. In the next article we’ll tackle <em>AddBefore(Node node), AddAfter(Node node) </em>and our three search methods: <em>Contains(T t), Find(T t),</em> and<em> FindLast(T t).</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=51aa6c68ea19" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/building-a-linked-list-system-from-scratch-in-c-part-1-51aa6c68ea19">Building a Linked List System From Scratch in C#, Part 1</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using C# Generic Types With Scriptable Objects in Unity]]></title>
            <link>https://micha-l-davis.medium.com/using-c-generic-types-with-scriptable-objects-in-unity-340cfc32811f?source=rss-8649ae2216b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/340cfc32811f</guid>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Micha Davis]]></dc:creator>
            <pubDate>Wed, 25 Aug 2021 16:41:01 GMT</pubDate>
            <atom:updated>2021-08-26T03:07:50.558Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using Class Inheritance to Pull The Wool Over Unity’s Eyes</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*wKIhQJCirUxonoTnYjLbmg.png" /><figcaption>Technically untrue.</figcaption></figure><p>I recently watched a brilliant presentation on <a href="https://youtu.be/raQ3iHhE_Kk">Game Architecture with Scriptable Objects</a> from Ryan Hipple, where he demonstrated a useful pattern he calls the Runtime Set. It’s a useful way to keep track of all of the active objects of a given type in the scene. If you’ve ever needed to keep track of how many enemies remain in a wave, or which buildings have been built in a player’s warcamp, this is a great way to have that information tracked and available to any class or method that wants it without hard dependencies or extra code.</p><p>The tricky part is, he doesn’t fully explain the implementation of the Runtime Set. He just shows a simplified version of the core object. Here’s the code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pxcg2k_NL21o4XzhOC14SQ.png" /><figcaption>Ryan Hipple, Unite Austin 2017</figcaption></figure><p>This is great, right? If I want to keep track of all the actors in my tactical rescue game, all I need is to create a new .asset out of a specific type of this generic. Like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ci_N_hUGE7n5DXnjPcPkLQ.png" /></figure><p>But if you saw the spoiler at the top… no. Unity doesn’t like that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*wKIhQJCirUxonoTnYjLbmg.png" /></figure><p>Generic MonoBehaviours are not supported?! This isn’t a MonoBehaviour at all! What sorcery did Ryan Hipple employ to make this work?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EKu4oqJRYesAQi-iezbUew.gif" /><figcaption>WTH, Unity?!</figcaption></figure><p>Okay, clearly we’re going to need a different approach. Unity doesn’t want to create an instance of a generic class, so we’ll have to dress our generic class in sheep’s clothing.</p><p>Let’s create a costume. It doesn’t have to be complicated — just enough of a exterior façade to fool Unity into giving us what we want.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ls5mJZhelOcvNe0Di7_1ag.png" /><figcaption>Hooooowwwll — er, I mean… Baaa!</figcaption></figure><p>Wow. Yeah, it’s that simple. <em>ActorRuntimeSet</em> inherits all the methods and fields of its parent class— in this case, the no-longer generic <em>RuntimeSet&lt;Actor&gt;. </em>So, there’s no need for a body. We’ve already defined that.</p><p>Now, we can create our .asset with the modified code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LHfBtmH2Di6hdXM-PQcpbg.png" /></figure><p>And here’s the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E1m9CJMJTUNYdErh_-vNBg.png" /></figure><p>I can tuck this asset into a serialized field on my Actor prefabs, and then instruct them to add and remove themselves from the list whenever they are enabled or disabled:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5LubXOW5uUItJ6DJMUZ4lg.png" /></figure><p>That’s the gist: by inheriting a generic class to a standard class you can sneak generic types under Unity’s notice. Once you have an asset built you can let any object refer to it to get an updated list of the items the list is built to track.</p><p>Can you think of other ways to use this trick? Drop a comment below and let me know!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=340cfc32811f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to C# Unit Testing in Unity]]></title>
            <link>https://medium.com/geekculture/c-unit-testing-in-unity-80d8bbfc62bb?source=rss-8649ae2216b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/80d8bbfc62bb</guid>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Micha Davis]]></dc:creator>
            <pubDate>Tue, 24 Aug 2021 18:51:18 GMT</pubDate>
            <atom:updated>2021-08-25T15:50:23.284Z</atom:updated>
            <content:encoded><![CDATA[<h4>Creating reliable, reusable tests to aid rapid development</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zDHST1UkGdsWTSYrsw22Og.png" /><figcaption>Be assertive!</figcaption></figure><p>Recently I completed a challenge to rotate the elements of a matrix clockwise or counter-clockwise by 45 degrees. In other words, I needed to be able to make this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*HRi05wopmFjHayPfatHhcg.png" /></figure><p>Turn into this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*Wrv4LHZgQuMc7FpzeBWAAA.png" /></figure><p>Or this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*sYC2CKw2X1-g7h8I-i2D_Q.png" /></figure><p>Here is a scenario where I know exactly what kind of output I needed from the method before I write it — which makes it the perfect candidate for unit testing.</p><p>A unit test is exactly what it sounds like. It’s a test that verifies the operation of a unit of code. You might be familiar with using Unity’s <em>Debug.Log()</em> method to verify the operation of your code as you develop. This is similar, but comes with several added advantages.</p><ul><li>Unit testing happens outside of the method itself. If you change the method, the test will still be valid because it isn’t dependent on the contents of the method. It’s just concerned with the the input/output (if any) or behavior of the code.</li><li>Unit testing can happen outside of play mode and without any input from the user. This makes getting to the point you want to test rapid, and feedback instantaneous.</li><li>You can organize your tests into suites of similar tests working on the same game system. If one of them fails, the whole suite fails.</li></ul><p>Let’s take a look at how to use the Test Runner module of Unity to create and organize our unit tests.</p><p><strong>The Test Runner</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JOIc7lgUOnK7ACgD5RpSNQ.png" /></figure><p>When you first open <em>Window &gt; General &gt; Test Runner</em> you will see something like the above screen. There are two tabs — one for Play Mode Testing and one for Edit Mode. I’ll be focusing on Edit Mode, as I want to run my tests without running the game.</p><p>To get started, click the <strong>Create EditMode Test Assembly Folder</strong>. This will create a new folder <em>and</em> an Assembly Definition Asset (.asmdef) file inside it. This file tracks references to the folders where you are storing the scripts you wish to test. Before we get to that, let’s click <strong>Create Test Script in Current Folder</strong>. It’s good practice to name this script after the class holding the methods you intend to test. So, in my case, I’ll call it the RotateMatrixTests script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*XKavchV3pTH5CRpxUXlWuA.png" /></figure><p>We also need to create an Assembly Definition Asset in the folder where the RotateMatrix test is found. To do that, right click in the scripts folder and select <em>Create &gt; Assembly Definition</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/957/1*cNhqnIn5Jj9rm6RU7SPaMg.png" /></figure><p>As you can see above, I named mine RotateMatrix.Scripts. This naming convention is based on the following template:</p><blockquote>[Project Title].[First Folder in Tree (excluding Assets)].[…additional folders in tree as necessary]</blockquote><p>In my case, the project title is Rotate Matrix, and the script I’m testing is in <em>Assets/Scripts/</em>. So the name of my Assembly Definition for that folder is “RotateMatrix.Scripts”</p><p>Once you have the Assembly Definition Asset created, go back to the Tests.asmdef file in the <em>Scripts/Tests/</em> folder and view it in the inspector. Under Assembly Definition References, click the + sign and drag the asmdef file you just created in the <em>Scripts/</em> folder into the slot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*5UD_41AYkYKdxoWjDzEDcw.png" /></figure><p>Now, if you’ve followed all these steps, you should be ready to open the RotateMatrixTests script and start testing! Clear out the default test methods, and create a new method named after the method you intend to test. I’m testing my <em>RotateClockwise(int[,] matrix) </em>and <em>RotateCounterClockwise(int[,] matrix) </em>methods, so I’ll call these <em>RotateClockwiseTest() </em>and <em>RotateCounterClockwiseTest()</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*514ymV2wdep61nWtiAq2JA.png" /></figure><p>Notice each has the [Test] property. This tells Unity to display these tests in the Test Runner window.</p><p>Now, in order to run the rotate clockwise method we need to provide it with a 2D array to fiddle with. So the first thing we need is to define such an array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uGGeIMuAuZ3be3jF9kpK6A.png" /></figure><p>Now, the <em>matrix</em> variable is defined as something that looks just like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*HRi05wopmFjHayPfatHhcg.png" /></figure><p>Next, we need to get a reference to the RotateMatrix script instance in the scene so we can access the method to test:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0KOzfd21phz-viz6izS02w.png" /></figure><p>The last ingredient is to call the <em>Assert() </em>class. This is the class that holds the logic for checking our output. And what kind of output do we expect? Well, we can check two corners of the original matrix and compare them to the corresponding destination points in the rotated matrix. If the values have moved to the positions we expect, then the rotation is successful.</p><p>So, the value originally contained in <em>matrix[0,0] </em>should equal the value of <em>result[0,1] — </em>that is, one column to the right of where it started. Likewise, the value in the original matrix at the maximum of both axes should equal the value in the result matrix at the maximum of the x axis and the maximum less one of the y axis — one row higher than when it started. Here’s one way to write an assert to test these conditions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w_m_erkCaHzb81muGBskKA.png" /></figure><p>If either of these conditions fail, we’ll get an explanation of which corner doesn’t match in the debug log.</p><p>I’m now ready to try and work out the solution to the problem over in my RotateClockwise method. I can try any number of approaches, and instantly test the method to see if it gives the expected output. Iterating on this problem just became 1000% easier.</p><p>If you’re curious, this is the method I finally came up with:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bR6CeJrwBXc_ZnuQBAZ_Rg.png" /><figcaption>The 3x2 Rotation Matrix can rotate Vector2 data around a center coordinate — perfect for rotating the indices of 2D arrays around the center of the array.</figcaption></figure><p>When you’re ready to test out your method, all you need to do is double click the test or test suite you wish to run in in the Test Runner (or click “Run All” to test everything in all test suites).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6cLM7IS202HI09jvKRe2cg.png" /><figcaption>Hey, look! It works!</figcaption></figure><p>As you have seen, unit testing can be a powerful way to get the most out of your time at the keyboard. I hope this brief introduction to unit testing has been useful to you!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80d8bbfc62bb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/c-unit-testing-in-unity-80d8bbfc62bb">Introduction to C# Unit Testing in Unity</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Refactoring Complicated Code]]></title>
            <link>https://medium.com/geekculture/refactoring-complicated-code-ad8b09a5b92b?source=rss-8649ae2216b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad8b09a5b92b</guid>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Micha Davis]]></dc:creator>
            <pubDate>Tue, 17 Aug 2021 17:28:25 GMT</pubDate>
            <atom:updated>2021-08-18T06:24:09.488Z</atom:updated>
            <content:encoded><![CDATA[<h4>From Brute Force to Formal Elegance</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*YkvLotO3yRnhXYEt" /></figure><p>Here’s the situation: you’ve been hammering away at a complicated coding problem for a day or so and you’ve iterated your way into a working system. Mission complete, right?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*jtJXxOdlaEB9yBKN" /><figcaption>Right?</figcaption></figure><p>Slow down, friend. It’s time to clean up that code before you go much further.</p><p>Finding a brute force solution is not the end of the line. It’s worthwhile to take a moment and look for ways to streamline what you’ve created. Search for repeated code, and possibly isolate that code into its own method. Look at where information starts and stops, and see if you can move things around to eliminate dependencies. Explore different data structures to see if there is one that can help you avoid costly operations.</p><p>Today, I’m going to do some cleanup of the Tactical Command System I outlined in the <a href="https://medium.com/geekculture/scripting-a-turn-based-tactical-command-system-b14b33000a0f">previous</a> <a href="https://medium.com/geekculture/turn-based-tactical-command-system-part-2-f8c57abaec74">two</a> articles. Most crucially, I’m going to refactor the command execution system to require less information passed up to the manager by using the <a href="https://medium.com/nerd-for-tech/undo-redo-with-the-command-pattern-in-unity-c-d3b63beab7a4">Command Pattern</a>.</p><p><strong>Out With The Old</strong></p><p>The first thing we need to examine is the end of the line. We have two methods involved in executing actions. The ProcessRoundRoutine collects all the relevant information and feeds it to a catch-all execute method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_B3uNDEcn7O6LHeU.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*o5hnS8e5RBKcYvmu.png" /></figure><p>As you can see, this is too much responsibility for one class. We don’t need to pass all the information to one location to execute it. We can let the objects that already hold this information execute their own actions. We just need to get a list of those actions encapsulated into a single object, and then pass that object. That’s the essence of the command pattern. So let’s prepare an interface and a series of command objects.</p><p><strong>In With The New</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*STTLcWZv4Wnzk1fJK8F6pg.png" /></figure><p>Here we define the contract for this interface to include adding and removing queued actions, as well as doing and undoing them. We also add a property for ExecutionTime which will become more relevant once animations are added. For now, it serves to keep actions from playing over the top of one another.</p><p>Now, each type of action still needs its own version of the execute method so we’ll create a command for each.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*9E9BNiqPEZK6RMBUQtcwIw.png" /></figure><p>Here’s the <em>MoveObjectCommand </em>script as an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hI7IPjeh3Mc16_Ys8ewrug.png" /></figure><p>And of course we need a method to add the command to a list:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5OPhEiItaOzVW-ld6ASRFQ.png" /></figure><p>At this point we can see that there is no longer a purpose for the Turn class. Here’s what it used to contain:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YKmLM5axW7EOP9ye.png" /></figure><p>All of this information is available to the actor, who is now responsible for constructing and executing their own actions. We can ditch this class entirely, and simply use the <em>_commandBuffer</em> as a list of pending actions.</p><p>Finally, this brings us to our newly refurbished <em>ProcessRoundRoutine </em>coroutine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TAb7eJom0xDdXZcXvJlBhw.png" /></figure><p>Now that’s much more streamlined. Most of the action happens in one line here: <em>currentActionNode.Value.Execute(); </em>That’s fantastic. No more ambiguously generic <em>object</em> types to check, fewer lists of lists to run through, fewer dependencies in general.</p><p>Using this general process, be sure to take time to clean up and refactor your code on occasion. Make sure you’re working on a safe branch of your repository — it’s easy to break something while you’re “fixing” it. It can be easy to work out a brute force solution and leave it there, but you might just be making things harder for yourself in the long run if you do. It’s better to refactor a new system you just built than to be forced into it later.</p><p>That’s all for today. In the next article we’ll work out how the imperiled AI victims will decide what to do with themselves while they wait to be saved.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad8b09a5b92b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/refactoring-complicated-code-ad8b09a5b92b">Refactoring Complicated Code</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Turn-Based Tactical Command System, Part 2]]></title>
            <link>https://medium.com/geekculture/turn-based-tactical-command-system-part-2-f8c57abaec74?source=rss-8649ae2216b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8c57abaec74</guid>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[gamedev]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[unity3d]]></category>
            <dc:creator><![CDATA[Micha Davis]]></dc:creator>
            <pubDate>Fri, 06 Aug 2021 23:16:03 GMT</pubDate>
            <atom:updated>2021-08-17T18:47:38.799Z</atom:updated>
            <content:encoded><![CDATA[<h4>Leveraging Linked Lists for Lightweight Loops</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qol5ay3NamkaQEklOpg7tg.png" /><figcaption>Poor Yellow.</figcaption></figure><p><a href="https://medium.com/geekculture/scripting-a-turn-based-tactical-command-system-b14b33000a0f">Welcome to part 2</a>. The objective before us: take all the information we collected in the <a href="https://medium.com/geekculture/roll-initiative-ec1343797da1">previous four articles</a> (the initiative list, the turn list, the action lists) and use that data to move the proper pieces on the map in the proper sequence.</p><p>The bulk of the process happens here, in the ProcessRoundRoutine coroutine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3RK8-cmpcFA1EuyptEMtPw.png" /></figure><p>This can easily be chopped up into three bites:</p><h4>Finding the Front of Each List</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o0QpyqPkhQQan5GmWquFpw.png" /></figure><p>This is pretty self explanatory. At the start of the round we need to be indexed to the front of each list.</p><p>Then we start a <em>while </em>loop, which will continue until the Round list is empty. Inside it, we do two things:</p><h4>Executing the Current Action</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cz1wOdADXx6i4_Fx04egdg.png" /></figure><p>Here we’re checking to see what sort of information we’ve been passed, and sending the appropriate overload to the ExecuteAction() method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mLL-JeOZgk4K80kFgW3UDA.png" /></figure><p>If we get a feat and a target, then that’s one of three targeted feats: Move a movable object, Break a breakable object, Save a save-able object. For now, I just have Move defined.</p><p>If we get a feat with no target, then that’s one of two untargeted feats: Panic in place, or take a moment to try and Loot an object in spite of present danger. For now we express those behaviors in the debug log.</p><p>Finally, if we are passed only a target, that must be the player requesting the move of an actor. All we need to do is signal the selected actor to complete the move.</p><h4>Traversing the Lists</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kmH4gI0gkl_m0LXsBKGUmg.png" /></figure><p>At last, we get to the lists. Here’s the pseudocode I worked this up from:</p><blockquote>set current action index to the next action node<br>if the action node IS NOT null, we have another action<br>…remove the previous action node from the action list<br>if the action node IS null, we’re at the end of the action list<br>…clear the action list<br>…set the current turn index to the next turn node<br>…AdvanceTurn()<br>…if the current turn node IS NOT null, we have another turn<br>……remove the previous turn node from the turn list<br>……set the current action index to the first node of the new turn’s action list<br>…if the current turn node IS null, we are all out of turns<br>……clear the turn list</blockquote><p>If you are at the end of a LinkedList when you call for the next node, you get a null value. So, we just nudge the list index along until it comes up null, and when it does we clear the list. When both lists are clear the round is over.</p><h4>Putting It All Together</h4><p>To get it all running, we wrap this whole process up in a coroutine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n-b61BWgeswsiYwt3DETQg.png" /></figure><p>Let’s see it all in action:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*MuFWH8fA684vGEtmoX87wg.gif" /></figure><p>You can see some of the debug information on the bottom. I haven’t done much to display what’s going on in the interface yet, but that’ll change. What’s important is that we can queue multiple actions from multiple actors and have them play out in sequence. Exactly what we wanted.</p><p>That’s all for now. The next article will focus on cleaning up this implementation into something a bit more elegant and streamlined.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8c57abaec74" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/turn-based-tactical-command-system-part-2-f8c57abaec74">Turn-Based Tactical Command System, Part 2</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scripting a Turn-Based Tactical Command System with C# in Unity]]></title>
            <link>https://medium.com/geekculture/scripting-a-turn-based-tactical-command-system-b14b33000a0f?source=rss-8649ae2216b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/b14b33000a0f</guid>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <dc:creator><![CDATA[Micha Davis]]></dc:creator>
            <pubDate>Fri, 06 Aug 2021 05:12:17 GMT</pubDate>
            <atom:updated>2021-08-17T16:29:41.988Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Brute Force Solution to a Complicated System</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xfGD187_UuVFegqdtv1gYg.png" /><figcaption>Disclaimer: I know there are standards and conventions for this sort of diagram, but I didn’t need to share this idea with anyone but me when I drew it, I just needed to see how things connect so I could plan.</figcaption></figure><p>Here’s the objective for today: create a system for recording and replaying multiple commands from multiple actors, where all actors first declare their moves in ascending initiative order, and afterward all moves are played in descending initiative order.</p><p>There <em>is</em> a more elegant solution than the one I’m presenting here. This is the initial brute force implementation to get the mechanics working. We’ll be shaping this all up into something more polished in the next article.</p><p>We’re gonna take this one in two parts. Today’s article will focus on actors declaring their actions. Tomorrow I’ll cover executing the action queue.</p><h4>Actors Declare Actions</h4><p>We’ll start just after we <a href="https://medium.com/geekculture/roll-initiative-ec1343797da1">calculate initiative</a>, with the DeclareActions() method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sUT457urX0BJXIOyfQ3lrw.png" /></figure><p>Stepping through this, we start with a for loop based on the number actors in the scene. We collect the last person in initiative order from the SelectionManager, and we create a new instance of the Turn Class (below). Then we check to see if the actor is a hero. If so, we turn on the UI panel for player commands, wait until their action is selected (with a bool comparing the number of actions the player gets with the number they’ve queued), and then turn the panel off. If the actor is not a hero, for now it creates a Panic feat with a range of 0 and passes that along.</p><p>For selecting the hero’s action, we have the following method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vXKoAZg_exJJLvlz3dCWFw.png" /></figure><p>That’s a slight refactor of our earlier SelectTarget() method. Here it is only enabled during the player’s turn, and now each possible outcome leads to a LogFeat() method call.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mgVBxc6JilGuCDrvxAITdw.png" /></figure><p>There are three overrides, one for each combination of feats and targets our actions will allow. Every action could potentially have a feat and a target, a feat alone, or a target alone. You’ll notice I’m using the <strong>object </strong>type for the target variable. This type can hold any data — specifically in this case, either a GameObject target, or a Vector3 movement target. We load the data received into the AddAction() method of the Turn instance we created earlier.</p><p>Let’s look at that Turn class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PdoL9D_zxSJ-iN1EytYaqg.png" /><figcaption>IMPORTANT: A ‘new object[2]’ is required each time AddAction is called to avoid overwriting the references in the action list.</figcaption></figure><p>All actions potentially have one or both of a Feat and a target. As I mentioned above, we have two potential data types in the target variable. The Feat is a third. I want to store all of these in one data structure to make it easy to package up, so I’ll use an object array to hold all three kinds of data in one variable.</p><p>In order to get the <em>action[]</em> information, I’ll use two methods, one that accepts a target with optional feat, and one that accepts a feat with optional target. That should handle all three possible combinations.</p><p>Now I finally have a single object that represents all the data for any action any player could have done, and I add it to the end of the list. Once all the actions are collected, we resume the DeclareActions() method at the end, where the instance of Turn is loaded into the Round list and we push the last person in initiative to the front of the line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w1pU2KtJQyrrVqAjSuOH6A.png" /><figcaption>The Round list is a linked list of Turns in the Round.</figcaption></figure><p>After this loop runs through all the actors in the scene there will be a list of Turns each with its own list of actions.</p><p>Whew. That’s the big part. Tomorrow I’ll go over the ProcessRound() method, which will playback our recorded actions by traversing the list of lists (of lists) we’ve generated. And THANKFULLY because we’re using linked lists we won’t have to use any <em>for</em> or <em>foreach</em> loops to do it.</p><p>Until then!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b14b33000a0f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/scripting-a-turn-based-tactical-command-system-b14b33000a0f">Scripting a Turn-Based Tactical Command System with C# in Unity</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Roll Initiative: Part 2]]></title>
            <link>https://medium.com/geekculture/roll-initiative-part-2-d7ab2d82ef27?source=rss-8649ae2216b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/d7ab2d82ef27</guid>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[c-sharp-development]]></category>
            <category><![CDATA[unity3d]]></category>
            <dc:creator><![CDATA[Micha Davis]]></dc:creator>
            <pubDate>Mon, 02 Aug 2021 22:45:58 GMT</pubDate>
            <atom:updated>2021-08-03T06:43:51.355Z</atom:updated>
            <content:encoded><![CDATA[<h4>Handling Tactical Initiative Data Using Linked Lists</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-TaOLf8RYvvm0An_IQ6UNg.png" /></figure><p>Today, the objective is to sort a C# Dictionary by the value property of each key/value pair, and then load that information into a LinkedList. This list will allow us to easily control who may act and keep the UI updated in the process.</p><p>We’ll begin by declaring a <em>LinkedList&lt;KeyValuePair&lt;Actor, int&gt;&gt;</em> to match the data from the dictionary. Then in the RoundStart() method <a href="https://medium.com/geekculture/roll-initiative-ec1343797da1">we made in the previous article</a> we’ll remove the debug loop and replace it with another loop that is specifically targeting the key/value pairs in the dictionary:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J3-4cG7LW3rNUR1B6s6Dug.png" /><figcaption>I changed the Key type from GameObject to Actor — it turned out to be more expedient to reference the class instance, and access the GameObject through that only when necessary.</figcaption></figure><p>Here I’m using LINQ to read the dictionary in Value order, and then adding the current value to the first node in the initiative list. This will result in the lowest initiative being stored in the last node when the <em>foreach </em>loop is complete.</p><p>Now that the dictionary is sorted, we need to update the UI to show the player where everyone is in the turn order. For that we’ll go to the UI Manager.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t3bMSCg3R1vLRT5jJxy1hw.png" /></figure><p>Here’s where we take advantage of the LinkedList. Normally, this kind of operation would be very resource intensive, requiring nested <em>for</em> loops. But since each node on the LinkedList has a reference to the nodes before and after it, we can simply remove the last item on the list and put it up front. Since we’re iterating backward from lowest to highest, this will place the portrait and initiative values in their proper places.</p><p>Note that to make this extensible we’ll later need to add the ability to populate and expand the _<em>initiativeRoster</em> and <em>_initiativeText </em>lists dynamically to account for any number of actors, but for now we’ll serialize it and populate the list for the four actors we have in the scene.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tZ_fRo6oggJXJHipgD5CYg.png" /></figure><p>Finally, we need a button to advance between turns. The button will trigger an AdvanceTurn() method in the GameManager class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eP2FaSK1jAeJ_hWS0SCufw.png" /></figure><p>Here I’m moving the first (current) actor/initiative pair on the list to the back of the list. Then I tell the UI Manager to update the initiative display, and I’m letting the SelectionManager know who the active actor is (more on that next article). Finally, I assign the current initiative value to an index so I can track where we are in the initiative round.</p><p>Time to look and see what we have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*rLuQRBSzm4F-KUHaJSB9oA.gif" /></figure><p>Magical.</p><p>That’s all for today. In my next article I’ll wrap up the initiative system by developing methods that will differentiate between player and AI turns and handle input appropriately.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7ab2d82ef27" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/roll-initiative-part-2-d7ab2d82ef27">Roll Initiative: Part 2</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Roll Initiative!]]></title>
            <link>https://medium.com/geekculture/roll-initiative-ec1343797da1?source=rss-8649ae2216b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec1343797da1</guid>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[unity3d]]></category>
            <dc:creator><![CDATA[Micha Davis]]></dc:creator>
            <pubDate>Mon, 02 Aug 2021 06:13:46 GMT</pubDate>
            <atom:updated>2021-08-02T06:53:09.729Z</atom:updated>
            <content:encoded><![CDATA[<h4>Creating An Event Driven System for Ordering Actors in Turn-Based Tactical Play with Unity 3D and C#</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*PZ7W65M_FTW0sJnlhMuo9g.png" /><figcaption>Zip is running circles around Zap this round.</figcaption></figure><p>Here’s the objective: using Unity 3D and C# we want to build a system that can alert any number of actors to roll a random number, and report back their name and initiative number to be stored in a dictionary as key/value pairs.</p><p>For the first part of this, we’ll be using the Func&lt;TResult&gt; Delegate. This is a kind of delegate that can accept any function that returns the type designated by TResult. In this case, we want a delegate method that will return an integer value — the actor’s initiative roll.</p><p>The delegate declaration looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9a1oQQEsFiNkWjJuLUBMLA.png" /></figure><p>When we signal that the round has started, any function subscribed to this delegate will return an integer. Let’s get some subscribers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*77NBhvG9FkJTR9Cddofhvg.png" /></figure><p>Here I have an abstract Actor class that my various actor types can inherit. Since each kind of actor has a different means of calculating their initiative, I’ll subscribe an abstract RollInitiative() int function to the OnRoundStart delegate. Now whatever way they use to determine the return value will be up to them. Here’s one example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZtdFU9A5BSOnoUdqjgG5VA.png" /></figure><p>Next we’ll declare the dictionary:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kFUJic8caGwMwWBziKIajQ.png" /></figure><p>There are two problems from here.</p><p>First problem: we need more than an integer returned to us. We’re putting this information into a Dictionary, which requires two types of data — a GameObject (the actor), and an integer (the actor’s initiative score).</p><p>The other problem: our Func delegate is <em>multicast,</em> meaning there are multiple subscribers. If we attempt to store the return value from OnRoundStart <strong>we will only get the value for the last subscriber on the invocation list. </strong>Obviously we want to store the value for every subscriber, so a simple <em>int roll = OnRoundStart?.Invoke() </em>will not work.</p><p>We can solve both problems in one <em>foreach</em> loop. The first problem will be solved by using the Delegate.Target property. If we cast the Target as an Actor, we can store the game object the Actor instance is attached to in a variable and use it as the key half of our dictionary key/value pair.</p><p>To solve the second problem, we iterate the foreach loop using the Delegate.GetInvocationList() method. This method keeps a list of all subscribers to the delegate. We Invoke() each subscriber in turn, and store the returned integer into a variable to use as the value half of the dictionary key/value pair.</p><p>Here’s the C# method to make it all work:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SK8dcWheHI2cW06ff-XTVg.png" /></figure><p>And from there we get the following output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/905/1*cR3nuIRH7EYnPEVgS9mUQA.png" /></figure><p>The first four entries come from the individual Actor’s RollInitiative() method, and the last four are the details of what is stored in the initiative dictionary. A perfect match.</p><p>In the next article we’ll put the dictionary to work by loading each key/value pair in it into a LinkedList&lt;T&gt;() which we can traverse and reorder according to the turn progression of the game and the initiative cost of the actors’ actions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec1343797da1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/roll-initiative-ec1343797da1">Roll Initiative!</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using NavMesh Obstacles in Unity]]></title>
            <link>https://medium.com/geekculture/using-navmesh-obstacles-in-unity-da50dd76f385?source=rss-8649ae2216b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/da50dd76f385</guid>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[game-design]]></category>
            <category><![CDATA[unity]]></category>
            <dc:creator><![CDATA[Micha Davis]]></dc:creator>
            <pubDate>Fri, 30 Jul 2021 16:57:16 GMT</pubDate>
            <atom:updated>2021-07-31T07:49:57.523Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vnm3Y9eXbGvADSQyNf_5KQ.png" /></figure><p>Today we visit the scene of an emergent urban tragedy: a tremor has collapsed some pedestrian walkways, leaving a bystander trapped beneath the rubble! Luckily, we have a pair of heroes on the scene, ready to save the day.</p><p>Thus the stage is set for today’s demonstration.</p><p>From a gameplay perspective, we need to be able to interact with objects on the scene that can be moved, but we also need those objects to prevent NavMesh agents from walking through them.</p><p>These two goals are in conflict. If we bake these objects into the NavMesh in the editor, then moving them during runtime will leave invisible obstructions in the NavMesh. Dropping the object in a new location will not create a new impassible area; the NavMesh Agents will ignore the obstacle completely, trusting the NavMesh to indicate where it can and cannot move.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*Q99Kgkz3WZG8IRg0pU33Ug.gif" /></figure><p>We could re-bake the NavMesh during runtime, but the larger our levels get the more that’s going to slow down gameplay.</p><p>Enter the NavMesh Obstacle component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*SJm26TjFI3rxMrY8bw36lg.png" /></figure><p>This component can be added to any object in the scene. It will make the object <em>carve</em> a hole in the NavMesh while it is stationary, and the hole will disappear when the object moves. There are a few options to customize this behavior, including adjusting the size and shape of the obstacle area (useful for strangely shaped objects), but the default settings should fit most uses.</p><p>Let’s see how my scene looks with all my movable blocks using the NavMesh Obstacle component, rather than being baked into the mesh:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*MKDw3f7a_cFt-MaLwAQAEA.gif" /></figure><p>Great! Well, clearly I need to be mindful of where I let the player drop a block… but otherwise, great! You can see the mesh updates any time our blocks start or stop moving, ensuring that an obstacle at rest remains an obstacle no matter where it rests.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da50dd76f385" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/using-navmesh-obstacles-in-unity-da50dd76f385">Using NavMesh Obstacles in Unity</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>