<?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 Gautam Vasudevan on Medium]]></title>
        <description><![CDATA[Stories by Gautam Vasudevan on Medium]]></description>
        <link>https://medium.com/@gautamv?source=rss-6941a28572f4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tG1cwPA_OQvpCECXTcQ3Yg.png</url>
            <title>Stories by Gautam Vasudevan on Medium</title>
            <link>https://medium.com/@gautamv?source=rss-6941a28572f4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 10:16:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gautamv/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[Advanced Behavior Tree Structures]]></title>
            <link>https://medium.com/@gautamv/advanced-behavior-tree-structures-4b9dc0516f92?source=rss-6941a28572f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b9dc0516f92</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Gautam Vasudevan]]></dc:creator>
            <pubDate>Wed, 02 Dec 2015 17:12:53 GMT</pubDate>
            <atom:updated>2015-12-02T17:12:53.848Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/425/0*CP2jZlBKynOQKuzX.jpg" /></figure><p>Behavior trees are an excellent way to have an extensible language for describing AI. While <a href="https://gamedevdaily.io/managing-ai-in-gigantic-523dc84763cf#.ozzjagdh4">understanding the basic structure</a> of behavior trees is critical, there are several interesting technological challenges when implementing behavior trees in an actual game.</p><p><em>Full-disclosure: While some of this article may reflect some of the high-level systems used in </em><a href="http://www.gogigantic.com/"><em>Gigantic</em></a><em>, I’m currently employed at a </em><a href="http://www.apple.com/"><em>fancy fruit company</em></a><em> and can neither confirm nor deny that any of this architecture is still in production at </em><a href="https://www.motiga.com/"><em>Motiga</em></a><em>.</em></p><h3>Behavior Tree Engine Challenges</h3><p>Depending on the engine you’re working in, you’ll likely run into a couple of common implementation challenges. In particular, most engines I’ve worked with have at least some necessary event-driven code you’ll need to plug into, and reusing parts of behavior trees becomes critical as the logic becomes more complex.</p><h4>Event Handling</h4><p>Event handling is one of the simpler tasks for a behavior tree to manage. That said, we want to retain the procedural nature of the tree because managing event-driven structures simultaneously with procedural structures makes understanding the logic of the tree extremely complex. Instead, we can choose to handle events by polling a particular sentinel value to determine if an alternate branch of logic should be executed.</p><p>In practice, we want to capture events separately from the behavior tree structure, so that we don’t miss events while executing tree logic that isn’t actively checking for a particular event. One way of handling this is to create an individual event monitoring object per event we wish to monitor. Prior to each tick of the behavior tree engine, event monitoring objects can determine if an event has changed since the last frame, and can record this state. It’s important at this point that the event monitoring object flags itself as having noticed a change. This allows the behavior tree to implement conditional nodes that can evaluate both if the event has been fired as well as if it has changed since the last time it was checked. This converts events being fired into states that can be polled, matching the natural structure of the behavior tree logic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*SyrGjhjOJKGA2w3MK5D-OA.png" /></figure><p>There are still some drawbacks to this mechanism, as it allows events to be ignored by the behavior tree if the logic is improperly created (such as not putting a conditional node in parallel with other logic if necessary). It also necessitates the creation of conditional nodes that check the state of an event as well as if the state itself has changed, potentially as unique nodes depending on how you want to ensure clarity. This reveals a fundamental truth of behavior trees — behavior trees are not a shortcut to making AI logic <em>simpler</em>, they merely improve the accessibility of that logic.</p><h4>Tree Reuse</h4><p>Of course, all this work in creating behavior trees would lack scalability and sustainability if we couldn’t reuse common logic expressed in our behavior trees. This can be accomplished by supporting the concept of “subtrees”. A subtree is basically another behavior tree, but where the root node passes the result of its child back to the tree that calls it (kind of like a subroutine). In order to connect a behavior tree with a subtree, a subtree proxy node is created, which has a reference to the subtree we want to execute. When that subtree proxy is ticked, it places the subtree root on the stack and continues execution. Finally, when the subtree’s child nodes return a result, the subtree passes this result back to the proxy node that called it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*HZGYnttTKFOorHQLxvP8XA.png" /></figure><p>This allows us to take any set of logical instructions and actions and bundle them up in a subtree and reuse them. One of the difficulties that arises from this setup is that it becomes harder to keep the graphs acyclic. For each non-subtree, we now have to run a cycle test using a depth-first search for each subtree ensure that no cycles exist across subtree proxy nodes.</p><p>If you want to get really crazy, you can build subtrees with output nodes that expose output links on the proxy node. This allows you to encapsulate partial logic, much like calling a function and specifying delegates to execute. However, if you go down this path, the same proxy node can appear several times in your stack because subtrees with outputs that link to other subtrees can create structures that while not cyclical, are recurrent along a single execution branch. Thus, passing values back from a subtree to a proxy node will require tracking which stack to return to.</p><h3>Behavior Tree Optimization Strategies</h3><p>While there is often leeway for performance or efficiency when running AI on a client, every extra bit of memory or CPU time for AI run on a server has a continuing cost thanks to the nature of hosted environments. Basically, the more efficient the code, the cheaper it is to run a server for a game. Keeping that in mind, several architectural decisions were made in order to maximize the overall efficiency of the AI without severely impacting code maintenance.</p><p>Because the AI will often be doing very similar actions and routinely parsing shared logic, the first and perhaps most important efficiency gain is had by splitting the stateful data from the stateless data of the AI. That is, the logical <em>structure</em> of the AI is immutable, static, and stateless. An individual node, however, may want to capture some stateful data in order to maintain execution across ticks for any particular instance of an AI character. In order to achieve this separation of stateful from stateless data, each node class’ member values are assumed stateless, and the node will instantiate stateful data per AI character to capture member values. This way, even as new AI characters proliferate, the amount of memory taken up by the AI tree structures will remain the same.</p><p>Beyond merely being strategically split, the data can also be organized using <a href="http://gamedevelopment.tutsplus.com/articles/what-is-data-oriented-game-engine-design--cms-21052">data-oriented engine design</a>. Because we know that the order in which data will be accessed is generally depth-first, we can organize the both the structure of the tree and its stateful data by flatting the tree structure into an array ordered in a depth-first manner. This way, as we execute the tree, we are generally moving in a single direction across an array, and often accessing data contiguously. With stateful data generally being relatively tiny, and with proper alignment, we can produce a very cache-friendly execution system. Be aware though that the devil is in the details for any given implementation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*8yqoc5bmZEPTKNXx5OdyAg.png" /></figure><p>Another efficiency gain made possible from this <a href="http://en.wikipedia.org/wiki/Flyweight_pattern">flyweight-style</a> split of stateful and stateless data for the behavior tree engine is that it allows for very simple lockless multithreaded access to the stateless portion of the data (the actual structure of the tree and the core logic of execution). With this property, we can maximize the parallelizable aspect of the behavior tree engine when applying <a href="http://en.wikipedia.org/wiki/Gustafson%27s_law">Gustafson’s Law</a>. The stateless data is implicitly read-only and can have multiple readers with no contention, and the write portions are stateful and stored in separate memory locations and thus invoke no write contention.</p><p>Barring other engine or infrastructure considerations, this setup allows the behavior trees to share and execute their structure across multiple AI across different matches or game instances (ensuring that AI interactions are completely independent from each other). Note that this only affects the engine driving the behavior tree — designers are still blissfully unexposed to the complexities of parallelism.</p><h3>What the FAQ?</h3><p><strong>How many behavior trees can I have using this system?</strong></p><p>Lots! Each behavior tree needs only one instance of the tree structure itself and for each AI using it, an instance of stateful data. I personally recommend keeping the number of designed behavior trees to a minimum as it maximizes sharing of both memory and logic, making the trees much more maintainable.</p><p><strong>Are behavior trees isomorphic to finite state machines?</strong></p><p>Behavior trees can be made to be trivially turing complete, while a pure finite state machine is not because the memory you can store in an FSM is limited by the number of states you have.</p><p><strong>Are behavior trees with an execution stack isomorphic to hierarchical finite state machines?</strong></p><p>Hierarchical FSMs are merely mechanisms to organize an FSMs transitions, but you’re still limited to states and moving around between them, capturing no further information within the structure. A behavior tree is more like a procedural programming language, where the state is subsumed into the current execution pointer and the contents of the heap and execution stack.</p><p><strong>What safe guards are there?</strong> <em>If two parallel branches both result in “run to target” actions that’s bad. Does the second branch detect another run to target action is in progress causing it to fail? Do both actions execute causing a character to stand in place or ping pong? Does one action stall until the other completes? Can the designer choose?</em></p><p>There’s likely not a single right answer in all cases. An important point here is that safe-guards are necessary in the editor to prevent trivially bad trees from being created. For example, the editor can prevent incompatible nodes from being run simultaneously, detecting cycles via subtrees, or any other pathological cases. For those that can’t be caught before execution (because they happen thanks to game state), the behavior tree engine can detect and report these errors. These are pretty rare though, as it’s pretty easy to find and fix all the issues in the behavior tree before hand (much like a compiler will catch most non-logic errors).</p><p><strong>What kind of editor did you use?</strong></p><p>Designers use a custom node editor written in C++ and wxWidgets (for compatibility with Unreal Engine 3) built to make organizing and debugging behavior trees as simple as possible. If you were to implement one yourself, having hooks into the game-engine to reflect current state is nice.</p><p><strong>Are there only three types of nodes logic, condition, and action? What other node types are there?</strong></p><p>There are plenty of kinds of nodes you can create that I haven’t covered here. For example, I created the following additional node types:</p><ul><li>Structural nodes — perform basic branching logic,</li><li>Evaluation nodes — like enumerators that use child nodes to evaluate each iterator value</li><li>Declaration nodes — evaluators that also set one or more values after evaluation, if they have a valid evaluation</li><li>Decorator nodes — these have a single input and single output and do something with the status that flows in. For example, an inverter node that changes success to failure or a log node that prints out to the screen that this node ran with some value</li></ul><p>In my experience, very few of these naming conventions are strict, so feel free to categorize nodes in a way that makes sense to you.</p><p><strong>Do you need to be a programmer to work with behavior trees?</strong></p><p>No! Not at all, you simply need to have a good grasp of logic. Behavior trees in Gigantic were created to allow technical designers to create the AI behaviors they wanted without having to write code. That said, the tool is only as good as the wielder.</p><p><strong>What problem spaces do behavior trees solve really, really well?</strong></p><p>AI and general scripting scenarios. They fundamentally let you create simple, reusuable logic using a visual programming language.</p><p><strong>What problem spaces do behavior trees solve poorly?</strong></p><p>The more complicated the logic you’re trying to express, the less scalable visual programming languages generally are. One way to tackle this is to encapsulate more complex logic within nodes so as to keep behavior trees relatively simple. In this way, you create a sort of language hierarchy that allows you to understand and structure the logic of the game more easily.</p><blockquote>Gautam Vasudevan began creating games as soon as he learned to program in BASIC as a wee child. He eventually learned to program in other languages (thankfully), and worked on pretty much every aspect of game engine programming for over 10 years. Today, he works on cool stuff at Apple. <a href="http://leamonde.net/">http://leamonde.net</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b9dc0516f92" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Managing AI in Gigantic]]></title>
            <link>https://medium.com/@gautamv/managing-ai-in-gigantic-523dc84763cf?source=rss-6941a28572f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/523dc84763cf</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Gautam Vasudevan]]></dc:creator>
            <pubDate>Tue, 01 Dec 2015 15:58:39 GMT</pubDate>
            <atom:updated>2015-12-01T18:18:23.250Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*L2Eo-ciKqXzwySJo.jpg" /></figure><p><a href="http://www.gogigantic.com/">Gigantic</a> is a 5-on-5 competitive multiplayer game in which each team is paired with a giant creature called a Guardian. The objective of the game is to destroy the enemy team’s Guardian. This is done by building power for your team’s Guardian by killing the enemy team’s players and capturing strategic parts of the map by spawning creatures to guard those areas. This fast-paced competitive game involves both player-vs-player and player-vs-AI elements as key parts of the game. As such, AI is a major component of giving Gigantic its unique flavor.</p><p>There are a number of ways to implement AI systems for a game such as Gigantic. In the past, I’ve worked on data-driven solutions using state machines to custom-coded solutions backed by neural networks. For Gigantic, the primary mechanism through which AI is implemented is behavior trees.</p><h3>State Machine Madness</h3><p>While there are a number of ways to model game AI, it’s often best to use a custom language to specify how an AI works, rather than using whatever language the game engine is written in. Using a simplified domain-specific programming language is important because the design of the AI is part of the overall design of the game. Since designers’ work improves with iterations, allowing them to define AI behavior greatly improves productivity and quality because their ideas can be implemented immediately instead of waiting for an engineer to translate their idea into the game.</p><p><a href="http://en.wikipedia.org/wiki/Finite-state_machine">Finite State Machines (FSM)</a> are a common and classic way to implement game AI. We can think of an AI character as being in various states of execution, and an FSM allows you to define these states, such as “guarding”, “attacking”, and “defending”. We also need to specify the transitions between states, which are the rules for when we go from one state to another (for example, we can go from “guarding” to “attacking” but not from “guarding” to “defending”).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*8KfnJbMVS609oVsP7GgX3g.png" /></figure><p>This is pretty simple, and we can manage the growing complexity of transitions through <a href="http://aigamedev.com/open/article/hfsm-gist/">hierarchical finite state machines</a>. Unfortunately, game design often wants to reuse states in different contexts, and there’s no way to do such a thing without at some level duplicating the state logic and transitions that are common between reused states.</p><p>For example, if I had an AI character that could parry with a sword when both attacking or defending, but after parrying would go back to either attacking or defending depending on what they were doing previously, we would have to create two parry states to encapsulate that difference in transition behavior.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*8Vrwk7QHBPnRX4q5gGEclw.png" /></figure><p>Even with great tools to help make things easier, managing states and transitions can become very quickly unwieldy, making complex logic difficult to maintain, let alone verify. Game design thrives on being able to reuse logic easily, improving modularity without increasing complexity.</p><h3>Behavior Trees To the Rescue</h3><p><a href="http://en.wikipedia.org/wiki/Behavior_Trees_%28Artificial_Intelligence,_Robotics_and_Control%29">Behavior trees</a> superficially look like state machines, and indeed can capture all the complexity of states and transitions. However, instead of exposing AI logic at the state level (which answers “what am I doing and what can I do next?”), behavior trees make things more clear by describing the flow of logic. In other words, behavior trees try to structure more clearly “how should I figure out what to do?”</p><p>In Gigantic, behavior trees are created through a graph-based visual programming language. A behavior tree is composed of a hierarchy of nodes, with parent nodes linking to child nodes. Parent nodes typically make flow control decisions, deciding if and what order their child nodes should be run. Nodes with no further children are called leaf nodes, and typically contain the conditions to check in the game or actions to perform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*q93ij0S-FQFpsB0PnbMzhA.png" /></figure><p>The logic of behavior trees is driven by the status of executing the logic of nodes. When a node is done executing, it passes the status of its execution back to its parent node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*UBRxLdd5G9EC-jEucToQXQ.png" /></figure><h3>Types of Nodes</h3><p>There are lots of different kinds of nodes, but common ones include <strong>condition nodes</strong>, which are leaf nodes that simply test some condition in the game such as “do I have more than 50% of my health?” Another type of leaf node is the <strong>action node</strong>, which tell the AI to do perform an in-game action, such as firing a weapon, and will return SUCCESS if the action completes successfully, FAILURE if it doesn’t, and RUNNING while it is still executing and needs more time.</p><p>A common parent node is the <strong>sequence node</strong> which has multiple child nodes and will execute them in order. If any of these child nodes returns the status FAILURE, the sequence node will be set to FAILURE. Otherwise, if all the child nodes return SUCCESS, the sequence node will be set to the status SUCCESS. This is basically a short-circuit logical AND node.</p><p>To see how a node like this might actually execute its logic, let’s start with a simple example of a sequence node that describes when an AI should attack an enemy. <em>We shall assume that there is an enemy nearby, but that the AI is unarmed.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*aPSbi_hAKNTfBpOx8nJ1CA.png" /></figure><p>First, the sequence node will be in the RUNNING state, and will execute its first child node by putting it into a RUNNING state as well:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*Jbgi-4RhDldwaW9O6D088g.png" /></figure><p>This will ask the game engine if there is an enemy near this AI. Since there is indeed an enemy nearby, this condition node will return SUCCESS to its parent sequence node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/1*uOjbCOanaU33qhKYgSFDzw.png" /></figure><p>The sequence node will then try to execute its second child node:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*NG2083X6mbKk0h3Q5gJNDw.png" /></figure><p>This condition node will check to see if the AI has a weapon. Since the AI is unarmed, this node will return a FAILURE status to its parent node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*Xh6piAUoZWcBkaVeZVc1rQ.png" /></figure><p>Because a sequence node will have its status set to FAILURE as soon as a child node returns a status of FAILURE, this sequence node will not execute its last child node that performs the action to attack the enemy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*0FeoUpqAN-1mfwnDOlA3wA.png" /></figure><p>A <strong>priority selector node</strong> will work much like a sequence node. It will execute its child nodes in order, and if any of them returns the status SUCCESS, the priority selector will be set to SUCCESS. Otherwise, if all the child nodes return FAILURE, the priority selector will be set the status FAILURE. In other words, this is a short-circuit logical OR node.</p><p>Here is an example of the <em>final state</em> of a priority selector node that determines if the AI should sleep:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*t-mRLfk8UN9ma6XC2PzMCw.png" /></figure><p>The priority selector first checked the condition “Am I tired?” which returned FAILURE. It then checked the condition “Am I lazy?” which also returned FAILURE. Lastly it checked the condition “Is it nap time?” which returned SUCCESS, so the priority selector returned SUCCESS.</p><p>A combination of sequences and priority selectors in addition to a few other logical nodes is all we need to let a behavior tree decide what an AI should do and how it should do it.</p><p>By composing logic through modular nodes, <em>behavior trees implicitly capture a modular programming paradigm</em>, which allows for creating complex behaviors without incurring the penalty of transition management inherent in complex FSMs.</p><p>For further examples of how behavior trees work, Chris Simpson provides some <a href="http://www.gamasutra.com/blogs/ChrisSimpson/20140717/221339/Behavior_trees_for_AI_How_they_work.php">great examples</a> based on <a href="http://sourceforge.net/projects/jbt/">Java Behavior Trees</a>.</p><h3>Implementation Challenges</h3><p>Making a complex system for a game like this necessarily requires some careful technical considerations. Naturally, an exploration of these considerations benefits from a background in computer engineering.</p><h4>Executing the Logic of a Node</h4><p>Games typically operate on the concept of “ticks”, which take care of the logic that happens between rendering frames of the game. Because some actions will take time to accomplish (such as moving from one point to another), behavior tree nodes also each implement a “tick” function.</p><p>How we implement the tick functions for nodes has a big impact on how easy it is to debug the logic implemented with behavior trees. A naive approach would be to have each parent node’s tick function directly call its child node’s tick function. However, now the execution stack-depth is driven by the depth of the tree, which is something designers shouldn’t have to worry about.</p><p>Alternatively, a tree could be implemented using a polling model, where for each frame of the game, each node of the behavior tree is ticked in depth-first order. While this model makes it incredibly easy to step-debug logic, it’s also very inefficient, since parent nodes are being ticked even when they have nothing to do but wait for a child node to finish executing.</p><p>When polling is a poor choice, we often turn to an event-driven model. In this model, a parent node tells its child node to execute, then waits for the child node to fire an event when it’s done. Because only actively executing nodes are running, we guarantee the highest execution efficiency. However, this model makes following the underlying behavior tree engine code difficult because execution doesn’t happen procedurally through code, but rather jumps around functions as events are fired.</p><p>Gigantic’s implementation employs a hybrid solution. First, we maintain a custom execution stack on the heap to store which nodes should be run. When a parent node wants to execute its child node, it pushes its child node on this execution stack, and sets the child node’s state to “running”. The execution engine will run the tick function for the top node on the stack until that child node’s status is either “success” or “failure.” Then the node is popped from the stack, and the next top-most node (the parent node) is notified of the child’s status.</p><p>For example, this simple tree that determines if we should sleep:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*srgj8iPHD-HruKvV-G5M4g.png" /></figure><p>Would have the following execution stack:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*H26Y_O4fxIkKlcHLh-olXw.png" /></figure><p>The actively running node is the conditional that is determining if we’re lazy, while its parent nodes are on the stack waiting for a response before they can continue execution.</p><p>Sometimes parent nodes need to run to monitor some other game process or prematurely end execution of their child nodes. These nodes are marked as “always tick” and will be executed before their children on the stack. This model gives us the best of all worlds, with a highly efficient execution process that’s easy to step-through and debug.</p><h4>Concurrent Execution of Tasks</h4><p>Another difficult technical concept to encapsulate in behavior trees is capturing concurrent logic. For example, a designer may want to implement a behavior where an AI is running towards a location on the map while simultaneously firing a gun at the enemy. Because it’s common for an AI to execute multiple tasks at the same time, a natural approach to solving this problem would be to introduce parallelism and break tasks into threads. Of course, multithreading can be a challenging concept for even senior engineers to wrangle, let alone designers with little engineering experience. Avoiding race conditions can become a severe impediment to changing the logic in the tree.</p><p>For Gigantic, we didn’t want to burden designers with such complex and esoteric technical details. While threads typically imply a preemptive model of parallel execution, we’re really only interested in the concurrency of tasks (in that they execute in order and in the same frame). Thus, we opted to use a cooperative multitasking model. This is implemented using a “parallel” node, which can have several child nodes that branch off of it (the name “parallel node” is somewhat misleading to engineers, but easier to identify with for non-engineers).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*qe8B079mUWOTE5HTi41_3A.png" /></figure><p>In addition to the execution stack the parallel node was originally on, a parallel node adds an execution stack per child branch. When the execution engine ticks nodes, we simply tick the nodes on the top of each stack in order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*D-ZntyyXUUse8td_WvRXoQ.png" /></figure><p>This model is similar to languages that use coroutines, and gives the structural illusion of multiple threads while executing strictly serially by waiting for each child branch to yield upon completing a single tick of execution. As a parallel node iterates over its children, it changes the currently active stack to use. This way, child nodes don’t have to know whether they are parented to a parallel node. For example, a sequence node parented to a parallel node can simply push its child to the currently active stack. All the active nodes are ticked between frames of the game, allowing for an easy way to execute concurrent logic.</p><h3>Scratching the Surface</h3><p>This is only the tip of the iceberg when it comes to behavior tree architecture details. For example, other technical challenges we addressed include subroutine support for improved modularity of code, and scalability of the behavior tree engine through a carefully crafted lockless design. In addition, behavior trees can be extended in many surprisingly powerful ways, integrating other AI constructs such as <a href="http://alumni.media.mit.edu/~jorkin/goap.html">Goal-Oriented Action Planning (GOAP)</a>, cloud-based machine-learning algorithms, or user-interactive RTS-style AI command systems. Behavior trees cleanly integrate decision-making logic with their subsequent actions, empowering designers to present unique challenges and opportunities to the player, allowing the AI to become an integral part of the Gigantic experience.</p><blockquote>Gautam Vasudevan began creating games as soon as he learned to program in BASIC as a wee child. He eventually learned to program in other languages (thankfully), and worked on pretty much every aspect of game engine programming for over 10 years. Today, he works on cool stuff at Apple. <a href="http://leamonde.net/">http://leamonde.net</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=523dc84763cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>