<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1014947009705527212</id><updated>2012-02-16T19:20:54.465+01:00</updated><category term='Flash'/><category term='Caged'/><category term='android'/><category term='iPhone'/><category term='Architecture'/><category term='AI'/><category term='Game'/><category term='Planner'/><category term='Baddies'/><category term='Completed'/><category term='Library'/><category term='graphics'/><category term='ActionScript'/><category term='Misc'/><category term='Design'/><category term='xna'/><category term='Cocos2D'/><category term='Tutorial'/><category term='ObjectiveC'/><category term='Meanies'/><category term='notes'/><title type='text'>Magic Scrolls of Code</title><subtitle type='html'>"One types the correct incantation on the keyboard and the screen comes to life, showing things that never were or could be. We are modern day magicians"</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>34</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-868153115623870099</id><published>2012-02-13T12:26:00.001+01:00</published><updated>2012-02-13T12:26:12.793+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AI'/><category scheme='http://www.blogger.com/atom/ns#' term='Tutorial'/><title type='text'>State Machines By Example: A Useful FSM</title><content type='html'>&lt;h1&gt;Introduction&lt;/h1&gt; &lt;p&gt;A finite state machine is a highly useful tool for programmers in general and for video game programmers in particular. If you’re not familiar with the notion, think about the Finite State Machine (FSM) as a GameBoy, and the States as different cartridges or games.&lt;/p&gt; &lt;p&gt;The GameBoy can only have one game running at any time (or no game at all, in which case it’s not very useful). Obviously, all cartridges share some characteristics (shape, connection pins, etc.…) that allow the GameBoy to interact with any game without knowing or caring what it actually does, just plug it in and it works.&lt;/p&gt; &lt;p&gt;There is one last, important, characteristic that defines this system. Each “game” or state has the rules to decide when to change to another different game (this kind of fucks up the metaphor, but I could not find any better).&lt;/p&gt; &lt;p&gt;So, code-wise, this would be a system as follows:&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-8_wAsfeAriE/TzjzUewzdnI/AAAAAAAACIA/qaDCINCJ6j4/s1600-h/FSM5.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="FSM" border="0" alt="FSM" src="http://lh3.ggpht.com/-P5yV9RTkb8s/TzjzU-EfU2I/AAAAAAAACIE/cD2hu71vZLI/FSM_thumb3.png?imgmax=800" width="328" height="286"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;The FSM has one state active, which it updates every cycle. At any moment, the FSM can be told to change to a different state. All states inherit from the State class, and have a common interface so the FSM doesn’t need to know what they do internally, just call the appropriate functions. &lt;/p&gt; &lt;h2&gt;FSM&lt;/h2&gt; &lt;p&gt;A classic FSM pseudo-code has a structure as follows:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2ea97a9e-c456-439e-a0d1-f3225472eed6" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;class&lt;/span&gt; FSM&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Current state the FSM uses&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    State* currentState;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Calls the Update function of &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// the currentState&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    Update();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Calls exit of the currentState&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// sets newState as currentState&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// call enter of currentState&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    ChangeState(State * newState);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;};&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;h2&gt;&amp;nbsp;&lt;/h2&gt; &lt;h2&gt;State&lt;/h2&gt; &lt;p&gt;And a base state pseudo-code has the following structure:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fde0517b-e4f8-4145-9691-9bf20f44710d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;class&lt;/span&gt; State&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Pointer to the parent FSM&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    FSM* fsmReference;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Called when the state is initialized&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// by the FSM&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Enter();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Called when the state is ended by the&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// FSM&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Exit();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Called every update cycle by the FSM&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Update();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Actual states are not instantiated, only derived states (in our example ShootEnemy, Defend, etc. )&lt;/p&gt; &lt;h1&gt;Making it Useful&lt;/h1&gt; &lt;p&gt;This basic FSM has several problems or shortcomings that limit it’s use, so let’s have a look at some of them.&lt;/p&gt; &lt;h2&gt;Changing State at the Proper Time&lt;/h2&gt; &lt;p&gt;&lt;strong&gt;&lt;u&gt;Problem&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The main flaw the basic FSM pattern has is the fact that at any moment, the current state can request the FSM to change to a different state. When this happens, the following steps take place:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;CurrentState-&amp;gt;Exit();  &lt;li&gt;CurrentState = NewState;  &lt;li&gt;CurrentState-&amp;gt;Enter() // This is the new state&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;If the fsmReference-&amp;gt;ChangeState() call was in the middle of the the Update function, this can leave the state in an undefined state, and propagate errors later on.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;u&gt;Solution&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;We shall make a request mechanism in the FSM, instead of changing the state, we will request the FSM to change to a state, and the FSM &lt;em&gt;after&lt;/em&gt; the state update will change to a different state. For this we replace the ChangeState function for a RequestoReplaceState, and a DoStateReplacement.&lt;/p&gt; &lt;p&gt;Check this pseudo-code for a clearer idea:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4885c482-4117-4f5e-b57e-6870f9683dba" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;class&lt;/span&gt; FSM &lt;span style="color:#008000"&gt;// With delay state change&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt;:&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Current state the FSM uses&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    State* currentState;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Temporal pointer to the new state&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    State* newState;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Flag indicating wheter to change state&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;bool&lt;/span&gt; requestedStateChange;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Calls the Update function of &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// the currentState&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Update();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// If a requested state pending, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// change to new state&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoStateReplacement();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Calls exit of the currentState&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// sets newState as currentState&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// call enter of currentState&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; RequestReplaceState(State * newState);&lt;/li&gt; &lt;li&gt;};&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; FSM::Update() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(requestedStateChange)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        DoStateReplacement();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState != null)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        currentState-&amp;gt;Update();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; FSM::RequestReplaceState(SinState newState)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    nextState = newState;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    requestedStateChange = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; FSM::DoStateReplacement()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(nextState == null)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    requestedStateChange = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(currentState != null)&lt;/li&gt; &lt;li&gt;    { currentState-&amp;gt;Exit();    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    currentState = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.nextState;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    nextState = null;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    currentState-&amp;gt;Enter();&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;h2&gt;&amp;nbsp;&lt;/h2&gt; &lt;h2&gt;Push &amp;amp; Pop&lt;/h2&gt; &lt;p&gt;&lt;strong&gt;&lt;u&gt;Problem&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Now we have a FSM that won’t inadvertently introduce subtle ninja bugs when we’re not looking, and can replace the current state for another. But we want to add to the FSM the ability of when we change to a state, to push the current state back into memory, to be popped later on. This might be useful for example in this scenario:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Guard.state = patrolling –&amp;gt; Hears sound  &lt;ul&gt; &lt;li&gt;Guard.state = goSeeDisturbance , PushBack( patrolling )  &lt;li&gt;Guard –&amp;gt; Doesn’t find anything in sound source. Pop ( goSeeDisturbance ) &lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Guard.state = patrolling&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Instead of destroying the patrolling state and then having to create it again, we just push it back, store it, and once the temporal state (goSeeDisturbance) is over, we resume patrolling. &lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;u&gt;Solution&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;To properly do this we need to add a pause functionality to our states. Notice a very important detail that is that the Pause / Update / Resume functions, overloaded in the base classes, are no longer called by the FSM but instead we have Manager functions (ManagerUpdate, ManagerPause, ManagerResume) that are called by the FMS, while derived classes (from State) just override the same version without the “Manager” part. This allows us to add some extra functionality to all Update / Pause / Resume calls of all derived classes. With this, we can implement pause / resume, by letting the user specify what is going to happen in his state when it pauses / resumes, while doing the maintenance work (setting flags and safety checks) in the manager version. Check the code for a clearer idea:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:531dcc95-eb8f-4a23-b8b5-8539ba2b8fdf" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 400px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;class&lt;/span&gt; State&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Pointer to the parent FSM&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    FSM* fsmReference;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Indicates whether the state is paused&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;bool&lt;/span&gt; isPaused;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Overriden in base clases to put pause&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// logic&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Pause();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Overriden in base clases to put &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// resume logic&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Resume();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Called when the state is initialized&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// by the FSM&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Enter();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Called when the state is ended by the&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// FSM&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Exit();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// NOT CALLED BY FSM ANYMORE&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Update();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Called by the FSM every update &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// cycle&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ManagerUpdate();&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// CAlled by FSM on pause&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ManagerPause();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Called by FSM on resume&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ManagerResume();    &lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; State::ManagerUpdate()&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(!&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.isPaused) &lt;/li&gt; &lt;li&gt;    { &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Update(); }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; State::ManagerPause()&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.isPaused = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Pause();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; State::ManagerResume()&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.isPaused = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Resume();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;And add a push &amp;amp; pop mechanism to the FSM, very similar to the replace mechanism, but we store the pushed state in a variable, to restore it later. This is only an example, so it’s not very useful. For it to be useful we would need a list of pushed states, because that would allow us to push and pop as much as we wanted.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:73d853eb-fcbf-41c6-a77b-c7ce856b124d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 3em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;class&lt;/span&gt; FSM&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Current state the FSM uses&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    State* currentState;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Calls the Update function of &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// the currentState&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    Update();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Calls exit of the currentState&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// sets newState as currentState&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// call enter of currentState&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    ChangeState(State * newState);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Stores a pushed state. &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Make this-&amp;gt;a list to store a pile of states.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; State pushedState = null;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Indicates whether a new state push &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// has been requested.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;bool&lt;/span&gt; requestedStatePush&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Indicates whether a new state pop has &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// been requested.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;bool&lt;/span&gt; requestedStatePop;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoStatePop();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; RequestStatePop();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoStatePush();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; RequestPushState(State newState);&lt;/li&gt; &lt;li&gt;};&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; FSM::Update() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;requestedStateChange)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;DoStateReplacement();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;requestedStatePush)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;DoStatePush();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;requestedStatePop)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;DoStatePop();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState != null)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState.ManagerUpdate();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; FSM::RequestPushState(State newState)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;nextState = newState;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;requestedStatePush = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; FSM::DoStatePush()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;nextState == null)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        Log.Write(&lt;span style="color:#a31515"&gt;&amp;quot;ERROR! Attempting state push in null state&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState == null)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        Log.Write(&lt;span style="color:#a31515"&gt;&amp;quot;ERROR! Attempting state push back null state&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;requestedStatePush = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;pushedState = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;pushedState.ManagerPause();&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;nextState;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;nextState = null;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState.SetWasPushed(&lt;span style="color:#0000ff"&gt;true&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState.Enter();&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; FSM::RequestStatePop()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;requestedStatePop = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; FSM::DoStatePop()&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;pushedState == null)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Log.Write(&lt;span style="color:#a31515"&gt;&amp;quot;ERROR! Attempting state pop null state&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState != null)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState.Exit();&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;requestedStatePop = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;pushedState;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;pushedState = null;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    assert &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState != null;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;currentState.ManagerResume();&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h2&gt;Timed Update&lt;/h2&gt; &lt;p&gt;Last improvement we can add is a timed update, by simply passing a “update time” to each state constructor and making the State::ManagerUpdate keep track of it:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:63af0e45-0bfb-40d3-a025-7861df8d7431" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ManagerUpdate()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(!&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;isPaused) &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    { &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;updatePeriod == 0 || &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                CurrentTime() - &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;lastUpdateTime &amp;gt;= &lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;updatePeriod)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;Update();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;-&amp;gt;lastUpdateTime = CurrentTime();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;h1&gt;&amp;nbsp;&lt;/h1&gt; &lt;h1&gt;The Code&lt;/h1&gt; &lt;p&gt;It’s not “by example” if it doesn’t have an example, so here is a complete example in Java.&lt;/p&gt; &lt;h2&gt;Finite State Machine&lt;/h2&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:834fab15-f85b-4382-bed7-6494c0b0d58c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 3em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * State machine behavior to provided &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * different decision states.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * Supports push/pop and replace &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * mechanisms. (Stack of only 1 stored state.)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Supports safe-state switch via request.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; FSM&lt;/li&gt; &lt;li&gt;{    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Reference to current state.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; SinState currentState = null;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* Reference to next state requested.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; SinState nextState = null;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Indicates whether a new &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* state replace has been requested.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; boolean requestedStateChange = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Stores a pushed state. &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* Make this a list to store a pile of states.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; SinState pushedState = null;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Indicates whether a new state push &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* has been requested.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; boolean requestedStatePush = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Indicates whether a new state pop has &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* been requested.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; boolean requestedStatePop = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Initializes a new instance of the StateMachine &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* class.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; FSM() {}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Update the FSM, and the current state if any.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    @Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; action() &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.requestedStateChange)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.DoStateReplacement();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.requestedStatePush)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.DoStatePush();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.requestedStatePop)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.DoStatePop();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState != null)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState.ManagerUpdate();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Request to change the current state to a new one. &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* Request will be stored till FSM update.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* @param newState New state to change to.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; RequestReplaceState(SinState newState)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.nextState = newState;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.requestedStateChange = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* Do the actual state replace.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoStateReplacement()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        String prev = &lt;span style="color:#a31515"&gt;&amp;quot;Null&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        String next = &lt;span style="color:#a31515"&gt;&amp;quot;Null&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.nextState == null)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            Log.Write(&lt;span style="color:#a31515"&gt;&amp;quot;ERROR! Attempting state replacement to null state&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        next = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.nextState.name;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.requestedStateChange = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState != null)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState.Exit();&lt;/li&gt; &lt;li&gt;            prev = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState.name;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.nextState;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.nextState = null;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;/li&gt; &lt;li&gt;        assert &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState != null;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState.SetWasPushed(&lt;span style="color:#0000ff"&gt;false&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState.Enter();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Log.Write(&lt;span style="color:#a31515"&gt;&amp;quot;STATE CHANGE: &amp;quot;&lt;/span&gt; + prev + &lt;span style="color:#a31515"&gt;&amp;quot; --&amp;gt; &amp;quot;&lt;/span&gt; + next);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* Request to push the current state to &lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* storage and run another one.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* @param newState New state to run.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; RequestPushState(SinState newState)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.nextState = newState;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.requestedStatePush = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Do the actual state push.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoStatePush()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        String prev = &lt;span style="color:#a31515"&gt;&amp;quot;Null&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        String next = &lt;span style="color:#a31515"&gt;&amp;quot;Null&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.nextState == null)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            Log.Write(&lt;span style="color:#a31515"&gt;&amp;quot;ERROR! Attempting state push in null state&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;/li&gt; &lt;li&gt;        next = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.nextState.name;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState == null)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            Log.Write(&lt;span style="color:#a31515"&gt;&amp;quot;ERROR! Attempting state push back null state&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;/li&gt; &lt;li&gt;        prev = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState.name;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.requestedStatePush = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.pushedState = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.pushedState.ManagerPause();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.nextState;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.nextState = null;&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        assert &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState != null;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState.SetWasPushed(&lt;span style="color:#0000ff"&gt;true&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState.Enter();&lt;/li&gt; &lt;li&gt;        Log.Write(&lt;span style="color:#a31515"&gt;&amp;quot;STATE PUSH: &amp;quot;&lt;/span&gt; + prev + &lt;span style="color:#a31515"&gt;&amp;quot; --&amp;gt; &amp;quot;&lt;/span&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                + next + &lt;span style="color:#a31515"&gt;&amp;quot; [Pushed state: &amp;quot;&lt;/span&gt;+ &lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.pushedState.name +&lt;span style="color:#a31515"&gt;&amp;quot;]&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Request pop of current state and &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* restoring of the pushed one.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; RequestStatePop()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.requestedStatePop = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* Does the actual pop.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoStatePop()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        String prev = &lt;span style="color:#a31515"&gt;&amp;quot;Null&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        String next = &lt;span style="color:#a31515"&gt;&amp;quot;Null&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.pushedState == null)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            Log.Write(&lt;span style="color:#a31515"&gt;&amp;quot;ERROR! Attempting state pop null state&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        next = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.pushedState.name;&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState != null)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState.Exit();&lt;/li&gt; &lt;li&gt;            prev = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState.name;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.requestedStatePop = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.pushedState;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.pushedState = null;&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        assert &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState != null;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.currentState.ManagerResume();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Log.Write(&lt;span style="color:#a31515"&gt;&amp;quot;STATE POP: &amp;quot;&lt;/span&gt; + prev + &lt;span style="color:#a31515"&gt;&amp;quot; --&amp;gt; &amp;quot;&lt;/span&gt; + next);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;h2&gt;&amp;nbsp;&lt;/h2&gt; &lt;h2&gt;State&lt;/h2&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d89d1137-0ec1-4862-9780-311d909e3392" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Base class for states.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * Supports state pause.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; SinState &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* Whether the state is paused.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; boolean isPaused;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Update period of the state.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; updatePeriod;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* Last update period.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;long&lt;/span&gt; lastUpdateTime;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Reference to the FSM&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; StateMachine FSM;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* State name.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; String name;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* Whether the state was pushed.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; boolean wasPushed;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;* Initializes an instance of the SinState class.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;* @param updatePeriod The time (in ms) between &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;     &lt;span style="color:#008000"&gt;*  each update. Set 0 for going as fast as possible.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;     &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; SinState(&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; updatePeriod, StateMachine FSM, String name) &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.isPaused = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.updatePeriod = updatePeriod;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.lastUpdateTime = System.currentTimeMillis();&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.FSM = FSM;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.name = name;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Override functions&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Enter();&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Exit();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Update();&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Pause();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Resume();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Manager functions&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ManagerUpdate()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(!&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.isPaused) &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        { &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.updatePeriod == 0 || &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    System.currentTimeMillis() - &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.lastUpdateTime &amp;gt;= &lt;/li&gt; &lt;li&gt;                    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.updatePeriod)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Update();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.lastUpdateTime = System.currentTimeMillis();&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ManagerPause()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.isPaused = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Pause();&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ManagerResume()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.isPaused = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Resume();&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.lastUpdateTime = System.currentTimeMillis();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; SetWasPushed(boolean pushed) &lt;/li&gt; &lt;li&gt;    { &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.wasPushed = pushed; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; boolean WasPushed() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.wasPushed; }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h1&gt;Conclusion&lt;/h1&gt;    &lt;p&gt;Finite states machines are a very useful construct for controlling the flow of a game, how your game scene updates in different situations, how your character acts… It’s not a holly grail, all purpose solution, but it works surprisingly well for many scenarios.&lt;/p&gt; &lt;p&gt;Enjoy.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-868153115623870099?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/868153115623870099/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2012/02/state-machines-by-example-useful-fsm.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/868153115623870099'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/868153115623870099'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2012/02/state-machines-by-example-useful-fsm.html' title='State Machines By Example: A Useful FSM'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-P5yV9RTkb8s/TzjzU-EfU2I/AAAAAAAACIE/cD2hu71vZLI/s72-c/FSM_thumb3.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-5337949680087788582</id><published>2012-02-04T21:49:00.001+01:00</published><updated>2012-02-04T21:49:10.115+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='Cocos2D'/><category scheme='http://www.blogger.com/atom/ns#' term='Library'/><category scheme='http://www.blogger.com/atom/ns#' term='android'/><title type='text'>More advanced concepts of getting cocos2dx working on Android from Windows 7</title><content type='html'>&lt;p&gt;&lt;br&gt;NOTE: This tutorial was done with cocos2d-1.0.1-x-0.9.1 and cocos2d-1.0.1-x-0.11.0, and so may not be 100% accurate for either version, or newer [Had to change version in the middle of development], but will serve as a general guideline for anyone using similar versions.&lt;/p&gt; &lt;p&gt;First Part: &lt;a href="http://magicscrollsofcode.blogspot.com/2011/09/quick-and-dirty-guide-to-getting.html"&gt;Quick and dirty guide to getting cocos2dx working on Android from Windows 7&lt;/a&gt;&lt;/p&gt; &lt;h1&gt;Introduction&lt;/h1&gt; &lt;p&gt;Working from the files we obtained in the first part, in this tutorial we shall cover:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Moving android-generated folder away from the cocos2d-x folder.&lt;/li&gt; &lt;li&gt;Add an extra library ( Chipmunk ).&lt;/li&gt; &lt;li&gt;Not having to use the Classes &amp;amp; Resources folders.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;It’s important to use the proper editing tool for the files generated by the “create-android-project.bat”, because regular windows editors add extra symbols like “\r” that will confuse your Cygwin. I recommend &lt;a href="http://notepad-plus-plus.org/"&gt;Notepad++&lt;/a&gt;.&amp;nbsp; A great editor that has the invaluable property of showing hidden symbols by doing: View -&amp;gt; Show symbol -&amp;gt; Show all characters.&lt;/p&gt; &lt;h1&gt;Moving The Folder&lt;/h1&gt; &lt;p&gt;I generated a new project FOLLOWING THE STEPS IN THE PART ONE OF THE TUTORIAL, let’s call it FVZ, using the following specs:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Package: com.companyname.fvz&lt;/li&gt; &lt;li&gt;Name: FVZ_2_3&lt;/li&gt; &lt;li&gt;Target ID: 13 (Android 2.3.3)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;And copied it from the cocos2d-x folder where it’s generated by default, to the folder where my Visual Studio solution held all my code of the project I was working on. To make this work, I had to modify: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;android/build_native.sh &lt;/li&gt; &lt;ul&gt; &lt;li&gt;GAME_ROOT=/cygdrive/d/All/Proyects/FVZ/Android/FVZ_2_3 &lt;br&gt;( Set the path to where we have copied the project folder, and add the /cygdrive/ in front so Cygwin can use the path ).&lt;br&gt;&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;android/jni/Android.mk&lt;/li&gt; &lt;ul&gt; &lt;li&gt;Modify project path: addprefix $(LOCAL_PATH)/../../../../FvZv2/ to point to the root folder of the cocos2dx instaltion (where you have all the cocos2dx code, ej: cocos2dx, CocosDenshion, tests, tools…&lt;br&gt;&lt;br&gt;What this does is search for all the Android.mk (makefiles) of the libraries you are going to use. (Notice the addprefix &amp;amp; addsufix functions, to generate the full paths).&lt;br&gt;&lt;br&gt;# Try to use LOCALPATH, otherwise you’ll have to play a bit with the path till you get it to Cygwin’s liking.&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;android/jni/helloworld&lt;/li&gt; &lt;ul&gt; &lt;li&gt;LOCAL_SRC_FILES: Add RELATIVE path to each code file (cpp / h) in your Visual Studio project folder. The start of my list is, for example:&lt;/li&gt; &lt;ul&gt; &lt;p&gt;LOCAL_SRC_FILES := main.cpp \&lt;br&gt;../../../../../FvZv2/FvZ/Classes/AppDelegate.cpp \&lt;br&gt;../../../../../FvZv2/FvZ/Classes/AppDelegate.h \&lt;br&gt;../../../../../FvZv2/FvZ/Classes/HelloWorldScene.cpp …&lt;/p&gt;&lt;/ul&gt; &lt;li&gt;LOCAL_C_INCLLUDES: Change Classes path to the folder where you keep all your code, and set the paths for the different cocos folders so they point to the required folders.&lt;/li&gt; &lt;li&gt;LOCAL_LDLIBS: Point the $(LOCAL_PATH)/../../libs/$(TARGET_ARCH_ABI)) to the proper location (TARGET_ARCH_ABI is usually armeabi).&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;andoid/build_native.sh&lt;/li&gt; &lt;ul&gt; &lt;li&gt;Change GAME_ROOT so it points to the root of your game folder, in my case:&lt;br&gt;GAME_ROOT=/cygdrive/d/All/Proyects/FVZ/Android/FVZ_2_1&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt; &lt;p&gt;With all that your project should compile with no problems using Cygwin. As a special note, I made a java script to parse my Classes folder, to generate my LOCAL_SRC_FILES paths, as there where hundreds of files, in nested folders and so on. Sharing it here:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e868f3b1-b415-4ad2-bec2-221dab3cc8c9" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;import java.io.File;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; ListFiles &lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; String initialPath = &lt;span style="color:#a31515"&gt;&amp;quot;D:&amp;#92;&amp;#92;All&amp;#92;&amp;#92;Proyects&amp;#92;&amp;#92;FvZ&amp;#92;&amp;#92;FvZv2&amp;#92;&amp;#92;FvZ&amp;#92;&amp;#92;Classes&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; main(String[] args) &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        FileTreePrint(&lt;span style="color:#a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; FileTreePrint(String path)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        String files;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        File folder = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; File(initialPath + path);&lt;/li&gt; &lt;li&gt;        File[] listOfFiles = folder.listFiles(); &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; listOfFiles.length; i++) &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (listOfFiles[i].isFile()) &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                files = listOfFiles[i].getName();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                System.out.println(&lt;span style="color:#a31515"&gt;&amp;quot;../../../../../FvZv2/FvZ/Classes&amp;quot;&lt;/span&gt;+ path.replace(&lt;span style="color:#a31515"&gt;&amp;quot;&amp;#92;&amp;#92;&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;/&amp;quot;&lt;/span&gt;) + &lt;span style="color:#a31515"&gt;&amp;quot;/&amp;quot;&lt;/span&gt; +files+ &lt;span style="color:#a31515"&gt;&amp;quot; &amp;#92;&amp;#92;&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; listOfFiles.length; i++) &lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (listOfFiles[i].isDirectory()) &lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                FileTreePrint(path + &lt;span style="color:#a31515"&gt;&amp;quot;&amp;#92;&amp;#92;&amp;quot;&lt;/span&gt; +listOfFiles[i].getName());&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;h1&gt;&amp;nbsp;&lt;/h1&gt; &lt;h1&gt;Adding Chipmunk to the Compilation&lt;/h1&gt; &lt;p&gt;We need to modify some files, but it’s a straightforward process if we take another library (CocosDenshion) as a guideline.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;android/jni/Android.mk&lt;/li&gt; &lt;ul&gt; &lt;li&gt;Add “chipmunk \” in the line under CocosDenshion/android.&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;android/jni/Application.mk&lt;/li&gt; &lt;ul&gt; &lt;li&gt;My APP_MODULES looks like:&lt;br&gt;APP_MODULES := cocos2d cocosdenshion chipmunk game&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;android/jni/helloworld&lt;/li&gt; &lt;ul&gt; &lt;li&gt;LOCAL_C_INCLLUDES: Add the path to the chipmunk folder that holds all the files, in my case:&lt;br&gt;$(LOCAL_PATH)/../../../../../FvZv2/chipmunk/include/chipmunk \&lt;/li&gt; &lt;li&gt;In LOCAL_LDLIBS add –lchipmunk&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt; &lt;p&gt;Lastly, there is one extra modification to be done, that took me quite a while. In the main &lt;strong&gt;java&lt;/strong&gt; file of the .java files generated for your project (in my case FVZ_2_3.java) we need to search for where it does System.loadLibrary(), and add:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;System.loadLibrary("chipmunk");&lt;br&gt;&lt;/li&gt;&lt;!--EndFragment--&gt;&lt;/ul&gt; &lt;h1&gt;Automating the Process&lt;/h1&gt; &lt;p&gt;The first thing you will notice with this system is that it’s SLOW. We have have to use the command line every time we want to compile, not to mention that this method copies all the resources of the game from the Resources folder to the assets one each time, which can be very time consuming.&lt;/p&gt; &lt;p&gt;So let’s optimize it a bit. &lt;/p&gt; &lt;h2&gt;Making the resource load faster&lt;/h2&gt; &lt;p&gt;Grab your build_native.sh and clean out all the logic for cleaning the resource folder, leaving it like this:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a3950b6c-78b9-4cf3-85a3-4f6fd482dc21" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;# set params&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;ANDROID_NDK_ROOT=/cygdrive/c/eclipse/android/android-ndk-r6b&lt;/li&gt; &lt;li&gt;GAME_ROOT=/cygdrive/d/All/Proyects/FVZ/Android/FVZ_2_1&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;GAME_ANDROID_ROOT=$GAME_ROOT/android&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;# build&lt;/li&gt; &lt;li&gt;$ANDROID_NDK_ROOT/ndk-build -C $GAME_ANDROID_ROOT $*&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;What is going to happen now is that you’re going to have to remember to manually copy each modified asset from your MVS project to the assets folder of the java project. A small price to pay for compiling in 10 seconds instead of 1 minute.&lt;/p&gt; &lt;p&gt;So go ahead and copy all your resources to the assets folder. Once you’re done…&lt;/p&gt; &lt;h2&gt;One Click Process&lt;/h2&gt; &lt;p&gt;I created a batch file to be called with a simple double click, that calls cygwin and gets the whole compilation process done. After double clicking on it you just need to refresh the java project in eclipse (F5) and hit “run” to have the game showing in your android. Here is the magical .bat:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7abc32e3-b359-4187-a84e-a1d3f0a59f5b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;C:&amp;#92;cygwin&amp;#92;bin&amp;#92;bash.exe -l &lt;span style="color:#a31515"&gt;&amp;#39;/cygdrive/d/All/Proyects/FvZ/Android/FVZ_2_1/android/build_native.sh&amp;#39;&lt;/span&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;pause&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Created this Compile.bat thanks to &lt;a href="http://forums.techguy.org/windows-xp/424616-calling-unix-scripts-dos-script.html"&gt;http://forums.techguy.org/windows-xp/424616-calling-unix-scripts-dos-script.html&lt;/a&gt; and &lt;a href="http://old.nabble.com/Cygwin---batch-file-td15088393.html"&gt;http://old.nabble.com/Cygwin---batch-file-td15088393.html&lt;/a&gt; .&lt;/p&gt; &lt;h1&gt;Conclusion&lt;/h1&gt; &lt;p&gt;Now you should be able to put your folder wherever you damn well please, add other libraries to it and compile by:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Compiling in MVS by hitting F5 (to check it works)&lt;/li&gt; &lt;li&gt;Compiling in native code by double clicking Compile.bat&lt;/li&gt; &lt;li&gt;Running the game in android by hitting F5 in eclipse, and then “run”.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Tell me if it worked for you!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-5337949680087788582?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/5337949680087788582/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2012/02/more-advanced-concepts-of-getting.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/5337949680087788582'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/5337949680087788582'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2012/02/more-advanced-concepts-of-getting.html' title='More advanced concepts of getting cocos2dx working on Android from Windows 7'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-4840222295843011162</id><published>2011-09-30T17:54:00.002+02:00</published><updated>2012-02-04T21:50:19.814+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='Cocos2D'/><category scheme='http://www.blogger.com/atom/ns#' term='Library'/><category scheme='http://www.blogger.com/atom/ns#' term='android'/><title type='text'>Quick and dirty guide to getting cocos2dx working on Android from Windows 7</title><content type='html'>&lt;p&gt;Second Part: &lt;a href="http://magicscrollsofcode.blogspot.com/2012/02/more-advanced-concepts-of-getting.html"&gt;More advanced concepts of getting cocos2dx working on Android from Windows 7&lt;/a&gt; &lt;/p&gt;&lt;p&gt;This guide builds on the &lt;a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_1_-_How_to_Create_a_New_cocos2d-x_project_on_multi-platforms#4-Start-with-Android"&gt;official guide&lt;/a&gt; for setting up Cocos2d-x on the Android Phone with additional explanations in the sections where the original one is outdated, incomplete or ambiguous, so hopefully no one else has to make the same mistakes I did. &lt;/p&gt; &lt;p&gt;First a general overview of what has to happen to see your precious game on the android phone. You create a Visual Studio project from a cocos2dx template and set it up to work with cocos2dx. Once you've got your game running and ready to test on the phone, you use a .bat file provided in the cocos2dx download to create a default android project from the "HelloWorld" project it comes with. In this new folder it creates in the android root, you override the existing classes and resources with the ones from your project, and you make sure they are in the makefile. Then you use Cygwin to compile everything into native code, and finally you create a new android project in eclipse, import the code you compiled and hopefully have it work in the phone.&lt;/p&gt; &lt;p&gt;It's a non-trivial process, but not that hard either, and the people at Cocos2dx have made quite an effort to get this working as it is. So, step by step.&lt;/p&gt; &lt;h1&gt;Step by Step&lt;/h1&gt; &lt;h2&gt;Step 1: Get all the android material&lt;/h2&gt; &lt;p&gt;Download android sdk, install it and test it in eclipse. This is a well-documented process in the &lt;a href="http://developer.android.com/sdk/installing.html"&gt;android sdk website&lt;/a&gt;.&lt;br /&gt;If you have an Android phone to test on, &lt;a href="http://developer.android.com/guide/developing/device.html"&gt;set it up for development&lt;/a&gt; and try a test application in it.&lt;br /&gt;Download the &lt;a href="http://developer.android.com/sdk/ndk/index.html"&gt;android ndk&lt;/a&gt; and extract it to a folder with no spaces in the path.&lt;/p&gt; &lt;h2&gt;Step 2: Get all the cocos2dx material&lt;/h2&gt; &lt;p&gt;Download the latest version of cocos2d-x and set it up for win32 development as stated &lt;a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Cocos2d-x_Application_Wizard_for_Visual_Studio_User_Guide"&gt;here&lt;/a&gt;. Download &lt;a href="http://www.cygwin.com/install.html"&gt;Cygwin&lt;/a&gt; and make sure you mark the "Devel" branch as "Install" when you download it.&lt;/p&gt; &lt;h2&gt;Step 3: Get the create-android-project.bat working&lt;/h2&gt; &lt;p&gt;From the &lt;a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_1_-_How_to_Create_a_New_cocos2d-x_project_on_multi-platforms#411-Create-project"&gt;original tutorial&lt;/a&gt;:&lt;/p&gt; &lt;p&gt;To adapt to my environment, I change the settings in create-android-project.bat.&lt;br /&gt;set _CYGBIN=C:\cygwin\bin&lt;br /&gt;The path of cygwin bin&lt;br /&gt;set _ANDROIDTOOLS=D:\anroid\android-sdk-windows\tools&lt;br /&gt;The path of android sdk tools&lt;br /&gt;set _NDKROOT=D:\anroid\android-ndk-r5b&lt;br /&gt;The root of ndk&lt;/p&gt; &lt;h2&gt;Step 4: Creating the project&lt;/h2&gt; &lt;p&gt;Now we create our Cocos2d-win Application form the MVS templates. If this template is not available, go back and check you did step 2 correctly.  You can create this project wherever you want. For the sake of brevity, let’s assume you are creating a Pong game, and aptly name your project “Pong”. While following the cocos2dx creation template unmark every cocos2d library except Simple Audio Engine in Cocos Denshion.&lt;/p&gt; &lt;p&gt;&lt;br /&gt;Next up we need to add 3 things to make the Pong project reach the cocos2dx files it needs. Include the source files, link the library files, and copy the dll files. To make this process easier &lt;a href="http://www.itechtalk.com/thread3595.html"&gt;create an environment variable&lt;/a&gt; with the root of your cocos2dx installation. I call mine COCOS2DX and it’s defined as C:\eclipse\android\cocos2d-1.0.1-x-0.9.1\cocos2d-1.0.1-x-0.9.1. &lt;/p&gt; &lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: If you modify this variable, or any other, and want to use it from visual studio, you need to restart visual studio.&lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;h2&gt;Step 4.1: Include the source files&lt;/h2&gt; &lt;p&gt;Inside Visual Studio, with your Pong project open, right click on the project and select Properties. At the top make sure you select “Configuration: All Configurations”, otherwise you’ll have to repeat the whole process for the each configuration you’ve got (usually just debug and release).&lt;br /&gt;Go to C/C++ / General /Additional Include Directories, there your template should have 9 includes already.  Something like this: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;.;&lt;/li&gt; &lt;li&gt;.\win32;&lt;/li&gt; &lt;li&gt;.\Classes;&lt;/li&gt; &lt;li&gt;..\cocos2dx;&lt;/li&gt; &lt;li&gt;..\cocos2dx\include;&lt;/li&gt; &lt;li&gt;..\cocos2dx\platform;&lt;/li&gt; &lt;li&gt;..\cocos2dx\platform\third_party\win32\OGLES;&lt;/li&gt; &lt;li&gt;..\CocosDenshion\Include;&lt;/li&gt; &lt;li&gt;%(AdditionalIncludeDirectories)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;br /&gt;The first 3 you can leave as-is, because they reference the project locally (including what’s in the project folder and in the win32 and classes folders. As you need these files, all is good. The next 5 however, all the ones that start with “..\cocos2dx”, assume you’re in the root cocos2dx folder. So we just correct this using our COCOS2DX environment variable. &lt;/p&gt; &lt;ul&gt; &lt;li&gt;.;&lt;/li&gt; &lt;li&gt;.\win32;&lt;/li&gt; &lt;li&gt;.\Classes;&lt;/li&gt; &lt;li&gt;$(COCOS2DX)\cocos2dx;&lt;/li&gt; &lt;li&gt;$(COCOS2DX)\cocos2dx\include;&lt;/li&gt; &lt;li&gt;$(COCOS2DX)\cocos2dx\platform;&lt;/li&gt; &lt;li&gt;$(COCOS2DX)\cocos2dx\platform\third_party\win32\OGLES;&lt;/li&gt; &lt;li&gt;$(COCOS2DX)\CocosDenshion\Include;&lt;/li&gt; &lt;li&gt;%(AdditionalIncludeDirectories)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;br /&gt;To be honest, I don’t quite know what the AdditionalIncludeDirectories are, so I just leave it there.&lt;br /&gt;With this however, your project knows where the cocos2dx files it needs are.&lt;br /&gt;&lt;/p&gt; &lt;h2&gt;Step 4.2 : Adding the libraries&lt;/h2&gt; &lt;p&gt;You need the .lib files that where created by compiling the cocos2d-win32.vc2010.sln, all the way back at step 2. We are going to store them locally for clarity, though you could just modify the linker path using our COCOS2DX variable. &lt;/p&gt; &lt;p&gt;In the Pong folder, next to the Classes and Resource folders, we create a Libs folder, and inside put two subfolders,  Debug and Release.&lt;br /&gt;Next we copy all the .lib files from our COCOS2DX/Debug.win32 and COCOS2DX/Release.win32 folders to the Debug and Release folders respectively. &lt;/p&gt; &lt;p&gt;&lt;br /&gt;Inside Visual Studio, with your Pong project open, right click on the project and select Properties. At the top make sure you select “Configuration: All Configurations”, otherwise you’ll have to repeat the whole process for the each configuration you’ve got (usually just debug and release).&lt;br /&gt;Go to Linker / General / Additional Library Directories and you should find something like:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;$(OutDir);&lt;/li&gt; &lt;li&gt;%(AdditionalLibraryDirectories)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;We need to add the library path, so we make it:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;$(OutDir);&lt;/li&gt; &lt;li&gt;$(SolutionDir)\Pong\Libs\$(Configuration);&lt;/li&gt; &lt;li&gt;%(AdditionalLibraryDirectories)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;As long as you only have Debug and Release configurations this should work fine. Visual will go to Pong/Libs/Debug or Pong/Libs/Release and get the .lib files from there when needed. If you add more configurations, make sure to add the appropriate folder to Libs. If your project is not named Pong, modify the path accordingly.&lt;br /&gt;Right now it should let you compile the project without errors but not run it. We’re missing the dlls.&lt;br /&gt;&lt;/p&gt; &lt;h2&gt;Step 4.3: Adding dlls&lt;/h2&gt; &lt;p&gt;First build your application both in release and in debug. This should create in your visual studio project debug and release folders with an executable called Pong.exe, and maybe a few other things. &lt;/p&gt; &lt;p&gt;Go back to the COCOS2DX/Debug.win32 and COCOS2DX/Release.win32 folders and fish out all the .dll files copying them to the Debug and Release folders in your Pong project folder respectively.&lt;br /&gt;There. You should be able to build and run, seeing your project on a small window. &lt;/p&gt; &lt;h2&gt;Step 5: Use the .bat to create an android project&lt;/h2&gt; &lt;p&gt;This is pretty well covered in the &lt;a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_1_-_How_to_Create_a_New_cocos2d-x_project_on_multi-platforms#411-Create-project"&gt;original tutorial&lt;/a&gt;. Click the create-android-project.bat and fill out the package path (org.cocos2dx.Pong in our case) and the name (Pong) and finally the version to compile to (a list of versions should appear in screen).&lt;/p&gt; &lt;p&gt;&lt;br /&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: This step is irrelevant of your visual studio project, you don’t need to copy your Pong visual studio project to the cocos2dx root or anything. The .bat creates a folder all by itself.&lt;br /&gt;&lt;/p&gt; &lt;h2&gt;Step 6: Fill in the meat&lt;/h2&gt; &lt;p&gt;This is the step that really had me stumped until I asked in the forums. The  .bat creates a new project based on the HelloWorld example in the root directory, so if you just run the bat and continue to the next step, in the end you’ll have the HelloWorld project in your phone instead of the Pong game. &lt;/p&gt; &lt;p&gt;&lt;br /&gt;Once you have the Pong folder created by the .bat in the root cocos2dx folder, you need to copy the content of your Classes folder in your visual studio Pong project to the cocos2dx Pong Classes folder, substituting the contents, and do the same with the Resources folder.&lt;/p&gt; &lt;p&gt;&lt;br /&gt;Now the files are in the appropriate place, but we still need to tell tel the makefile to take them into consideration when compiling, so we go to &lt;strong&gt;COCOS2DX\Pong\android\jni\helloworld\Android.mk&lt;/strong&gt; and modify the makefile. You’ll notice that near the top of the file there is a LOCAL_SRC_FILES that already includes the path of the main.cpp and the 2 HelloWorld project files. Leave the main but override the rest so it has the paths of the files you added.&lt;/p&gt; &lt;p&gt;In my case:&lt;br /&gt;LOCAL_SRC_FILES := main.cpp \&lt;br /&gt;../../../Classes/AppDelegate.cpp \&lt;br /&gt;../../../Classes/Elements/Ball.cpp \&lt;br /&gt;../../../Classes/Elements/Stick.cpp \&lt;br /&gt;../../../Classes/Scenes/PlayScene.cpp \&lt;br /&gt;../../../Classes/Scenes/TitleScreen.cpp&lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;h2&gt;Step 6: Run Cygwin to compile&lt;/h2&gt; &lt;p&gt;Just like the &lt;a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_1_-_How_to_Create_a_New_cocos2d-x_project_on_multi-platforms#412-Build-native-code"&gt;original tutorial&lt;/a&gt; explains&lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;h2&gt;Step 7: Create a project in eclipse and import the Pong project&lt;/h2&gt; &lt;p&gt;Just like the &lt;a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_1_-_How_to_Create_a_New_cocos2d-x_project_on_multi-platforms#413-Import-project-to-eclipse"&gt;original tutorial&lt;/a&gt; explains. &lt;/p&gt;&lt;p&gt;Second Part: &lt;a href="http://magicscrollsofcode.blogspot.com/2012/02/more-advanced-concepts-of-getting.html"&gt;More advanced concepts of getting cocos2dx working on Android from Windows 7&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-4840222295843011162?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/4840222295843011162/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/09/quick-and-dirty-guide-to-getting.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/4840222295843011162'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/4840222295843011162'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/09/quick-and-dirty-guide-to-getting.html' title='Quick and dirty guide to getting cocos2dx working on Android from Windows 7'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-4667171154857834962</id><published>2011-08-27T22:36:00.001+02:00</published><updated>2011-08-27T22:36:12.023+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><category scheme='http://www.blogger.com/atom/ns#' term='xna'/><title type='text'>Baddies 4–Saving</title><content type='html'>&lt;h1&gt;Introduction&lt;/h1&gt; &lt;h2&gt;Objective&lt;/h2&gt; &lt;p&gt;The objective of the saving system is, given a node, any node, serialize it completely and automatically without having to go through the mistake prone method of each time we add a member, having to remember to update the load and save functions.&lt;/p&gt; &lt;p&gt;To this aim, we will use the IntermediateSerializer class, with a few auxiliary classes to handle logical exceptions in our design and in the IntermediateSerializer class.&lt;/p&gt; &lt;h2&gt;Considered options&lt;/h2&gt; &lt;p&gt;There are several options when saving the game in XNA, the 3 more common ones are serializing using the XmlSerializer class, serializing using the IntermediateSerializer class, and finally, saving with a BinaryWriter class.&lt;/p&gt; &lt;p&gt;The reasons why I won’t use the XmlSerializer are explained in the conclusion in &lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/xna-serialization-to-xml-tutorial.html"&gt;this post&lt;/a&gt;, so I won’t expand on it. The BinaryWriter, on the other hand, requires knowing the data you are going to read beforehand, as it’s in binary format and you need to select “chunks” of specific lengths to be interpreted as variables. This does not work well with the initial objective of the saving being automatic or not having to make custom save / load functions for each class. This leaves us with the IntermediateSerializer.&lt;/p&gt; &lt;h1&gt;Serialization&lt;/h1&gt; &lt;h2&gt;Serializing with the IntermediateSerializer&lt;/h2&gt; &lt;p&gt;The use of the IntermediateSerializer is best documented in these 2 articles by Shawn Hargraves: &lt;a href="http://blogs.msdn.com/b/shawnhar/archive/2008/08/12/teaching-a-man-to-fish.aspx"&gt;Teaching a man to fish&lt;/a&gt;, and &lt;a href="http://blogs.msdn.com/b/shawnhar/archive/2008/08/12/everything-you-ever-wanted-to-know-about-intermediateserializer.aspx"&gt;Everything you ever wanted to know about IntermediateSerializer&lt;/a&gt;. &lt;/p&gt; &lt;p&gt;On the positive side, it’s simple (doesn’t require the multithreading gymnastics for the container that the XmlSerializer needs), and highly customizable (adding private members, excluding public members, setting members as references, dealing with collections, etc…). On the negative side, it only works on windows, but that’s an acceptable inconvenient, and it needs the full .Net Framework 4.0 instead of the client version (&lt;a href="http://blogs.msdn.com/b/nicgrave/archive/2010/11/24/build-time-content-pipeline-usage-in-windows-games.aspx"&gt;explanation&lt;/a&gt;).&lt;/p&gt; &lt;h2&gt;Working with the Node&lt;/h2&gt; &lt;p&gt;For our specific situation, we want to serialize a subtree of nodes, this nodes being any class that might derive from node. For example, we might have a situation like this:&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-j4VVta8n518/TllVNBbit8I/AAAAAAAACDo/DZeaRNJcHwg/s1600-h/Saving1%25255B2%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="Saving1" border="0" alt="Saving1" src="http://lh6.ggpht.com/-8Cl0u42J_qI/TllVNsdSYdI/AAAAAAAACDs/elLYhoiY7Gw/Saving1_thumb.png?imgmax=800" width="201" height="157"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;This diagram represents that there is a root node “Scene” with two added child nodes “Camera” and “Map” (ignore the UML notation, it was the only editor at hand). We want to serialize Scene without caring about what hangs from it and have everything serialized to XML.&lt;/p&gt; &lt;p&gt;The first attempt at serializing this launched an error of cyclic reference, as each Node has a pointer to it’s parent. To solve this we declared the parent member of a node as a &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.content.contentserializerattribute.sharedresource.aspx"&gt;ContentSerializerAttribute.SharedResource&lt;/a&gt; so instead of storing the parent, it just stored a reference to this. This serialized, but upon closer investigation of the XML code it became apparent that this generated “shadow” parents, as we had both the original parent that called the serialization, and the one each child was referencing, resulting in something like this:&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-qTjWw39IFBQ/TllVN0wL_pI/AAAAAAAACDw/2WvIvBnGK48/s1600-h/Ssaving2%25255B3%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="Ssaving2" border="0" alt="Ssaving2" src="http://lh5.ggpht.com/-VdQrxKhciLE/TllVOhwFemI/AAAAAAAACD0/D5CBK0iM_oI/Ssaving2_thumb%25255B1%25255D.png?imgmax=800" width="267" height="173"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;That’s because the children of each node are not references themselves, if they were we’d just have a very slim XML tree with just references, no content, and then the references at the bottom. Something like this:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8ce5f987-8b61-4605-9808-54dd8db7cb97" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;1.0&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;?&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;XnaContent&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;xmlns:Utils&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Baddies.Utils&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;&lt;/span&gt;&lt;span style="color:#ff0000"&gt;xmlns:Scenes&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Baddies.Scenes&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;&lt;/span&gt;&lt;span style="color:#ff0000"&gt;xmlns:Nodes&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Baddies.Nodes&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000"&gt; Root of the tree&lt;/span&gt;&lt;span style="color:#0000ff"&gt;--&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Asset&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;Type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Utils:NodeSerializer&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;#Resource1&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Asset&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000"&gt; References&lt;/span&gt;&lt;span style="color:#0000ff"&gt;--&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Resources&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000"&gt; Scene node&lt;/span&gt;&lt;span style="color:#0000ff"&gt;--&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Resource&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;#Resource1&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;Type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Scenes:Scene&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Node&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;ParentRelativePos&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;false&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;ParentRelativePos&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Children&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;Type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Utils:SharedResourceDictionary[int,Nodes:Node]&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Item&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;          &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Key&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;0&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Key&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;          &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Value&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;#Resource2&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Value&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Item&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Item&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;          &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Key&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;1&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Key&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;          &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Value&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;#Resource3&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Value&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Item&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Children&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Visible&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Visible&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Position&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;-0 -0 -0&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Position&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Parent&lt;/span&gt;&lt;span style="color:#0000ff"&gt; /&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Resource&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000"&gt; Camera node &lt;/span&gt;&lt;span style="color:#0000ff"&gt;--&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Resource&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;#Resource2&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;Type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Baddies.Camera&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Cam&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;ParentRelativePos&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;false&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;ParentRelativePos&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Children&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;Type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Utils:SharedResourceDictionary[int,Nodes:Node]&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; /&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Visible&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Visible&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Position&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;0 0 0&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Position&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Parent&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;#Resource1&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Parent&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;W&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;673&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;W&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;H&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;442&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;H&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;DisplayBorder&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;DisplayBorder&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Resource&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000"&gt; Map node &lt;/span&gt;&lt;span style="color:#0000ff"&gt;--&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Resource&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;#Resource3&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;Type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Baddies.Map.MapGrid&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Map&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;ParentRelativePos&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;ParentRelativePos&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Children&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;Type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Utils:SharedResourceDictionary[int,Nodes:Node]&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; /&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Visible&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Visible&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Position&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;0 0 0&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Position&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Parent&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;#Resource1&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Parent&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;DisplayTerrain&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;DisplayTerrain&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;DisplayGrid&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;DisplayGrid&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;DisplayCollisionMap&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;false&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;DisplayCollisionMap&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;MapWidth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;16&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;MapWidth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;MapHeight&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;16&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;MapHeight&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;TileSize&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;16&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;TileSize&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Tiles&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000"&gt; Map tiles excuded for brevity &lt;/span&gt;&lt;span style="color:#0000ff"&gt;--&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Tiles&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;tileSetTex&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;TileMap/tileset1&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;contentRef&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;#Resource3&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;contentRef&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;tileSetTex&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Resource&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Resources&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;XnaContent&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;To achieve that structure we need to set the children of the Node as references as well. The children of each node are kept in a dictionary, so to solve this “dictionary of shared resources” dilemma, I wrote a class to handle it, explained in &lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/dictionary-of-shared-resources-for.html"&gt;this article&lt;/a&gt;. This is one of the few exceptions to the “not writing custom code for saving different data” but only because the IntermediateSerializer doesn’t support these type of dictionaries out of the box.&lt;/p&gt; &lt;p&gt;Another exception is that even if we mark everything as references, the first node to be serialized is not considered a reference because it’s the starting point of the serializer. To solve this I made a wrapper class to serialize that only contains the root node, as a reference.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c864cf58-c062-4e37-9e0b-53efddc7876e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Auxiliary class that takes care of &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; the serialization of a Node tree.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;remarks&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Works by having a reference to the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; node and serializing itself, so the root&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; node is also regarded as a reference.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/remarks&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;NodeSerializer&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Root node to serialize.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    [&lt;span style="color:#2b91af"&gt;ContentSerializerAttribute&lt;/span&gt;(SharedResource = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Node&lt;/span&gt; root;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Serializes a node to XML.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;param name=&amp;quot;node&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color:#008000"&gt;Node to &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; serialize.&lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;param name=&amp;quot;name&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color:#008000"&gt;Name of the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; file to serialize to.&lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Serialize(&lt;span style="color:#2b91af"&gt;Node&lt;/span&gt; node, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; name)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.root = node;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af"&gt;XmlWriterSettings&lt;/span&gt; settings = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XmlWriterSettings&lt;/span&gt;();&lt;/li&gt; &lt;li&gt;        settings.Indent = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;using&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;XmlWriter&lt;/span&gt; writer = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;XmlWriter&lt;/span&gt;.Create(name, settings))&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;IntermediateSerializer&lt;/span&gt;.&lt;/li&gt; &lt;li&gt;                Serialize(writer, &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Deserializes the XML file provided &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; and returns the created node.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;param name=&amp;quot;name&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color:#008000"&gt;Name of the xml file.&lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color:#008000"&gt;Node serialized in that file.&lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Node&lt;/span&gt; Deserialize(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; name)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af"&gt;Node&lt;/span&gt; node = &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#2b91af"&gt;XmlReaderSettings&lt;/span&gt; settings = &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XmlReaderSettings&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;using&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;XmlReader&lt;/span&gt; reader = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;XmlReader&lt;/span&gt;.Create(name, settings))&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;NodeSerializer&lt;/span&gt; serial = &lt;/li&gt; &lt;li&gt;                &lt;span style="color:#2b91af"&gt;IntermediateSerializer&lt;/span&gt;.&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                Deserialize&amp;lt;&lt;span style="color:#2b91af"&gt;NodeSerializer&lt;/span&gt;&amp;gt;(reader, &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;            node = serial.root;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; node;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The last exception comes with the Texture2D class. I wanted that the serializing of a tree included the actual textures used, as not to have to do a “second” step when loading. This has 2 problems. First, the texture is binary data, so kind of difficult to serialize in xml. Second, the texture has a reference to the ContentManager that loaded it, so it creates a circular dependency that there is no way to solve, short of dumping the use of the ContentManager all together. &lt;/p&gt; &lt;p&gt;To work around these 2 issues I created a Texture2D proxy class, as shown here:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3aeb7b7d-564d-422e-aa56-dccd02f40872" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 3em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Texture2DProxy&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Reference to the the parent class &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; that holds a ContentManager to load &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; this texture.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    [&lt;span style="color:#2b91af"&gt;ContentSerializerAttribute&lt;/span&gt;(SharedResource = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;)]&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;IContentHolder&lt;/span&gt; contentRef;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Texture we wrap.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Texture2D&lt;/span&gt; texture;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Name of the texture in the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; ContentManager.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; name;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Initializes a new instance of &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; the Texture2DProxy class.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;param name=&amp;quot;contentRef&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Content manager that will be &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; used for loading.&lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Texture2DProxy(&lt;span style="color:#2b91af"&gt;IContentHolder&lt;/span&gt; contentRef)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.contentRef = contentRef;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.name = &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.texture = &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Initializes a new instance of &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; the Texture2DProxy class.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Texture2DProxy()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.contentRef = &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.name = &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.texture = &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Gets or sets the name of the texture.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;value&amp;gt;&lt;/span&gt;&lt;span style="color:#008000"&gt;Name of the texture.&lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/value&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Name&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;set&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.name = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.name; }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Gets or sets the texture of the proxy.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;remarks&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; The Get method has lazy &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; initialization of the texture, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; in the sense that if we have the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; texture name and not the texture, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; it loads it when we request the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; texture. This is useful for when &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; we load a texture via serialization.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/remarks&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;value&amp;gt;&lt;/span&gt;&lt;span style="color:#008000"&gt;Texture that we wrap.&lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/value&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    [&lt;span style="color:#2b91af"&gt;ContentSerializerIgnore&lt;/span&gt;]&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Texture2D&lt;/span&gt; Texture&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.texture == &lt;span style="color:#0000ff"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.name != &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty)&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#2b91af"&gt;ContentManager&lt;/span&gt; man = &lt;/li&gt; &lt;li&gt;                    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.contentRef.GetContent();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.texture = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.contentRef.GetContent().&lt;/li&gt; &lt;li&gt;                    Load&amp;lt;&lt;span style="color:#2b91af"&gt;Texture2D&lt;/span&gt;&amp;gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.name);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.texture; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;set&lt;/span&gt; &lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.texture = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;; &lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Loads the selected texture.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;param name=&amp;quot;name&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Name of the texture to &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; load.&lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Load(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; name)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.name = name;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.texture = &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.contentRef.GetContent().&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            Load&amp;lt;&lt;span style="color:#2b91af"&gt;Texture2D&lt;/span&gt;&amp;gt;(name);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;    &lt;p&gt;What is serialized in the texture is the actual name of the texture, so afterwards we can load the name, and load the actual texture by request in the “get” field.&amp;nbsp; This makes for a simple method of loading the textures from xml, the only problem is a project can have many ContentManagers running, and we have no way of knowing which one is the one the texture is associated to. To get around this we create a IContentHolder interface.&lt;/p&gt; &lt;p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d7a476e2-901d-40d3-b069-0f7fdef7af2f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Class that has a content manager.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af"&gt;IContentHolder&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Returns the ContentManager &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; associated to this class.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color:#008000"&gt;A ContentManager object.&lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;ContentManager&lt;/span&gt; GetContent();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;&lt;/p&gt; &lt;p&gt;The parent class that owns the texture implements this interface, and is required to pass itself to the texture as a reference upon creation. That way the steps when deserializing are as follows:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;When the serializer creates the parent class, the parent class creates the Texture2DProxy object and assigns itself as the ContentHolder. &lt;/li&gt; &lt;li&gt;The deserializer fills the serialized data of the Texture2DProxy (the name of the texture),&lt;/li&gt; &lt;li&gt;At some point, the game requests the texture for the first time, in the “get” field we see it’s not loaded yet, so we create it using the ContentManager of our parent, and we’re ready to go.&lt;/li&gt;&lt;/ol&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-4667171154857834962?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/4667171154857834962/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-4saving.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/4667171154857834962'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/4667171154857834962'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-4saving.html' title='Baddies 4–Saving'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/-8Cl0u42J_qI/TllVNsdSYdI/AAAAAAAACDs/elLYhoiY7Gw/s72-c/Saving1_thumb.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-5855673333500233330</id><published>2011-08-24T11:17:00.001+02:00</published><updated>2011-08-24T11:17:41.994+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='xna'/><title type='text'>Dictionary of shared resources for serializing in XNA</title><content type='html'>&lt;h1&gt;Introduction&lt;/h1&gt; &lt;h2&gt;Contents&lt;/h2&gt; &lt;p&gt;This is a class built on the articles of Shawn Hargreaves for using the IntermediateSerializer class to serialize collections of shared resources. In his article he only does the List&amp;lt;T&amp;gt; collection, but I had the need to serialize a Dictionary&amp;lt;T,K&amp;gt; instead, and maybe someone else will need it at some point, so here it is.&lt;/p&gt; &lt;h2&gt;Background&lt;/h2&gt; &lt;p&gt;The class is to be used with the &lt;a href="http://blogs.msdn.com/b/shawnhar/archive/2008/07/28/intermediateserializer-vs-xmlserializer.aspx"&gt;IntermediateSerializer&lt;/a&gt;. It’s a class for serializing data to XML in XNA, and though it only works on the windows platform, it’s easier to use and has a lot more flexibility than the XmlSerializer. Introduction to the IntermediateSerializer can be found &lt;a href="http://blogs.msdn.com/b/shawnhar/archive/2008/08/12/teaching-a-man-to-fish.aspx"&gt;here&lt;/a&gt;, and a more extensive explanation can be found &lt;a href="http://blogs.msdn.com/b/shawnhar/archive/2008/08/12/everything-you-ever-wanted-to-know-about-intermediateserializer.aspx"&gt;here&lt;/a&gt;. Following that, you might want to check out &lt;a href="http://blogs.msdn.com/b/shawnhar/archive/2008/08/26/customizing-intermediateserializer-part-2.aspx"&gt;how to write your own ContentTypeSerializer&lt;/a&gt;, and specifically, how to do it for a &lt;a href="http://blogs.msdn.com/b/shawnhar/archive/2008/11/20/serializing-collections-of-shared-resources.aspx"&gt;collection of shared resources&lt;/a&gt;.&lt;/p&gt; &lt;h1&gt;Code&lt;/h1&gt; &lt;p&gt;As you have seen in the example about collections of shared resources, Shawn only deals with the List class, leaving us to work through the rest. Well, here is a Dictionary class with the values as shared resources.&lt;/p&gt; &lt;h2&gt;Dictionary evolves to… SharedResourceDictionary!&lt;/h2&gt; &lt;p&gt;The shared resource dictionary is nothing more than an empty wrapper:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:921f54fb-60ed-41cb-898a-6cea6f5385cd" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;SharedResourceDictionary&lt;/span&gt;&amp;lt;K, T&amp;gt; : &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;K, T&amp;gt;&lt;/li&gt; &lt;li&gt;{    }&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;h2&gt;SharedResourceDictionarySerializer&lt;/h2&gt; &lt;p&gt;This is where the serializing gets done. As a side note, you don’t need to do anything special to get the SharedResourceDictionary to use the SharedResourceDictionarySerializer, just by having both classes in your project it’s fine.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0e5dd561-8b58-4153-9c5b-2381856f1327" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af"&gt;ContentTypeSerializer&lt;/span&gt;]&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;SharedResourceDictionarySerializer&lt;/span&gt;&amp;lt;T, K&amp;gt; : &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;ContentTypeSerializer&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &amp;lt;&lt;span style="color:#2b91af"&gt;SharedResourceDictionary&lt;/span&gt;&amp;lt;T, K&amp;gt;&amp;gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ContentSerializerAttribute&lt;/span&gt; itemFormat = &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ContentSerializerAttribute&lt;/span&gt;()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        ElementName = &lt;span style="color:#a31515"&gt;&amp;quot;Item&amp;quot;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    };&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ContentSerializerAttribute&lt;/span&gt; keyFormat = &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ContentSerializerAttribute&lt;/span&gt;()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        ElementName = &lt;span style="color:#a31515"&gt;&amp;quot;Key&amp;quot;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    };&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ContentSerializerAttribute&lt;/span&gt; valueFormat = &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ContentSerializerAttribute&lt;/span&gt;()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        ElementName = &lt;span style="color:#a31515"&gt;&amp;quot;Value&amp;quot;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    };&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Serialize(&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af"&gt;IntermediateWriter&lt;/span&gt; output,&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#2b91af"&gt;SharedResourceDictionary&lt;/span&gt;&amp;lt;T, K&amp;gt; value,&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af"&gt;ContentSerializerAttribute&lt;/span&gt; format)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;T, K&amp;gt; item &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; value)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            output.Xml.WriteStartElement(&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                itemFormat.ElementName);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            output.WriteObject(&lt;/li&gt; &lt;li&gt;                item.Key, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                keyFormat);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            output.WriteSharedResource(&lt;/li&gt; &lt;li&gt;                item.Value, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                valueFormat);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            output.Xml.WriteEndElement();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;override&lt;/span&gt; &lt;span style="color:#2b91af"&gt;SharedResourceDictionary&lt;/span&gt;&amp;lt;T, K&amp;gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Deserialize(&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af"&gt;IntermediateReader&lt;/span&gt; input,&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#2b91af"&gt;ContentSerializerAttribute&lt;/span&gt; format,&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af"&gt;SharedResourceDictionary&lt;/span&gt;&amp;lt;T, K&amp;gt; existingInstance)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (existingInstance == &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            existingInstance = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;SharedResourceDictionary&lt;/span&gt;&amp;lt;T, K&amp;gt;();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;while&lt;/span&gt; (input.MoveToElement(itemFormat.ElementName))&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            T key;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            input.Xml.ReadToDescendant(&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                keyFormat.ElementName);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            key = input.ReadObject&amp;lt;T&amp;gt;(keyFormat);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            input.Xml.ReadToNextSibling(&lt;/li&gt; &lt;li&gt;                valueFormat.ElementName);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            input.ReadSharedResource&amp;lt;K&amp;gt;(&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                valueFormat, &lt;/li&gt; &lt;li&gt;                (K value) =&amp;gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                existingInstance.Add(key, value);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            });&lt;/li&gt; &lt;li&gt;            input.Xml.ReadEndElement();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; existingInstance;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&lt;font size="1"&gt;Sorry about the “strung out” format of the code, otherwise the blog settings compact it into an unreadable mess.&lt;/font&gt; &lt;/p&gt; &lt;h2&gt;Calling the serializer&lt;/h2&gt; &lt;p&gt;To use this code, we have a dictionary we want to serialize:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c53928fd-c05a-42fd-9bf9-c5bad3ec8e72" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af"&gt;SharedResourceDictionary&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;int&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;&amp;gt; dictTest = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;SharedResourceDictionary&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;int&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;To serialize it to Xml we do this:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:44e2fb21-ae73-424d-9d1c-87d0bbf4ba10" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af"&gt;XmlWriterSettings&lt;/span&gt; settings = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XmlWriterSettings&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;settings.Indent = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;XmlWriter&lt;/span&gt; writer = &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;XmlWriter&lt;/span&gt;.Create(&lt;span style="color:#a31515"&gt;&amp;quot;out.xml&amp;quot;&lt;/span&gt;, settings))&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;IntermediateSerializer&lt;/span&gt;.Serialize(&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        writer, &lt;/li&gt; &lt;li&gt;        dictTest, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;To deserialize from Xml we do this:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f3091049-0a82-4a36-b580-3509f5bc2266" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af"&gt;XmlReaderSettings&lt;/span&gt; settings = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XmlReaderSettings&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;XmlReader&lt;/span&gt; reader = &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;XmlReader&lt;/span&gt;.Create(&lt;span style="color:#a31515"&gt;&amp;quot;out.xml&amp;quot;&lt;/span&gt;, settings))&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.dictTest = &lt;span style="color:#2b91af"&gt;IntermediateSerializer&lt;/span&gt;.&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Deserialize&amp;lt;&lt;span style="color:#2b91af"&gt;SharedResourceDictionary&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;int&lt;/span&gt;,&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;&amp;gt;&amp;gt;&lt;/li&gt; &lt;li&gt;        (reader, &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;h1&gt;&amp;nbsp;&lt;/h1&gt; &lt;h1&gt;Notes&lt;/h1&gt; &lt;p&gt;Special thanks to &lt;a href="http://stackoverflow.com/questions/7165496/error-using-xmlreader-in-c-and-xna-4-0"&gt;r2d2rigo&lt;/a&gt; for helping me hunt down some bugs in the code.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-5855673333500233330?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/5855673333500233330/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/dictionary-of-shared-resources-for.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/5855673333500233330'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/5855673333500233330'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/dictionary-of-shared-resources-for.html' title='Dictionary of shared resources for serializing in XNA'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-2526402518483280450</id><published>2011-08-19T19:30:00.002+02:00</published><updated>2011-08-19T22:59:11.142+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='xna'/><title type='text'>XNA Serialization to XML tutorial</title><content type='html'>&lt;h1&gt;Introduction&lt;/h1&gt; &lt;h2&gt;What is serialization?&lt;/h2&gt; &lt;p&gt;Serialization is a semi-automatic process where XNA gets a class &lt;del&gt;that has been marked as [Serializable] and&lt;/del&gt; stores/loads it from xml. An awesome idea in theory but with a few shortcomings. &lt;/p&gt; &lt;h2&gt;Why these tutorials?&lt;/h2&gt; &lt;p&gt;While trying to add serialization to one of my games, I’ve come to notice that there is a very real lack of documentation on XML serialization if you want to do anything more complex than serializing a struct with a few value members inside. When you get to serializing derived classes, dictionaries, trees,… and in general the normal stuff any game needs, there are a lot of hidden pitfalls to be aware of.&lt;br /&gt;It’s a big list of things to go work, but I’m going to share my experiments with serialization and hopefully it will help someone else.&lt;/p&gt; &lt;h2&gt;Windows only&lt;/h2&gt; &lt;p&gt;This tutorial is aimed only at the windows platform, simply because it’s the only one I’ve got. I promise that if anyone likes them enough to send me a WP7 or an Xbox, I’ll make sure they are compatible. :D&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Note*: Check the comments to see the small change to make it work in WP7 and XBox&lt;/p&gt; &lt;h1&gt;Serialization example&lt;/h1&gt; &lt;h2&gt;A look at MSDN serialization&lt;/h2&gt; &lt;p&gt;This tutorial is based on the simplest of all options, the default MSDN tutorial for serializing xml, to be found at &lt;a href="http://msdn.microsoft.com/en-us/library/bb203924.aspx"&gt;MSDN: Serialization&lt;/a&gt;. The version provided in the download of that article is aimed for the Xbox, which makes it kind of confusing on the windows platform as it uses classes that are not needed, such as the Guide class. If you are working on the Xbox and have doubts, have a look in the book: "XNA Game Studio 4.0 Programming".&lt;/p&gt; &lt;p&gt;&lt;br /&gt;My main objection to the example at &lt;a href="http://msdn.microsoft.com/en-us/library/bb203924.aspx"&gt;MSDN: Serialization&lt;/a&gt; is that it does NOT tell you where it gets the "device" object, and if you download the code to find out, it uses the "Guide" to get the device in a rather confusing way. &lt;/p&gt; &lt;p&gt;&lt;br /&gt;Searching around on how to make it work I found another 3 ways to get the storage device here &lt;a href="http://msdn.microsoft.com/en-us/library/bb203920(v=xnagamestudio.10).aspx"&gt;MSDN : Get StorageDevice&lt;/a&gt;. Note that even though they are ALMOST the same as in the &lt;a href="http://msdn.microsoft.com/en-us/library/bb203924.aspx"&gt;MSDN: Serialization&lt;/a&gt; example code, the function call varies, not much, but enough to drive one crazy. God bless IntelliSense. If you’ve got more interest, there is a theoretical explanation on how to get a storage device at &lt;a href="http://msdn.microsoft.com/en-us/library/bb200105.aspx#ID4ETD"&gt;MSDN : User Storage&lt;/a&gt;.&lt;/p&gt; &lt;h2&gt;Steps&lt;/h2&gt; &lt;p&gt;For both saving and loading the steps are almost the same. Notice most of the work goes into getting the actual file we are going to save to. The function calls to achieve this vary slightly from platform to platform, but we’re going to stick with windows, as the code download at the &lt;a href="http://msdn.microsoft.com/en-us/library/bb203924.aspx"&gt;MSDN: Serialization&lt;/a&gt; tutorial has a Xbox version.&lt;/p&gt; &lt;ol&gt; &lt;li&gt;User requests to Save / Load&lt;/li&gt; &lt;li&gt;Get the StorageDevice.&lt;/li&gt; &lt;ol&gt; &lt;li&gt;Request StorageDevice&lt;/li&gt; &lt;li&gt;Receive StorageDevice&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Get a StorageContainer from the device.&lt;/li&gt; &lt;ol&gt; &lt;li&gt;Request StorageContainer&lt;/li&gt; &lt;li&gt;Receive StorageContainer&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;Get the Stream of the file we want to use from the container.&lt;/li&gt; &lt;li&gt;Serialize / Deserialize.&lt;/li&gt; &lt;li&gt;Cleanup. &lt;/li&gt;&lt;/ol&gt; &lt;h2&gt;Code&lt;/h2&gt; &lt;p&gt;With that out of the way, let’s head over to the code!&lt;br /&gt;We will be using a very simple data class to serialize in this first tutorial.&lt;/p&gt; &lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:075128f0-00bf-4d01-b487-210fd04870ba" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Data class with some basic data &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; for testing.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;[&lt;span style="color:#2b91af"&gt;Serializable&lt;/span&gt;]&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Data1&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; age;&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; name = &lt;span style="color:#a31515"&gt;"Chuel"&lt;/span&gt;;&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Initializes a new instance of &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; the Data1 class. &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Required default constructor for &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; serialization.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Data1()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.age = 0;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Initializes a new instance of &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; the Data1 class. &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;param name="age"&amp;gt;&lt;/span&gt;&lt;span style="color:#008000"&gt;Age of the chuel.&lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Data1(&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; age) &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.age = age;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Draw(&lt;span style="color:#2b91af"&gt;SpriteBatch&lt;/span&gt; spriteBatch, &lt;span style="color:#2b91af"&gt;SpriteFont&lt;/span&gt; font)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; text = &lt;span style="color:#a31515"&gt;"Name: "&lt;/span&gt; + &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.name + &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#a31515"&gt;" Age: "&lt;/span&gt; + &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.age;&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        spriteBatch.DrawString(&lt;/li&gt; &lt;li&gt;            font, text, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Vector2&lt;/span&gt;(10, 10), &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af"&gt;Color&lt;/span&gt;.White);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;&lt;p&gt;&lt;/p&gt; &lt;p&gt;&lt;br /&gt;&lt;del&gt;First thing to notice here is that you need to &lt;strong&gt;mark with the [Serializable]&lt;/strong&gt; attribute the class you want to serialize&lt;/del&gt; (Not necessary for serializing to XML, check r2d2rigo's comment), there are more options to explore with related attributes and you can find it &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute(v=vs.95).aspx"&gt;here&lt;/a&gt;. Second, that it’s a &lt;strong&gt;public class&lt;/strong&gt;, as the object you serialize must be public. Third, &lt;strong&gt;only public properties or fields&lt;/strong&gt; will be serialized (age and name).&lt;/p&gt; &lt;p&gt;&lt;br /&gt;And here is how we load / save the data.&lt;/p&gt; &lt;p&gt; &lt;/p&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a14e37b3-c6d1-4570-b4a0-09d8f7e5796b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 3em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; This is a game component that &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; implements IUpdateable.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; It serializes data in a simple way, &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; no preprocesing or special content.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; No blocking when doing the async. calls.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Mode2&lt;/span&gt; : &lt;/li&gt; &lt;li&gt;    Microsoft.Xna.Framework.&lt;span style="color:#2b91af"&gt;DrawableGameComponent&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Data1&lt;/span&gt; data1;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;SpriteBatch&lt;/span&gt; spriteBatch;&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Keyboard polling helpers&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;KeyboardState&lt;/span&gt; currentState;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;KeyboardState&lt;/span&gt; previousState;&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Action to do&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;enum&lt;/span&gt; &lt;span style="color:#2b91af"&gt;State&lt;/span&gt; { &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Innactive, RequestDevice, &lt;/li&gt; &lt;li&gt;        GetDevice, GetContainer, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        SaveLoad };&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;State&lt;/span&gt; state;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;enum&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt; { None, Save, Load };&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt; action;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Needed variables&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#2b91af"&gt;IAsyncResult&lt;/span&gt; result = &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;StorageDevice&lt;/span&gt; device = &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#2b91af"&gt;StorageContainer&lt;/span&gt; container = &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;;&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Mode2(&lt;span style="color:#2b91af"&gt;Game&lt;/span&gt; game)&lt;/li&gt; &lt;li&gt;        : &lt;span style="color:#0000ff"&gt;base&lt;/span&gt;(game)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        state = &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.Innactive;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        action = &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;.None;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Initialize()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;base&lt;/span&gt;.Initialize();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.spriteBatch = &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;SpriteBatch&lt;/span&gt;(Game.GraphicsDevice);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.data1 = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Data1&lt;/span&gt;();&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Update(&lt;span style="color:#2b91af"&gt;GameTime&lt;/span&gt; gameTime)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#008000"&gt;// Key checking&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        previousState = currentState;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        currentState = &lt;span style="color:#2b91af"&gt;Keyboard&lt;/span&gt;.GetState();&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#008000"&gt;// STEP 1: User requests save / load&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000"&gt;// When we press the "s" key&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (currentState.IsKeyUp(&lt;span style="color:#2b91af"&gt;Keys&lt;/span&gt;.S) &amp;amp;&amp;amp;&lt;/li&gt; &lt;li&gt;            previousState.IsKeyDown(&lt;span style="color:#2b91af"&gt;Keys&lt;/span&gt;.S))&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#008000"&gt;// Request to save&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (state == &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.Innactive &amp;amp;&amp;amp; &lt;/li&gt; &lt;li&gt;                action == &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;.None)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state = &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.RequestDevice;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.action = &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;.Save;                    &lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#008000"&gt;// When we press the "l" key&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (currentState.IsKeyUp(&lt;span style="color:#2b91af"&gt;Keys&lt;/span&gt;.L) &amp;amp;&amp;amp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            previousState.IsKeyDown(&lt;span style="color:#2b91af"&gt;Keys&lt;/span&gt;.L))&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// Request to save&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (state == &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.Innactive &amp;amp;&amp;amp; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                action == &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;.None)&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state = &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.RequestDevice;&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.action = &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;.Load; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000"&gt;// Do the actual save or load&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.action == &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;.Save &amp;amp;&amp;amp; &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state != &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.Innactive)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Save();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.action == &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;.Load &amp;amp;&amp;amp; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state != &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.Innactive)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Load();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000"&gt;// Make the age change so we can see &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#008000"&gt;// stuff change with the save / load&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;Mouse&lt;/span&gt;.GetState().LeftButton == &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;ButtonState&lt;/span&gt;.Pressed)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.data1.age += 1;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;base&lt;/span&gt;.Update(gameTime);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Function where we do all the saving&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Save()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;switch&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.RequestDevice:&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#008000"&gt;// STEP 2.a: Request the device&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            result = &lt;/li&gt; &lt;li&gt;                &lt;span style="color:#2b91af"&gt;StorageDevice&lt;/span&gt;.BeginShowSelector(&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#2b91af"&gt;PlayerIndex&lt;/span&gt;.One, &lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state = &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.GetDevice;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.GetDevice:&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// If the device is ready&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (result.IsCompleted)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#008000"&gt;// STEP 2.b: Recieve the device&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                device = &lt;/li&gt; &lt;li&gt;                &lt;span style="color:#2b91af"&gt;StorageDevice&lt;/span&gt;.EndShowSelector(result);                            &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;                &lt;span style="color:#008000"&gt;// STEP 3.a: Request the container&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                result.AsyncWaitHandle.Close();&lt;/li&gt; &lt;li&gt;                result = &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                result = device.BeginOpenContainer(&lt;/li&gt; &lt;li&gt;                    &lt;span style="color:#a31515"&gt;"StorageDemo"&lt;/span&gt;, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;, &lt;/li&gt; &lt;li&gt;                    &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state = &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.GetContainer;&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.GetContainer:&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// If the container is ready&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (result.IsCompleted)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#008000"&gt;// STEP 3.b: Recieve the container&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                container = device.EndOpenContainer(result);&lt;/li&gt; &lt;li&gt;                result.AsyncWaitHandle.Close();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state = &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.SaveLoad;&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.SaveLoad:&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; filename = &lt;span style="color:#a31515"&gt;"savegame.sav"&lt;/span&gt;;&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// Check to see whether &lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#008000"&gt;// the file exists.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (container.FileExists(filename))&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#008000"&gt;// Delete it so that we &lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#008000"&gt;// can create one fresh.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                container.DeleteFile(filename);&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#008000"&gt;// Create the file.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;Stream&lt;/span&gt; stream = &lt;/li&gt; &lt;li&gt;                container.CreateFile(filename);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#008000"&gt;// Convert the object to XML data &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// and put it in the stream.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af"&gt;XmlSerializer&lt;/span&gt; serializer = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XmlSerializer&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;Data1&lt;/span&gt;));&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            serializer.Serialize(stream, data1);&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// Close the file.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            stream.Close();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#008000"&gt;// Dispose the container, to &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// commit changes.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            container.Dispose();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            state = &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.Innactive;&lt;/li&gt; &lt;li&gt;            action = &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;.None;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Function where we do all the loading&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Load()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;switch&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.RequestDevice:&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// STEP 2.a: Request the device&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            result = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#2b91af"&gt;StorageDevice&lt;/span&gt;.BeginShowSelector(&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#2b91af"&gt;PlayerIndex&lt;/span&gt;.One, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;, &lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state = &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.GetDevice;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.GetDevice:&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#008000"&gt;// If the device is ready&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (result.IsCompleted)&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#008000"&gt;// STEP 2.b: Recieve the device&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                device = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#2b91af"&gt;StorageDevice&lt;/span&gt;.EndShowSelector(result);&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#008000"&gt;// STEP 3.a: Request the container&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                result.AsyncWaitHandle.Close();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                result = &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;                result = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    device.BeginOpenContainer(&lt;/li&gt; &lt;li&gt;                    &lt;span style="color:#a31515"&gt;"StorageDemo"&lt;/span&gt;, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;, &lt;/li&gt; &lt;li&gt;                    &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state = &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.GetContainer;&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.GetContainer:&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// If the container is ready&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (result.IsCompleted)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#008000"&gt;// STEP 3.b: Recieve the container&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                container = &lt;/li&gt; &lt;li&gt;                    device.EndOpenContainer(result);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                result.AsyncWaitHandle.Close();&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.state = &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.SaveLoad;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;case&lt;/span&gt; &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.SaveLoad:&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; filename = &lt;span style="color:#a31515"&gt;"savegame.sav"&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#008000"&gt;// Check to see whether the save exists.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (!container.FileExists(filename))&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#008000"&gt;// If not, dispose of the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#008000"&gt;// container and return.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                container.Dispose();&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;return&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// Create the file.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af"&gt;Stream&lt;/span&gt; stream = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                container.OpenFile(&lt;/li&gt; &lt;li&gt;                filename, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#2b91af"&gt;FileMode&lt;/span&gt;.Open);&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// Convert the object to XML &lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#008000"&gt;// data and put it in the stream.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;XmlSerializer&lt;/span&gt; serializer = &lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XmlSerializer&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;Data1&lt;/span&gt;));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;            data1 = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                (&lt;span style="color:#2b91af"&gt;Data1&lt;/span&gt;)serializer.Deserialize(stream);&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;// Close the file.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            stream.Close();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#008000"&gt;// Dispose the container, to commit changes.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            container.Dispose();&lt;/li&gt; &lt;li&gt;            state = &lt;span style="color:#2b91af"&gt;State&lt;/span&gt;.Innactive;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            action = &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;.None;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }        &lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Draw(&lt;span style="color:#2b91af"&gt;GameTime&lt;/span&gt; gameTime)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;base&lt;/span&gt;.Draw(gameTime);&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.spriteBatch.Begin();&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.data1.Draw(&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.spriteBatch, &lt;/li&gt; &lt;li&gt;            Game.Content.Load&amp;lt;&lt;span style="color:#2b91af"&gt;SpriteFont&lt;/span&gt;&amp;gt;(&lt;span style="color:#a31515"&gt;"Font"&lt;/span&gt;));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.spriteBatch.End();&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;   &lt;p&gt;We need to get 2 objects with asynchronous calls, the device and the container. This means we need to be &lt;strong&gt;careful not to block the game&lt;/strong&gt; while we wait for the calls to return, and that’s why the code looks like a magical rainbow of cases, because the Update() function might be called lots of times while we wait and we don’t want strange stuff to happen.&lt;/p&gt; &lt;p&gt;&lt;br /&gt;We create a data object with just a name and an age, when we press the left button of the mouse the age increments, press “s” to save and “l” to load the last saved data.&lt;/p&gt; &lt;p&gt;&lt;br /&gt;Following the comments in the code should give a pretty clear idea of what is going on, but we’ll go over it none the less, I’ll ignore the support code not directly related with serialization, but if there are any doubts just ask in the comments. There is some redundant code but I haven’t shortened to make it as simple as possible to follow the logic. &lt;/p&gt; &lt;p&gt;&lt;br /&gt;Both save and load work very similar. First thing we need to do is get a storage device. This requires using an asynchronous call, so we request it (2.a) by calling BeginShowSelector and when it’s ready result.IsCompleted will return true. Notice that in windows we pass PlayerIndex.One as the parameter.&lt;br /&gt;&lt;/p&gt; &lt;p&gt;We then receive the device (2.b) and request the container from the device (3.a) with BeginContainer, where we pass the name of the “container” to use, in this case, the folder StorageDemo.&lt;/p&gt; &lt;p&gt;&lt;br /&gt;We finally we receive the container (3.b) and we’re ready to go! No more magical asynchronous stuff.&lt;/p&gt; &lt;p&gt;&lt;br /&gt;The last part is where the actual serializing takes place. For saving we get ourselves a file to write on (4) and a serializer for the type of data we want to serialize (Data1 in this case), and then just serialize it into the file (5) and tidy up (6). For loading we check the file exists (4) and then open it and deserialize into our data object (5) and clean up (6).&lt;/p&gt; &lt;p&gt;The generated XML looks like this:&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0f2aa89d-311f-41c0-a488-1e7f41adb0ed" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;1.0&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;?&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Data1&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;xmlns:xsi&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;http://www.w3.org/2001/XMLSchema-instance&lt;/span&gt;"&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;xmlns:xsd&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;http://www.w3.org/2001/XMLSchema&lt;/span&gt;"&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;age&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;43&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;age&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Chuel&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Data1&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&lt;br /&gt;A last note on how the system is set up. The Mode2 class is a DrawableGameComponent so that the XNA framework takes care of everything (updating and drawing and so on), and in our game class we only need to add one line. Has nothing to do with serializing. &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6a6665b5-f77f-47b0-b96d-ccea0f3b9e7a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol start="1" style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Game1()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            graphics = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;GraphicsDeviceManager&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            Content.RootDirectory = &lt;span style="color:#a31515"&gt;"Content"&lt;/span&gt;;&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Components.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Mode2&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;h1&gt; &lt;/h1&gt; &lt;h1&gt;Notes&lt;/h1&gt; &lt;h2&gt;Data that XNA serializes&lt;/h2&gt; &lt;p&gt;XNA doesn’t serialize everything that there is in the class, but the documentation is not too specific on what’s game besides “public properties and fields”. So I went ahead a did a little testing. Here are the results of testing, plus what I found searching around the net.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Serializes public &lt;strong&gt;properties&lt;/strong&gt;, not private or protected.&lt;/li&gt; &lt;li&gt;Serializes public &lt;strong&gt;fields&lt;/strong&gt; with both get and set, not partially implemented fields.&lt;/li&gt; &lt;li&gt;&lt;strong&gt;Arrays&lt;/strong&gt; of values or of objects work fine.&lt;/li&gt; &lt;li&gt;&lt;strong&gt;Lists&amp;lt;&amp;gt;&lt;/strong&gt; of values or of objects work fine. Tough not generic List&amp;lt;T&amp;gt;&lt;/li&gt; &lt;li&gt;Cannot serialize ArrayList.&lt;/li&gt; &lt;li&gt;Cannot serialize an enumeration of unsigned long with any value larger than  9,223,372,036,854,775,807.&lt;/li&gt; &lt;li&gt;Typical XNA classes like &lt;strong&gt;Rectangle&lt;/strong&gt; or &lt;strong&gt;Vector&lt;/strong&gt; work just fine with the serialization.&lt;/li&gt; &lt;li&gt;On serializing &lt;strong&gt;references&lt;/strong&gt;, I found &lt;a href="http://forums.create.msdn.com/forums/t/47544.aspx"&gt;this bit from the MSDN forum&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;“It will serialize references, but always as a copy. Basically what this means is that each time the serializer hits an object that has a reference to another serializable object exposed as a public property or field, it will write out the all of the public properties/fields of the object that reference points to. This is a problem if you have the same object referenced multiple times by something you're serializing. For instance...say you had two MeleeAttack's referencing the same PhysicsObject. After deserialization, you'd end up with your MeleeAttack's having references to different PhysicsObject's with identical properties. I used to get around this by giving certain types of objects a unique integer ID, and then having the objects serialize the ID rather than the reference. Then after deserialization I'd have another initialization step where I'd use the ID to look up the corresponding object and grab a reference.”&lt;/li&gt; &lt;li&gt;From textures it only saves the name.&lt;/li&gt;&lt;/ul&gt; &lt;h2&gt;Leads for more complex serialization magic&lt;/h2&gt; &lt;p&gt;For elements that we read from Xml that are not in the object, we can use the XmlAnyElementAttribute and XmlAnyAttributeAttribute attributes. Useful for compatibility with older versions of our code. Check it &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlanyelementattribute(v=vs.95).aspx"&gt;here&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;To ignore a field we can use the XmlIgnoreAttribute. Check it &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlignoreattribute(v=vs.95).aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;You can override the serialization of any set of objects by creating one of the appropriate attributes and adding it to an instance of the XmlAttributes class. More complex stuff that can be used to serialize things that come from dlls, or to control how to serialize your own data. Check it &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributeoverrides(v=vs.95).aspx"&gt;here&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;You can use the IXmlSerializable interface to provide custom formatting for Xml serialization and deserialization. Check it &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable(v=vs.95).aspx"&gt;here&lt;/a&gt;.&lt;/p&gt; &lt;h2&gt;4 Quick rules&lt;/h2&gt; &lt;p&gt;To end, 4 quick reference rules to keep in mind for serializing:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;del&gt;Note #1: You need to mark the class you want to serialize with the [Serializable] attribute.&lt;/del&gt;&lt;/li&gt; &lt;li&gt;Note #2: Serialized classes must have a default constructor. &lt;/li&gt; &lt;li&gt;Note #3: Serialization only converts the public properties and fields&lt;/li&gt; &lt;li&gt;Note #3: The actual object you serialize must be public.&lt;/li&gt;&lt;/ul&gt; &lt;h1&gt;Conclusion&lt;/h1&gt; &lt;p&gt;The serialization mechanism is awesome as in it’s automatic for the classes marked, but it’s lack of handling references correctly makes it useless for saving a big “chunk” of game data (like a whole level) as it doesn’t avoid the work of having to set up a ID system to take care of the references. It is best suited for things as saving the player preferences, or the control settings or similar.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-2526402518483280450?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/2526402518483280450/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-serialization-to-xml-tutorial.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2526402518483280450'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2526402518483280450'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-serialization-to-xml-tutorial.html' title='XNA Serialization to XML tutorial'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-5154189362228171712</id><published>2011-08-16T10:34:00.001+02:00</published><updated>2011-08-16T10:34:40.511+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='notes'/><category scheme='http://www.blogger.com/atom/ns#' term='xna'/><title type='text'>XNA Notes–Day 6</title><content type='html'>&lt;p&gt;There is an onscreen keyboard that can be shown with Guide.BeginShowKeyboardInputwhich is an asyncronous object and you need to wait till the isCompleted parameter returns true. On Windows and XBox you need to add the GamerServicesComponent to the list of components the game has. &lt;/p&gt; &lt;p&gt;The Xbox can have a chat pad keyboard attached to the controller. &lt;/p&gt; &lt;p&gt;2 modes for audio: Fire and Forget (just call play on the audio and that's it, it plays until it finishes) and using SoundEffectInstance which lets you pause, resume, and things like that.&lt;/p&gt; &lt;p&gt;There is a Miscrosft Cross-Platform Audio Creations Tool to manage all audio, add effects and so on.&lt;/p&gt; &lt;p&gt;You can read microfone data from xna.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-5154189362228171712?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/5154189362228171712/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-6.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/5154189362228171712'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/5154189362228171712'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-6.html' title='XNA Notes–Day 6'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-3911600112422233729</id><published>2011-08-15T11:43:00.001+02:00</published><updated>2011-08-15T11:43:55.354+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='notes'/><category scheme='http://www.blogger.com/atom/ns#' term='xna'/><title type='text'>XNA Notes–Day 5</title><content type='html'>&lt;p&gt;&lt;br&gt;In short, the Importer reads from text into objects, and the Processor loads stuff (like the actual textures from the texture name) using other Processors (like the default processor for a texture) and applies effects.&lt;/p&gt; &lt;h2&gt;Content Processor&lt;/h2&gt; &lt;p&gt;To create a custom content processor you add a new Content Pipeline Extension Library and add a reference to it from your project.&lt;/p&gt; &lt;p&gt;Create a new class in this project and derive it from ContentProcessor, setting the template variable to whatever type of file we want to load and what type of file we're going to generate.&lt;/p&gt; &lt;p&gt;public MyContentProcessor : ContentProcessor&amp;lt;InputType, OutputType&amp;gt; {}&lt;/p&gt; &lt;p&gt;Then we override the Process function (that looks something like this)&lt;/p&gt; &lt;p&gt;public OutputType Process(InputType input, ContentProcessorContext context)&lt;/p&gt; &lt;p&gt;Finally, in the Content project, select the element to process and change the processor to the new one you made.&lt;/p&gt; &lt;p&gt;You can add properties to the content processor you create, and they appear on the visual studio properties when you select the asset in the content project.&lt;/p&gt; &lt;h2&gt;Content Importer&lt;/h2&gt; &lt;p&gt;To load new formats (game level).&lt;br&gt;You crterate a Content Pipeline Extension Project and create a class that derives from ContentImporter&amp;lt;ToImport.Extension&amp;gt; and override the Import function.&lt;/p&gt; &lt;h2&gt;Content Writer&lt;/h2&gt; &lt;p&gt;You inherit from the ContetTypeWriter&amp;lt;ClassToWrite&amp;gt; class,&lt;br&gt;and in that class you override the Write function and write out all the data you need to.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-3911600112422233729?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/3911600112422233729/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-5.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/3911600112422233729'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/3911600112422233729'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-5.html' title='XNA Notes–Day 5'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-4973572598789567897</id><published>2011-08-13T19:07:00.001+02:00</published><updated>2011-08-13T19:07:16.609+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='notes'/><category scheme='http://www.blogger.com/atom/ns#' term='xna'/><title type='text'>XNA Notes–Day 4</title><content type='html'>&lt;h2&gt;Effects&lt;/h2&gt; &lt;p&gt;The effects are what makes the 3D world of xna roll.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Basic effect is the default one and keeps track of stuff like lighting (ambient, directional, specular, emissive), texturing, colors and fog. It also has interfaces to be derived from by the user (one for matrix positioning, one for light and one for fog).&lt;/li&gt; &lt;li&gt;Alpha test effect is used for performance by rejecting pixels based off their alpha level. &lt;/li&gt; &lt;li&gt;Enviroment map effect is used for maping the enviroment into an object (think mirror).&lt;/li&gt; &lt;li&gt;Skinned Effect is ussed for animation of models, it relies heavily on a proper content processor.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;br&gt;&lt;/p&gt; &lt;h2&gt;States of the graphics device&lt;/h2&gt; &lt;p&gt;A state can be passed to the Begin method of the spritebatch when selecting to draw something. &lt;/p&gt; &lt;p&gt;Blend state controls how colors blend together, can be used in various ways, among others to get the negative of the image, to control the alpha operations and play with the depth stencil.&lt;/p&gt; &lt;h2&gt;Render targets&lt;/h2&gt; &lt;p&gt;There is a RenderTarget2D class that allows you to render to it instead of the device, which you can use it by calling GraphicsDevice.SetRenderTarget(rt), and in HiDef mode you can have up to 4 different render targets going. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-4973572598789567897?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/4973572598789567897/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-4.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/4973572598789567897'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/4973572598789567897'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-4.html' title='XNA Notes–Day 4'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-5978252453401834292</id><published>2011-08-11T18:30:00.001+02:00</published><updated>2011-08-11T18:30:27.589+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies 3–Editor</title><content type='html'>&lt;h2&gt;Showing the game in a PictureBox&lt;/h2&gt; &lt;p&gt;The first hurdle when making the game editor with windows forms was getting it to render XNA in a form. For this I used a picture box and followed the tutorial at &lt;a href="http://royalexander.wordpress.com/2008/10/09/xna-30-and-winforms-the-easy-way/"&gt;http://royalexander.wordpress.com/2008/10/09/xna-30-and-winforms-the-easy-way/&lt;/a&gt; The only change I had to do was to make sure the back buffer size of the xna game was the same as the size of the picture box, otherwise weird stretching ensued.&lt;/p&gt; &lt;p&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt; &lt;h2&gt;Coupling game and form&lt;/h2&gt; &lt;p&gt;There is a high coupling between both game and form in the editor, as this is the fastest and cleanest method of working the interaction between both of them. None of the two classes are intended for any reuse or generic use, so it’s the best decision. For this, we just store a reference to each other during the initialization of the program so they are linked, and provide methods to access whatever is needed.&lt;/p&gt; &lt;p&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt; &lt;h2&gt;Creating a map&lt;/h2&gt; &lt;p&gt;Creating a new map is an essential part of the editor. I created a new form that took care of receiving all the necessary information and then launched an event with this data.&lt;br&gt;This event was caught by the main editor form and then passed on to the game class, where we just create a MapGrid node and add it to the root.&lt;/p&gt; &lt;p&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt; &lt;h2&gt;Loading a TileSet&lt;/h2&gt; &lt;p&gt;To load a new tile set we create a dialog that uses the windows interface to load the required image file with the tileset. If the image is found, we create one TilePictureBox out of each bit of the image that is of tileSize x tileSize size and set it in the appropriate form container, so when the user clicks on one he can use it to draw.&lt;br&gt;Making a specific class instead of just using a normal picturebox was necessary to make the tiles react to the OnClick event. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-5978252453401834292?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/5978252453401834292/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-3editor.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/5978252453401834292'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/5978252453401834292'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-3editor.html' title='Baddies 3–Editor'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-6677920956972891795</id><published>2011-08-10T16:55:00.001+02:00</published><updated>2011-08-10T16:55:07.731+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies 2.8–Map</title><content type='html'>&lt;p&gt;The map is just a list of lists, holding all the tiles in a matrix. The only thing it has special is the fact that it holds the tileset image, with all the images of the tiles, as well as other extra graphics used for other drawing modes (for example the grid). When the map wants to draw the tiles (be it as the actual tiles, or the grid, or the collision) it passes the corresponding graphic to the tiles in their draw function.&lt;/p&gt; &lt;p&gt;&lt;br&gt;The map tiles are not nodes because of the need to access them easily with a 2D coordinate system, and the special operations that we need to do with them. It’s one of the cases where efficiency is more important than following the tree structure. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-6677920956972891795?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/6677920956972891795/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-28map.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/6677920956972891795'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/6677920956972891795'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-28map.html' title='Baddies 2.8–Map'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-6995087820663177121</id><published>2011-08-10T16:36:00.001+02:00</published><updated>2011-08-10T16:36:20.715+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies 2.7–GUI Input Management</title><content type='html'>&lt;p&gt;The game engine needs to accept input from several devices, mainly Xbox, Windows and WP7. For each of them a different input controller must be used. &lt;br&gt;The input device is an independent class, it has no power to actually do anything or change anything. Instead what happens is it interprets the input of the user using various means (selection boxes, touches, radial menus, etc.) depending on the device. Once it knows what the action is (select units in rectangle, interact with clicked position, etc.) it sends a message with all the relevant data, to be caught and acted upon by whoever is responsible.&lt;/p&gt; &lt;p&gt;&lt;br&gt;Having a system like this prevents having lots of cases for the different controllers distributed all through the code, and also avoids duplication of work.&lt;/p&gt; &lt;p&gt;&lt;br&gt;The basic idea is that the primary input of the user gets transformed into some kind of command with data that then the engine then interprets homogenously, without knowing who sends the message. Adding new input devices is therefore trivial, as the engine just receives messages of commands to execute.&lt;/p&gt; &lt;p&gt;&lt;br&gt;As for the GUI elements themselves, they hang from the input device they are designed for (no sense having a selection box in the Xbox for example), and the input device object is always on the top of any other nodes so it gets the input / drawn first. The input device has a GUINode that is active at the moment. That’s the one which attempts to answer to the user event (say a keypress), if it consumes the event, that’s it.&amp;nbsp; If not, we send a message to the system, to see if any other node wants anything to do with that event.&lt;/p&gt; &lt;p&gt;&lt;br&gt; Having the GUINodes independent of the InputDevice class makes it so if we want a standard GUI element (say a minimap), we just add the MiniMapGUINode to the InputDeviceNode, and we know it’s going to be on top, whereas if we want something more local (for example a speech bubble) we add it to the player node, and it’s going to be in the proper Z height, at the same as the player.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-6995087820663177121?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/6995087820663177121/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-27gui-input-management.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/6995087820663177121'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/6995087820663177121'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-27gui-input-management.html' title='Baddies 2.7–GUI Input Management'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-8774613960967460920</id><published>2011-08-10T13:16:00.001+02:00</published><updated>2011-08-10T13:16:45.266+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='notes'/><category scheme='http://www.blogger.com/atom/ns#' term='xna'/><title type='text'>XNA Notes–Day 3</title><content type='html'>&lt;p&gt;If you want to set a rectangle section of the screen to draw to, set the GrpahicsDevice.ScissorRectangle to a rectange and set the ScissorTestEnable property of the GraphicsDevice.RasterizerState to true.&lt;/p&gt; &lt;p&gt;GraphicsAdapter is used to deal with all the graphic output devices a pc might have (XBox and WP7 only have one), and their characteristics. &lt;/p&gt; &lt;p&gt;GraphicsDevice has a back buffer and uses double buffering. the Present method does the flip, and there are several flip modes&lt;/p&gt; &lt;ul&gt; &lt;li&gt;.PresentInterval.One (Flips when the display is in vertical retrace phase)&lt;/li&gt; &lt;li&gt;PresentInterval.Two (Flips every other vertical retrace)&lt;/li&gt; &lt;li&gt;PresentInterval.Immediate (Flips as soon as possible, can cause tearing but good for measuring performance)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;GraphicsDevice.SupportedOrientations lets you determine the positions you let the game work in WP7.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-8774613960967460920?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/8774613960967460920/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-3.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/8774613960967460920'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/8774613960967460920'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-3.html' title='XNA Notes–Day 3'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-4514723914937060346</id><published>2011-08-09T18:44:00.001+02:00</published><updated>2011-08-09T18:44:05.049+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies 2.6–Event Manager</title><content type='html'>&lt;p&gt;In a tree architecture, getting the nodes to communicate without making a huge mess of links between nodes requires special attention. That’s where the EventManager comes in. &lt;/p&gt; &lt;p&gt;&lt;br&gt;It provides a one or two way communication system among nodes in the system. The nodes don’t even need to know that the other node exists. It works on one to one and one to many modes with no difference. &lt;/p&gt; &lt;p&gt;&lt;br&gt;The EventManager is a singleton, there is only one and shared by the whole system. Anyone can register a new event, with a specific name. Once an event has been added to the EventManager, anyone can register an EventHandler&amp;nbsp; to it, and anyone can trigger it. When someone triggers an Event, everyone who registered an EventHandler gets notified. As this system is built on the delegate system of XNA, it incorporates sending extra information along with triggering an Event, mainly arguments and who triggered it. &lt;/p&gt; &lt;p&gt;&lt;br&gt;This works fine for general purpose messages (like print subtree, that orders the root node to print it’s subtree) or for messages that require a reply (someone asking for a content manager, just send an argument object with a content manager memberto fill, and check it in the sender class.)&lt;/p&gt; &lt;p&gt;&lt;br&gt;The main issue with this system is security, as messages are not guaranteed to be answered, but the Debug.Assert function covers almost all the dead ends in this case.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-4514723914937060346?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/4514723914937060346/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-26event-manager.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/4514723914937060346'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/4514723914937060346'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-26event-manager.html' title='Baddies 2.6–Event Manager'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-8568905114910949397</id><published>2011-08-09T18:03:00.001+02:00</published><updated>2011-08-09T18:03:07.170+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies 2.5–FSM and Scene</title><content type='html'>&lt;p&gt;A FSMNode (finite state machine node), is a node with the special property that it has one active node, and that is the only one which gets updated and drawn. The FSMNode also has special methods to change the active node. A FSM is a pattern I find myself using a lot when I program games, so a generic Node-like FSM is a great deal.&lt;/p&gt; &lt;p&gt;&lt;br&gt;Closely linked with the FSMNode is the Scene node. This is so because in any given game you have different scenes and only one of them active at any given time, so the FSMNode is the perfect place to hang SceneNodes. The one peculiar thing the SceneNodes have is a ContentManager, so the loaded content is local to each scene and when we swap scenes we don’t have to worry about what we load/unload. The common assets can be loaded in the Game ContentManager.&lt;/p&gt; &lt;p&gt;&lt;br&gt;Seeing as the ContentManager class already caches and takes care of assets for us, there is no immediate need to make a resource manager and manage all assets globally. Again, we sacrifice a small amount of performance for simplicity of use. &lt;/p&gt; &lt;p&gt;That’s it for the specifics of those two classes. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-8568905114910949397?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/8568905114910949397/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-25fsm-and-scene.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/8568905114910949397'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/8568905114910949397'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-25fsm-and-scene.html' title='Baddies 2.5–FSM and Scene'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-1524272379240851447</id><published>2011-08-08T19:34:00.001+02:00</published><updated>2011-08-08T19:34:43.734+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies 2.4–Camera</title><content type='html'>&lt;p&gt;First off, to define what we mean by camera. My camera in this project is just a position in 2D space, a width and a height. It does not take care of rendering itself, and is independent of screen size.&lt;/p&gt; &lt;p&gt;&lt;br&gt;The camera is a delicate element,&amp;nbsp; there are a lot of ways to implement it and a lot of variation in those ways. In this particular architecture is complicated because once we set up a camera, which by definition is a node, every node in the selected subtree needs to deal with it, and we want to avoid cluttering the draw function with a camera parameter that might even not be needed. &lt;/p&gt; &lt;p&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt; &lt;h2&gt;Methods I discarded&lt;/h2&gt; &lt;p&gt;Using a singleton, easy to access from any sub node, very little security on who controls it, the game can only have one “view”, and it doesn’t fit in the spirit of the node based system. An easy patch.&lt;/p&gt; &lt;p&gt;&lt;br&gt;Passing the camera by parameter on the draw, but this forces all draw routines to have a parameter they might not need (see GUI), creating an overloaded Draw is dangerous, as you let the user too easily forget to use the one with the camera. Another problem is even if you do it like this, you must position the camera somewhere, and it doesn’t make much sense to do so with a node, so we would have to add a special element to one of our nodes, which is the whole thing I was trying to avoid by using generic nodes.&lt;/p&gt; &lt;p&gt;&lt;br&gt;Setting the camera as a static member of a main class, like the scene, and let anyone access it like that. That’s a more “local” version than the singleton, but it suffers from most of the same problems and a few more, like the variable can be accessed with no Scene created (BAD), and we mark just ONE type of node as capable of using the camera, thus limiting the use of the camera element.&lt;/p&gt; &lt;p&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt; &lt;h2&gt;Final method&lt;/h2&gt; &lt;p&gt;The solution I found best is to derive the camera like just another node, that can be added anywhere. The node it is added to effectively takes on the camera’s position (by setting the position of the parent node to it’s negated position in every update of the camera), and that node and all the subtree it spans are displaced as required by just the normal draw mechanism, which includes adding the parent’s (now camera’s) absolute position to our relative position. &lt;/p&gt; &lt;p&gt;&lt;br&gt;The other detail is we need a proper camera that is capable of culling. For this we implement a cull method in node, which does accept a Camera object, but is only used by the camera behind scenes, and only by those “affected” by a camera, thus removing the problem mentioned with overriding the Draw method. Every node can override this cull method to suit it’s needs, and the camera calls it every update, keeping only a list of elements that are inside the camera as the ones ready to draw.&lt;/p&gt; &lt;p&gt;&lt;br&gt;That’s basically it.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-1524272379240851447?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/1524272379240851447/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-24camera.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/1524272379240851447'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/1524272379240851447'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-24camera.html' title='Baddies 2.4–Camera'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-3715782691127516086</id><published>2011-08-08T18:57:00.002+02:00</published><updated>2011-08-08T19:00:04.997+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies 2.3–Node Updating</title><content type='html'>&lt;p&gt;When dealing with a node – tree architecture, there are several functions that need to be called recursively down the tree (mainly draw and update, but there are others, such as cull, exit, etc…) AND they need to be overridden in base classes, normally by the user. &lt;/p&gt; &lt;p&gt;&lt;br /&gt;Implementing either one of these things is trivial, but doing them together and while keeping the system secure from any slips by the user is not. One possible solution is to remind the user in the documentation that when he overrides update, he must always call update on all the children of the node. But this is playing with fire, eventually someone is going to forget, and put a sneaky bug in the system. &lt;/p&gt; &lt;p&gt;&lt;br /&gt;The easiest solution I’ve come up with is putting 2 functions of each: Update and NodeUpdate, Draw and NodeDraw… where the single word function is an abstract function for the user to override, and the double word function is the one actually called in the tree update, which calls to the simple function, does some extra necessary stuff and updates the children. &lt;/p&gt; &lt;p&gt;&lt;br /&gt;This method is transparent to the user, who just needs to override the function as expected, and doesn’t let him fuck up anything important. Similar solutions might be made with delegates or callbacks.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-3715782691127516086?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/3715782691127516086/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-23node-updating.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/3715782691127516086'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/3715782691127516086'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-23node-updating.html' title='Baddies 2.3–Node Updating'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-1219577958591474819</id><published>2011-08-08T12:44:00.002+02:00</published><updated>2011-08-08T12:44:24.723+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='notes'/><category scheme='http://www.blogger.com/atom/ns#' term='xna'/><title type='text'>XNA Notes–Day 2</title><content type='html'>&lt;h2&gt;Random notes&lt;/h2&gt; &lt;p&gt;Program.cs is a stub that launches the game, it's not even used in WP7&lt;/p&gt; &lt;p&gt;GraphicsDeviceManager implements IGraphicsDeviceService and IGraphicsDeviceManager, and adds itself to the "Services" property of the game object.&lt;/p&gt; &lt;p&gt;You can create your own services and get them directly from the game object. For example a spritebatch for public access with: Services.AddService(typeof(SpriteBatch), spriteBatch); and you can access it from anywhere you can access the game with SpriteBatch spriteBatch  = Game.Services.GetService( typeof(SpriteBatch)) as SpriteBatch;&lt;/p&gt; &lt;h2&gt;GraphicsDeviceManager (GDM)&lt;/h2&gt; &lt;p&gt;Instantiating a GraphicsDeviceManager makes it create a GraphicsDevice as a property of game and of GDM. It also creates the GraphicsProfile property and the IsFullScreen property (that behaves differently depending on the platform).&lt;/p&gt; &lt;p&gt;The GDM also has preferences. The runtime attempts to use them and if it cant it falls back on the closest it can.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;PreferedMultisampling (to blend pixels together and remove jagged edges, defaults to fault because of the overhead)&lt;/li&gt; &lt;li&gt;PreferedBackbufferHeight /Width (size of the window on non-fullscreen, resolution on fullscreen and ignored in XBox and WP7)&lt;/li&gt; &lt;li&gt;PreferBackBufferFormat / ScencilFormat (how many bits used for the deph and stencil buffers)&lt;/li&gt; &lt;li&gt;SupportedOrientations (for WP7)&lt;/li&gt; &lt;li&gt;SynchronizeWithVerticalRetrace (prevents tearing by pausing until ready to draw).&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;There are also events you can hook to, most interesting the PreparingDeviceSettings, lets you override any of the settings before the device is created.&lt;/p&gt; &lt;p&gt;Two interesting methods&lt;/p&gt; &lt;ul&gt; &lt;li&gt;ApplyChanges (flushes the changes)&lt;/li&gt; &lt;li&gt;ToggleFullscrenn (changes to and from full screen)&lt;/li&gt;&lt;/ul&gt; &lt;h2&gt;Game class&lt;/h2&gt; &lt;p&gt;There are 2 methods to overload BeginDraw and EndDraw which are not visible on default. Overloading EndDraw requires you to call base.EndDraw.&lt;/p&gt; &lt;p&gt;Two other interesting methods to override, BeginRun and EndRun, called just before begins/ after finishes to run.&lt;/p&gt; &lt;p&gt;Events you can hook to:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;OnActivated (Game is activated at the beginign and any time it regains focus)&lt;/li&gt; &lt;li&gt;OnDeactivated (Ditto)&lt;/li&gt; &lt;li&gt;OnExiting (Just once when the game is exiting)&lt;/li&gt;&lt;/ul&gt; &lt;h3&gt;Methods in Game&lt;/h3&gt; &lt;ul&gt; &lt;li&gt;Exit (Causes the game to start shutting down)&lt;/li&gt; &lt;li&gt;ResetElapsedTime (Does exactly that)&lt;/li&gt; &lt;li&gt;Run (As expected in W and XBox, but throws an exception in WP7 as it's a blocking method)&lt;/li&gt; &lt;li&gt;SupressDraw (Stops calling draw until the next time Update is called)&lt;br /&gt;Tick (Advances one frame, calls Update and Draw)&lt;/li&gt;&lt;/ul&gt; &lt;h3&gt;Propierties in Game&lt;/h3&gt; &lt;ul&gt; &lt;li&gt;InactiveSleepTime (Tells the system how long to sleep when not the active process, default 20 milliseconds)&lt;/li&gt; &lt;li&gt;IsActive (Tells you if you're the active process)&lt;/li&gt; &lt;li&gt;LaunchParameters (Gests the comand line parameters of in WP7 information oabout the parameters required for launch)&lt;/li&gt;&lt;/ul&gt; &lt;h2&gt;Game time&lt;/h2&gt; &lt;p&gt;Game has two parameters to handle time IsFixedTimeStep and TargetElapsedTime, and the update method gets passed a GameTime object with the current time. Interesting detail is the IsRunningSlowly property in the GameTime object, which can be used in FixedTimeStep mode.&lt;br /&gt;For measuring the performance of the game, variable time step should be used.&lt;/p&gt; &lt;h2&gt;Components &lt;/h2&gt; &lt;p&gt;Encapsulated game objects of 3 types, GameComponent, DrawableGameComponent and GamerServicesComponent.&lt;br /&gt;Focusing on the first 2, they have the same structure more or less than Game.&lt;br /&gt;You can add components in the Game class initialize method, and they get handled by the Game, with no supervision.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-1219577958591474819?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/1219577958591474819/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/1219577958591474819'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/1219577958591474819'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-2.html' title='XNA Notes–Day 2'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-2517726934905779123</id><published>2011-08-06T19:06:00.001+02:00</published><updated>2011-08-06T19:06:11.598+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies 2.2–Node Positioning</title><content type='html'>&lt;p&gt;Each node in the game tree has two possible options when drawing, either relative to the parent node, or with absolute coordinates respect to the screen. This is done as some things like the GUI need to be relative to the screen, regardless of where they are hooked up in the game tree, and for most&amp;nbsp; of the compound objects (like a player with a sword and a shadow) it makes it a lot easier if you just put the sword and shadow relative to the player and forget about it (when the player moves, the sword and shadow will as well.)&lt;/p&gt; &lt;p&gt;&lt;br&gt;But this apparently simple system raises some questions. First and foremost, do we keep 2 sets of coordinates? We keep the absolute coordinates and (needed for drawing from everyone) and the relative ones? Or we recalculate one of them?&lt;br&gt;And if we recalculate one of them, do we do it on demand? Or every cycle?&lt;br&gt;And finally, how do we keep this coordinates “safe” so there are no hidden bugs when the user modifies one set and not the other?&lt;/p&gt; &lt;p&gt;&lt;br&gt;The answer I came with is to keep just the relative coordinates as &lt;strong&gt;position&lt;/strong&gt;, and a variable &lt;strong&gt;parentRelativePos&lt;/strong&gt;, which indicates whether the coordinates are relative to the parent post or not.&amp;nbsp; No double set of coordinates because data replication is a sure way for innocent bugs to creep invisible into the system, and still attack months later in the development. So by elimination we end up with just one set of data.&lt;/p&gt; &lt;p&gt;&lt;br&gt;As for recalculating them, it will be apparent by now that for the vast majority of nodes, it is only needed to recalculate the absolute position it when drawing (for those relative to the parent position), and not at all for the rest (the not relative to the parent position nodes can just draw with their normal &lt;strong&gt;position&lt;/strong&gt; variable, as it’s absolute coordinates). &lt;/p&gt; &lt;p&gt;&lt;br&gt;So, the problem boils down to recalculating when drawing, or when the &lt;strong&gt;position&lt;/strong&gt; changes. Doing it when the position changes is viable ONLY if we keep the position variable private, and all changes to it go through an accessor method that updates the position and the “&lt;strong&gt;drawPosition&lt;/strong&gt;”, but that is still a possible nest of bugs (maybe we need to open the level of security of &lt;strong&gt;position&lt;/strong&gt; to protected, or have some unacceptable access times in derived classes from node, and this is again giving a user full access to fuck up). So we recalculate the &lt;strong&gt;drawPosition&lt;/strong&gt; every draw cycle, by adding the parent &lt;strong&gt;drawPosition&lt;/strong&gt; to our own &lt;strong&gt;position&lt;/strong&gt;. Just 3 additions per cycle of extra cost, not so bad when the alternative is ninja bugs all over the code. &lt;/p&gt; &lt;p&gt;&lt;br&gt;In conclusion, one variable that is &lt;strong&gt;position&lt;/strong&gt;, the Boolean &lt;strong&gt;parentRelativePos&lt;/strong&gt; indicates whether it is a relative or absolute position, and the &lt;strong&gt;drawPosition&lt;/strong&gt; is updated every draw cycle from our position and our parents &lt;strong&gt;drawPosition&lt;/strong&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-2517726934905779123?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/2517726934905779123/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-22node-positioning.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2517726934905779123'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2517726934905779123'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-22node-positioning.html' title='Baddies 2.2–Node Positioning'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-2660545368263997700</id><published>2011-08-06T18:33:00.001+02:00</published><updated>2011-08-06T18:33:37.648+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='notes'/><category scheme='http://www.blogger.com/atom/ns#' term='xna'/><title type='text'>XNA Notes–Day 1</title><content type='html'>&lt;p&gt;I’ve been programming on and off in XNA for a few years now, but I never really tried to learn XNA, just picked it up on the go, googling whatever was needed at the time. But now I’m working on a more dedicated project, and not knowing my tools in depth can only lead to suboptimal solutions. Therefore, I have decided to pick up the XNA Game Studio 4.0 Programming book, by Tom Miller and Dean Johnson, and start studying it from the beginning. &lt;/p&gt; &lt;p&gt;This series of XNA notes are just that, notes from studying, with curious / useful facts from the book, as I have a horrible memory and writing things down is the only way to remember them. Besides, someone else might find something useful here, who knows?&lt;/p&gt; &lt;p&gt;So, without further ado, today’s notes:&lt;/p&gt; &lt;h2&gt;Random notes&lt;/h2&gt; &lt;p&gt;Game Studio 4.0 is broken up in two different profiles, Reach which encompasses features for all platforms and HiDef, which includes extra features that exist only in W and Xbox. &lt;/p&gt; &lt;p&gt;The origin of coordinates is the top left.&lt;/p&gt; &lt;p&gt;font.MeassureString(string) tells you the width and height of the string in that font.&lt;/p&gt; &lt;h2&gt;SpriteBatch&lt;/h2&gt; &lt;p&gt;Tinting in spritebatch.draw multiplies the original color of the image by the color of the tint.&lt;/p&gt; &lt;p&gt;Using a Vector2 instead of a rectangle means we don't do any stretching to the image when we render it on screen, we draw it with the original size.&lt;/p&gt; &lt;h3&gt;Different overloads&lt;/h3&gt; &lt;p&gt;In the appropriate draw overloads, the source rectangle lets you decide which part of the original image you're going to draw. Useful for frame animations.&lt;br&gt;In the appropriate draw overloads you can rotate the image using an angle in radians and an origin for the rotation.&amp;nbsp; You can also scale the image.&lt;br&gt;You have some crappy SpriteEffect options, which are to flip horizontaly, to flip vertically or none. &lt;br&gt;There is a Z ordering for the drawing between 1.0 (top) and 0.0 (bottom) that only matter if you play arround with the sorting modes.&lt;/p&gt; &lt;h3&gt;Sprite Sorting&lt;/h3&gt; &lt;p&gt;Using spritebatch.begin we can specify the draw mode, among others SpriteSortMode…&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Texture (sort by texture, do all of the same texture at the same time as not to have to change, default), &lt;/li&gt; &lt;li&gt;FrontToBack / BackToFront (taking into account the depth specified for each draw call),&lt;/li&gt; &lt;li&gt;Deferred (Drawn in the order it is used in the draw calls), &lt;/li&gt; &lt;li&gt;Immediate (different from all others bacause hardware render calls are not batched, but instead we have one hardware call per draw call).&lt;/li&gt; &lt;li&gt;&lt;/li&gt;&lt;/ul&gt; &lt;h3&gt;Sprite Blending&lt;/h3&gt; &lt;p&gt;Using spritebatch.begin we can also specify the blend state, among others BlendState.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;AlphaBlend (default blending mode, transparency works fine),&lt;/li&gt; &lt;li&gt;Opaque (No blending),&lt;/li&gt; &lt;li&gt;Aditive (adds the colors together).&lt;/li&gt;&lt;/ul&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-2660545368263997700?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/2660545368263997700/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2660545368263997700'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2660545368263997700'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/xna-notesday-1.html' title='XNA Notes–Day 1'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-775827255383583670</id><published>2011-08-05T17:59:00.001+02:00</published><updated>2011-08-05T17:59:45.189+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies 2.1–Nodes and Tree Architecture</title><content type='html'>&lt;p&gt;The game engine follows a tree based architecture, that is, all elements are nodes, and any node might be added as a child to someone else. Every node might have at most one parent. Specific cases and conditions can be dealt with from each individual node.&lt;br&gt;There are three main reasons I’ve opted for this architecture, to the point of redoing several months of work.&lt;br&gt;&lt;/p&gt; &lt;h2&gt;Low coupling&lt;/h2&gt; &lt;p&gt;Ideally, in any computer science project there is low coupling and high cohesion. This means, in an OOP environment, that the best situation would be one where no class needs to know about any other to function. This is a goal worth pursuing, even though it is not always reasonable to stick to this guideline, as you need to sacrifice other factors (speed, mainly) to achieve it, and that is simply not an option in a game project.&lt;br&gt;But, low coupling is good, as I’ve found out in countless projects, and most games can be designed as to minimize coupling and be all the better for it. And a tree based architecture is awesome for this because since any node can be added to any other node, you can never assume a node will have a particular parent on whose internal workings it relies, therefore you must always write modular code. &lt;br&gt;This, of course, takes a toll on efficiency and forces us to use several communication mechanisms we shall see later.&lt;br&gt;&lt;/p&gt; &lt;h2&gt;Avoiding repetition&lt;/h2&gt; &lt;p&gt;In almost all game projects I’ve done, I find the objects follow a hierarchical structure, and from each parent object I usually have to call the “Enter, Update, Draw, Exit” functions of all of its children, for which I have to create individual variables, and so on. There is a lot of duplicated effort for each object you add to a class. &lt;br&gt;A tree architecture solves all this by putting all items as nodes, so they can be stored and treated in a generic way. No more remembering to modify half the class when I add a new variable or remove it.&lt;br&gt;The down side is that accessing elements in a class from the outside is slower as a search of some sort, or even a direct access to the index of the element requires extra work, and therefore time.&lt;br&gt;&lt;/p&gt; &lt;h2&gt;Empirical experience&lt;/h2&gt; &lt;p&gt;I worked with the Cocos libraries in a couple of projects and found it to be a delightful system, working fine in the limited environment that is a smartphone.&lt;/p&gt; &lt;h2&gt;Conclusion&lt;/h2&gt; &lt;p&gt;All in all, it’s a good system for games that don’t try to squeeze the processor dry, and like a clean easy to maintain system.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-775827255383583670?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/775827255383583670/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-21nodes-and-tree-architecture.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/775827255383583670'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/775827255383583670'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-21nodes-and-tree-architecture.html' title='Baddies 2.1–Nodes and Tree Architecture'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-2728071885664089329</id><published>2011-08-04T10:21:00.002+02:00</published><updated>2011-08-05T17:59:26.363+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies 1.3–Project distribution</title><content type='html'>&lt;p&gt;Reflecting the new architecture explained in the previous article (&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-12-architecture.html"&gt;Baddies 1.2 – Architecture&lt;/a&gt;), the whole project was divided in 4 MVS projects, though within a single solution.&lt;/p&gt; &lt;p&gt;&lt;br /&gt;Microsoft Visual Studio is a superb tool for this kind of things and as long as you don’t put recursive references of a project within a project, it gets the job done very easily. &lt;/p&gt; &lt;p&gt;&lt;br /&gt;The project structure ends up looking like this:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Core&lt;/li&gt; &lt;li&gt;Game&lt;/li&gt; &lt;li&gt;Editor&lt;/li&gt; &lt;li&gt;Content&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;br /&gt;The Content project holds the content files (graphics, audio, and so on) used by any of the other 3 projects, as the amount of non-shared assets in the whole solution is relatively small compared to the total amount of assets.&lt;br /&gt;The references between all the projects are the logical ones, with the game and editor using the core and content. &lt;/p&gt; &lt;p&gt;&lt;a href="http://lh6.ggpht.com/-6RxyjPAliKc/TjpWccWixuI/AAAAAAAACDY/4bgPmjI3rAU/s1600-h/ProjDep%25255B2%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="ProjDep" border="0" alt="ProjDep" src="http://lh3.ggpht.com/-5XZ8TGlNC8E/TjpWdMNI7rI/AAAAAAAACDc/Y-cnu97P23I/ProjDep_thumb.png?imgmax=800" width="229" height="172" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;This setup of projects is interesting to remember as it’s common to a lot of game projects (tough instead of the editor there might be another secondary tool for the game).&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-2728071885664089329?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/2728071885664089329/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-13project-distribution.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2728071885664089329'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2728071885664089329'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-13project-distribution.html' title='Baddies 1.3–Project distribution'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-5XZ8TGlNC8E/TjpWdMNI7rI/AAAAAAAACDc/Y-cnu97P23I/s72-c/ProjDep_thumb.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-6299726909513108678</id><published>2011-08-03T17:53:00.002+02:00</published><updated>2011-08-04T09:58:35.249+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies 1.2 - Architecture</title><content type='html'>&lt;p&gt;The initial project was to make a single complete game, and the architecture reflects this. It’s a tree based system where almost every element of the game is a node, and any node can be attached to another. The architecture is inspired on the one implemented very successfully by the&lt;a href="http://www.cocos2d-iphone.org/"&gt; Cocos2D&lt;/a&gt; libraries.&lt;br /&gt;Here is a class diagram in a simplified UML notation, with each class linked to where it was going to be used later on, instead of normal UML composition, as any node can be linked to any node as need arises.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh6.ggpht.com/-dtIStULtQQY/Tjlu3FQcHgI/AAAAAAAACC4/dcjfMECwJLY/s1600-h/Architecture%2525202%25255B2%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="Architecture 2" border="0" alt="Architecture 2" src="http://lh3.ggpht.com/-qRn0mKzVv5I/Tjlu3gAfnCI/AAAAAAAACC8/yJTQ2MNvOBs/Architecture%2525202_thumb.png?imgmax=800" width="244" height="181" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;After the start of the project, it became apparent however that an editor was needed for the game. The editor shared many of the classes of the original game, thus the need for a library of common classes that both projects (game and editor) needed to access. This evolved into 3 separate architectures, as follows.&lt;/p&gt; &lt;h2&gt;Core architecture&lt;/h2&gt; &lt;p&gt;The core architecture has all the classes of the core engine and their dependency to the Node class. Classes in green are XNA classes.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh6.ggpht.com/-UQDFIaXmYZ8/Tjlu4d-TA_I/AAAAAAAACDA/153KWI7p2t0/s1600-h/Architecture%252520Core%25255B2%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="Architecture Core" border="0" alt="Architecture Core" src="http://lh3.ggpht.com/-xNjW7cF6vss/Tjlu4z26gMI/AAAAAAAACDE/IobOgpJKTbY/Architecture%252520Core_thumb.png?imgmax=800" width="244" height="57" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;h2&gt;Game architecture&lt;/h2&gt; &lt;p&gt;The game architecture has the relationship of the core classes among each other to create the actual game. All the white classes are from the core library, while blue classes are specific to the actual game.&lt;br /&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-SkuEF8ZYfYU/Tjlu5vPpcvI/AAAAAAAACDI/H4JN9HCYmI8/s1600-h/Architecture%252520Game%25255B2%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="Architecture Game" border="0" alt="Architecture Game" src="http://lh3.ggpht.com/-KVxwGfQVQaU/Tjlu6ArRVwI/AAAAAAAACDM/RdodviABmEg/Architecture%252520Game_thumb.png?imgmax=800" width="244" height="109" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;h2&gt;Editor architecture&lt;/h2&gt; &lt;p&gt;The editor architecture is mainly one editor form and one game editor closely coupled via a picture box form. Inside the game editor we build the actual game to be shown, and in the editor form we create the GUI via windows forms.&lt;br /&gt;&lt;/p&gt;   &lt;p&gt;&lt;a href="http://lh6.ggpht.com/-UDow0uGZdOw/Tjlu6w_xTII/AAAAAAAACDQ/e4lo2Afkf7Q/s1600-h/Architecture%252520Editor%25255B2%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="Architecture Editor" border="0" alt="Architecture Editor" src="http://lh3.ggpht.com/-LnI6Mfcc_1o/Tjlu7Z_sGPI/AAAAAAAACDU/sorV30rhYt4/Architecture%252520Editor_thumb.png?imgmax=800" width="244" height="216" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-6299726909513108678?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/6299726909513108678/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-12-architecture.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/6299726909513108678'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/6299726909513108678'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/08/baddies-12-architecture.html' title='Baddies 1.2 - Architecture'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-qRn0mKzVv5I/Tjlu3gAfnCI/AAAAAAAACC8/yJTQ2MNvOBs/s72-c/Architecture%2525202_thumb.png?imgmax=800' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-7111240877018309119</id><published>2011-07-17T19:57:00.016+02:00</published><updated>2011-08-27T22:38:15.886+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Baddies'/><title type='text'>Baddies – 1.1 Introduction</title><content type='html'>&lt;p&gt;This is the first of a series of articles documenting the creation of the Baddies project.  &lt;/p&gt; &lt;p&gt;&lt;br /&gt;The Baddies project is a game idea I have been musing for quite a long time, and as I have found that an initial all-encompassing game architecture to be well beyond my means, I shall attempt to do it in an incremental fashion (tough following an initial architecture diagram), documenting each design decision whilst coding. What better way to do this than by blogging about it?&lt;/p&gt; &lt;p&gt;&lt;br /&gt;The Baddies game is a tactic based game, with heavy focus on the minions the player can control and some base building. The story of the game also plays a very important role in the final version. &lt;/p&gt;&lt;p&gt;It’s a game aimed for Xbox, windows and Windows Phone 7. For simplicity I shall focus first on the windows version and leave the other 2 for later on. I build on the XNA framework for the code of the game. I work on Microsoft Visual Studio 2010 as my IDE, and with subversion as my backup version storage.&lt;/p&gt; &lt;p&gt;&lt;br /&gt;I will use this post as an index for the rest of articles in this series. The articles will only focus on the code side of this project tough. Any story or graphics news will have to wait till almost completion of the game.&lt;/p&gt; &lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/07/baddies-11-introduction.html"&gt;1 – General&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/07/baddies-11-introduction.html"&gt;1.1 – Introduction&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-12-architecture.html"&gt;1.2 - Architecture&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-13project-distribution.htmlhttp://magicscrollsofcode.blogspot.com/2011/08/baddies-13project-distribution.html"&gt;1.3 - Project distribution&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-21nodes-and-tree-architecture.html"&gt;2 - Core&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-21nodes-and-tree-architecture.html"&gt;2.1 - Nodes and Tree Architecture&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-22node-positioning.html"&gt;2.2 - Node Positioning&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-23node-updating.html"&gt;2.3 - Node Updating&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-24camera.html"&gt;2.4 - Camera&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-25fsm-and-scene.html"&gt;2.5 - FSM and Scene&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-26event-manager.html"&gt;2.6 - Event Manager&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-27gui-input-management.html"&gt;2.7 - GUI Input Management&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-28map.html"&gt;2.8 - Map&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-3editor.html"&gt;3 - Editor&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://magicscrollsofcode.blogspot.com/2011/08/baddies-4saving.html"&gt;4 - Saving &lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-7111240877018309119?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/7111240877018309119/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/07/baddies-11-introduction.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/7111240877018309119'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/7111240877018309119'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2011/07/baddies-11-introduction.html' title='Baddies – 1.1 Introduction'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-3325642493704880809</id><published>2010-12-14T20:41:00.002+01:00</published><updated>2012-02-04T23:01:50.206+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AI'/><category scheme='http://www.blogger.com/atom/ns#' term='Tutorial'/><title type='text'>Behavior Trees by Example. AI in an Android game.</title><content type='html'>&lt;h1&gt;Introduction&lt;/h1&gt;  &lt;p&gt;I'm going to cover the implementation of a very useful technique for AI programmers called Behavior Trees (BT for now on). I've been using this technique for the AI in the Android game I'm working on, and found a few quirks in the architecture that made me have to struggle somewhat. Just thought I'd share what I've learnt during the process.&lt;/p&gt;  &lt;p&gt;As this is for an Android game, the code is in Java. Even so, it is easily ported to C++, and there are almost no Android specific libraries or function calls to worry about.&lt;/p&gt;  &lt;h2&gt;What are Behavior Trees?&lt;/h2&gt;  &lt;p&gt;The internet is crawling with places to find a good theory on what behavior trees are, and how to implement them, for example:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;AIGameDev : &lt;a href="http://aigamedev.com/open/articles/bt-overview/"&gt;Overview&lt;/a&gt;; &lt;a href="http://aigamedev.com/open/articles/popular-behavior-tree-design/"&gt;Approaches&lt;/a&gt;; Video tutorial &lt;a href="http://aigamedev.com/open/articles/behavior-trees-part1/"&gt;part 1&lt;/a&gt;, &lt;a href="http://aigamedev.com/open/articles/behavior-trees-part2/"&gt;part 2&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Behavior_Trees"&gt;Wikipedia&lt;/a&gt; (Not recommended for videogame development, but interesting) &lt;/li&gt;    &lt;li&gt;Etc... &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Overview&lt;/h2&gt;  &lt;p&gt;This article assumes you know what a BT is. If not have a look at the mentioned articles. I’m going to work with a system with the following architecture.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_vaakdclE4vY/TQfIXD96tZI/AAAAAAAABJg/vExnkX40jds/s1600-h/ClassDiagramBT%5B12%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="ClassDiagramBT" border="0" alt="ClassDiagramBT" src="http://lh5.ggpht.com/_vaakdclE4vY/TQfIX3ruauI/AAAAAAAABJk/r5XGZlYh5bY/ClassDiagramBT_thumb%5B8%5D.png?imgmax=800" width="294" height="424" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It may look complex at a glance, but once we’ve gone over each part you will see like it’s quite understandable, elegant and flexible. At a glance you can see 3 “groups” of classes. The Tasks (yellow), the TaskControllers (blue) and the Decorators (green). We shall consider each of these in turn, and then how they all fit together.&lt;/p&gt;  &lt;h1&gt;Architecture&lt;/h1&gt;  &lt;h2&gt;General&lt;/h2&gt;  &lt;p&gt;The general idea for the system is that you have a set of modular tasks (MoveTo, FindClosestEnemy, FindFleeDirection, WaitTillArrivedAtDestination…) and you use them to form a BT. The base Task class provides a interface for all these tasks, the leaf tasks are the ones just mentioned, and the parent tasks are the interior nodes that decide which task to execute next. &lt;/p&gt;  &lt;p&gt;The tasks have only the logic they need to actually do what is required of them, all the decision logic of whether a task has started or not, if it needs to update, if it has finished with success, etc. is grouped in the TaskController class, and added by composition. This is done for two reasons, one is for elegance when creating new tasks, the other for security when creating new decorators.&lt;/p&gt;  &lt;p&gt;The decorators are tasks that “decorate” another class by wrapping over it and giving it additional logic. In our case we use them to give a update speed to a certain task, or to reset it once is done, or similar things. You can read about the Decorator pattern &lt;a href="http://www.oodesign.com/decorator-pattern.html"&gt;here&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;Finally, the Blackboard class is a class owned by the parent AI that every task has a reference to. It works as a knowledge database for all the leaf tasks, and it’s where we keep the data that is generated on one task and needs to be used by another. &lt;/p&gt;  &lt;h2&gt;Tasks&lt;/h2&gt;  &lt;h3&gt;Task&lt;/h3&gt;  &lt;p&gt;All tasks follow the same interface:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e56bf2fa-bfaa-419a-8cf0-6eb9fb1c7db6" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Base abstract class for all the tasks in &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * the behavior tree.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; Task &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Reference to the Blackboard data&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; Blackboard bb;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Creates a new instance of the Task class&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param blackboard Reference to the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; *    AI Blackboard data&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Task(Blackboard blackboard)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.bb = blackboard;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Override to do a pre-conditions check to &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * see if the task can be updated.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * @return True if it can, false if it can't&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; boolean CheckConditions();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Override to add startup logic to the task&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Start();&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Override to add ending logic to the task&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; End();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Override to specify the logic the task &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * must update each cycle&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoAction();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Override to specify the controller the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * task has&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @return The specific task controller.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; TaskController GetControl();&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;      &lt;p&gt;As in any BT node, a CheckConditions and a DoAction functions, to check if the node can be updated, and to actually update the node, respectively. The Start and End functions are called just before starting to update the node, and just after finishing the logic of the node. &lt;/p&gt;  &lt;p&gt;To be a true interface, the Blackboard member should not be in the class, but for simplicity and ease of use, it’s the perfect place for it. So fuck interfaces then.&lt;/p&gt;  &lt;h3&gt;Leaf Task&lt;/h3&gt;  &lt;h4&gt;Base LeafTask class&lt;/h4&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ec579fee-0cd1-4dcf-9b8c-202ab3c65e94" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Leaf task (or node) in the behavior tree.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; *  &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Specifies a TaskControler, by composition, &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * to take care of all the control logic, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * without burdening the Task class with &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * complications.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; LeafTask extends Task &lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Task controler to keep track of the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Task state.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; TaskController control;&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Creates a new instance of the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * LeafTask class&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * @param blackboard Reference to the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; *  AI Blackboard data&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; LeafTask(Blackboard blackboard) &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        super(blackboard);&lt;/li&gt; &lt;li&gt;        CreateController();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Creates the controller for the class&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; CreateController()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.control = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; TaskController(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Gets the controller reference.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    @Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; TaskController GetControl()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.control;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;The leaf task has a TaskController for the support logic of the class, and implements the GetControl method. It has no more extra logic or methods. &lt;/p&gt;  &lt;p&gt;The child classes of the LeafTask class are the ones with the real logic of the system, and each time we want a new behavior we are going to have to create a few. They are building blocks, modular, and they can be combined in different ways to generate different effects. Take for example simplistic Chase and a Flee strategies, they could work like this:&lt;/p&gt;  &lt;p&gt;Chase&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;GetClosestEnemy&lt;/li&gt;    &lt;li&gt;SetClosestEnemyPositionAsDestination&lt;/li&gt;    &lt;li&gt;&lt;em&gt;MoveToDestination&lt;/em&gt;&lt;/li&gt;    &lt;li&gt;&lt;em&gt;WaitUntilPositionNearDestination&lt;/em&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Flee&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;CalculateFleeDestination&lt;/li&gt;    &lt;li&gt;&lt;em&gt;MoveToDestination&lt;/em&gt;&lt;/li&gt;    &lt;li&gt;&lt;em&gt;WaitUntilPositionNearDestination&lt;/em&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The italics mark the reused leaf nodes in two different sequences of behaviors. That shows how modular leaf nodes can be recycled and used to conform many different behaviors. &lt;/p&gt;  &lt;p&gt;Leaf tasks often calculate data needed by other leaf tasks. Instead of making a complex linking of the whole tree of tasks, we just use a common “data dump” for all of them, a Blackboard object shared by all the tasks, that simply has member data used by the different tasks.&lt;/p&gt;  &lt;p&gt;Some examples of Leaf Nodes follow.&lt;/p&gt;  &lt;h4&gt;LeafTask Examples&lt;/h4&gt;  &lt;h5&gt;GetClosestEnemyTask&lt;/h5&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:02964f48-7cef-48cd-891f-d43ee8d8c705" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Finds the closest enemy and stores &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * it in the Blackboard.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; GetClosestEnemyTask extends LeafTask &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Creates a new instance of the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * GetClosestEnemyTask task&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param blackboard Reference to the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; *  AI Blackboard data&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; GetClosestEnemyTask(Blackboard bb)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        super(bb);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Checks for preconditions&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    @Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; boolean CheckConditions() &lt;/li&gt; &lt;li&gt;    {        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; Blackboard.players.size() &amp;gt; 1 &amp;amp;&amp;amp; &lt;/li&gt; &lt;li&gt;        bb.player.GetCursor().GetPosition() != null;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Finds the closest enemy and stores &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * it in the Blackboard&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    @Override&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoAction() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        Enemy closestEnemy = null;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#008000"&gt;// Finds the closest enemy&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000"&gt;// Omited for clarity&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000"&gt;// Store it in the blackboard&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        bb.closestEnemy = closestEnemy;&lt;/li&gt; &lt;li&gt;        GetControll().FinishWithSuccess();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Ends the task&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    @Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; End() &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        LogTask(&lt;span style="color:#a31515"&gt;"Ending"&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Starts the task&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    @Override&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Start() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        LogTask(&lt;span style="color:#a31515"&gt;"Starting"&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;MoveTo&lt;/h5&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0d207c7a-4ec0-41ce-ab18-4276df632124" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * This task requests the player to &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * move to a given destination.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * It takes the destination from &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * the Blackboard.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; MoveToDestinationTask extends LeafTask &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Creates a new instance of the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * MoveToDestinationTask class&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param blackboard Reference of the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; *  AI Blackboard data&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; MoveToDestinationTask(Blackboard bb)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        super(bb);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Sanity check of needed data for the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * operations&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    @Override&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; boolean CheckConditions() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        LogTask(&lt;span style="color:#a31515"&gt;"Checking conditions"&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; bb.moveDirection != null;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Requests the cursor to move&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    @Override&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoAction() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        LogTask(&lt;span style="color:#a31515"&gt;"Doing action"&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        bb.player.Move(bb.moveDirection);&lt;/li&gt; &lt;li&gt;        control.FinishWithSuccess();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Ends the task&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    @Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; End() &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        LogTask(&lt;span style="color:#a31515"&gt;"Ending"&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Starts the task&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    @Override&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Start() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        LogTask(&lt;span style="color:#a31515"&gt;"Starting"&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h3&gt; &lt;/h3&gt;  &lt;h3&gt;Parent Task&lt;/h3&gt;  &lt;p&gt;Parent task require a bit more explanation than leaf tasks, but don’t worry, we’ll work through it step by step. The base class is similar to the LeafTask class:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0927473d-786e-47e9-bc1e-6d2b96878324" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 3em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Inner node of the behavior tree, a &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * flow director node that selects the&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * child to be executed next.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * (Sounds macabre, hu?)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * Sets a specific kind of TaskController for &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * these kinds of tasks.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; ParentTask extends Task &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* TaskControler for the parent task&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;ParentTaskController control;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; ParentTask(Blackboard bb)&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    super(bb);&lt;/li&gt; &lt;li&gt;    CreateController();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* Creates the TaskController.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; CreateController()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.control = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; ParentTaskController(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* Gets the control reference&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;@Override&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; TaskController GetControl() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; control;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* Checks for the appropiate pre-state &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* of the data&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;@Override&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; boolean CheckConditions() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    LogTask(&lt;span style="color:#a31515"&gt;"Checking conditions"&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; control.subtasks.size() &amp;gt; 0;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* Abstract to be overridden in child &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* classes. Called when a child finishes &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* with success.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ChildSucceeded();&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* Abstract to be overridden in child &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* classes. Called when a child finishes &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* with failure.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ChildFailed();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* Checks whether the child has started, &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* finished or needs updating, and takes &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* the needed measures in each case&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* "curTask" is the current selected task,&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* a member of our ParentTaskController&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;@Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoAction() &lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    LogTask(&lt;span style="color:#a31515"&gt;"Doing action"&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(control.Finished())&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000"&gt;// If this parent task is finished&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#008000"&gt;// return without doing naught.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(control.curTask == null)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000"&gt;// If there is a null child task &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#008000"&gt;// selected we've done something wrong&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// If we do have a curTask...&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;( !control.curTask.&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        GetControl().Started())&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#008000"&gt;// ... and it's not started yet, start it.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        control.curTask.&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            GetControl().SafeStart();&lt;/li&gt; &lt;li&gt;    }        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(control.curTask.&lt;/li&gt; &lt;li&gt;        GetControl().Finished())&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000"&gt;// ... and it's finished, end it properly.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        control.curTask.&lt;/li&gt; &lt;li&gt;            GetControl().SafeEnd();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(control.curTask.&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            GetControl().Succeeded())&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.ChildSucceeded();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(control.curTask.&lt;/li&gt; &lt;li&gt;            GetControl().Failed())&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.ChildFailed();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    {        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#008000"&gt;// ... and it's ready, update it.        &lt;/span&gt;&lt;/li&gt; &lt;li&gt;        control.curTask.DoAction();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }    &lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* Ends the task&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;@Override&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; End() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    LogTask(&lt;span style="color:#a31515"&gt;"Ending"&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* Starts the task, and points the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* current task to the first one &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* of the available child tasks.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;@Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Start() &lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    LogTask(&lt;span style="color:#a31515"&gt;"Starting"&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    control.curTask = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        control.subtasks.firstElement();&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(control.curTask == null)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        Log.e(&lt;span style="color:#a31515"&gt;"Current task has a null action"&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;    &lt;p&gt;The ParentTask class has a ParentTaskController, identical to the TaskController for the LeafNode (handles state logic, if it’s ready, finished, ect…) but adds responsibility for the child tasks (a subtasks vector) and the current selected task (curTask).&lt;/p&gt;  &lt;p&gt;It also implements the Start and End functions, very similar to the LeafTask. But the big change comes in the DoAction function. Here it updates the child of the parent task acordingly. All Parent tasks have the same process, that can be explained conceptually more or less like this:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Safety checks to see if we have all the data, and it’s not null&lt;/li&gt;    &lt;li&gt;If the current child task is not started, start it&lt;/li&gt;    &lt;li&gt;Else, if the current child task is finished, finalize it propperly and check if it finished with success or failure. &lt;strong&gt;Here&lt;/strong&gt; is where the different ParentTask differ, on what happens if the child fails or succeeds. (ChildSuceeded and ChildFailed virtual functions)&lt;/li&gt;    &lt;li&gt;Else, the child must be updated, so we call it’s DoAction.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The abstract ChildSuceeded and ChildFailed functions are for the classes derived from ParentTask to specify how they behave when a child ends their execution. We have two distinct cases for this, Sequence and Selector classes.&lt;/p&gt;  &lt;h4&gt;Sequence&lt;/h4&gt;  &lt;p&gt;In a sequence of tasks, if a child ends it’s execution with a &lt;strong&gt;failure&lt;/strong&gt;, we &lt;strong&gt;bail&lt;/strong&gt; and the Sequence ParentTask ends with failure. This makes sense, as sequences are used for example for the Chase explained earlier:&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;GetClosestEnemy&lt;/li&gt;      &lt;li&gt;SetClosestEnemyPositionAsDestination&lt;/li&gt;      &lt;li&gt;&lt;em&gt;MoveToDestination&lt;/em&gt;&lt;/li&gt;      &lt;li&gt;&lt;em&gt;WaitUntilPositionNearDestination&lt;/em&gt;&lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;p&gt;If task 2 fails, we don’t want the ParentTask to continue on to MoveToDestination, as the Destination Vec2 is possibly corrupted, and will result in undefined behavior. &lt;/p&gt;  &lt;p&gt;On the other hand if a child finishes with success, we continue onto the next task.&lt;/p&gt;  &lt;p&gt;Some code to illustrate this:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9efcd594-2cc4-4cee-a1f8-8f5108337df9" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * This ParentTask executes each of it's children &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * in turn until he has finished all of them.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * It always starts by the first child, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * updating each one.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * If any child finishes with failure, the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Sequence fails, and we finish with failure.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * When a child finishes with success, we &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * select the next child as the update victim.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * If we have finished updating the last child, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * the Sequence returns with success.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; Sequence extends ParentTask &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Creates a new instance of the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Sequence class&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param blackboard Reference to &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * the AI Blackboard data&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Sequence(Blackboard bb)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        super(bb);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * A child finished with failure.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * We failed to update the whole sequence. &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Bail with failure.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    @Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ChildFailed() &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        control.FinishWithFailure();&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * A child has finished with success&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Select the next one to update. If &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * it's the last, we have finished with &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * success.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    @Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ChildSucceeded() &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; curPos = &lt;/li&gt; &lt;li&gt;            control.subtasks.&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            indexOf(control.curTask);&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;( curPos == &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            (control.subtasks.size() - 1))&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            control.FinishWithSuccess();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            control.curTask = &lt;/li&gt; &lt;li&gt;                control.subtasks.&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                elementAt(curPos+1);&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(!control.curTask.CheckConditions())&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                control.FinishWithFailure();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h4&gt; &lt;/h4&gt;  &lt;h4&gt;Selector&lt;/h4&gt;  &lt;p&gt;The selector chooses one it’s children to update, and if that fails it chooses another until there are no more left. This means that if the current chosen child ends with failure, we choose another. If we can’t choose another, we end with failure. On the other hand, if our child returns with success, we can happily finish with success as well, as we only need one of the children to succeed.&lt;/p&gt;  &lt;p&gt;In retrospect, the Sequencer is like a AND gate, and the Selector as a OR gate, only applied to tasks instead of circuits…&lt;/p&gt;  &lt;p&gt;Some code then.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e4b2a1f8-28bb-4e07-8931-4747bac9825d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * This parent task selects one of it's &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * children to update.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * To select a child, it starts from the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * beginning of it's children vector&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * and goes one by one until it finds one &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * that passes the CheckCondition test.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * It then updates that child until its &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * finished.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * If the child finishes with failure, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * it continues down the list looking another&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * candidate to update, and if it doesn't &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * find it, it finishes with failure. &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * If the child finishes with success, the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Selector considers it's task done and &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * bails with success. &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; Selector extends ParentTask &lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Creates a new instance of &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * the Selector class&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * @param blackboard Reference to &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; *  the AI Blackboard data&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Selector(Blackboard bb)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        super(bb);        &lt;/li&gt; &lt;li&gt;    }    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Chooses the new task to update.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * @return The new task, or null &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; *  if none was found&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Task ChooseNewTask()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Task task = null;&lt;/li&gt; &lt;li&gt;        boolean found = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; curPos = &lt;/li&gt; &lt;li&gt;            control.subtasks.&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            indexOf(control.curTask);&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;while&lt;/span&gt;(!found)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(curPos == &lt;/li&gt; &lt;li&gt;                (control.subtasks.size() - 1))&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                found = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                task = null;&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;li&gt;            &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            curPos++;&lt;/li&gt; &lt;li&gt;            &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            task = control.subtasks.&lt;/li&gt; &lt;li&gt;                elementAt(curPos);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(task.CheckConditions())&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                found = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; task;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * In case of child finishing with &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * failure we find a new one to update,&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * or fail if none is to be found&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    @Override&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ChildFailed() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        control.curTask = ChooseNewTask();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(control.curTask == null)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            control.FinishWithFailure();&lt;/li&gt; &lt;li&gt;        }        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * In case of child finishing with &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * sucess, our job here is done, finish &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * with sucess&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * as well&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    @Override&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ChildSucceeded() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        control.FinishWithSuccess();        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h2&gt;Task Controller&lt;/h2&gt;  &lt;h3&gt;Introduction&lt;/h3&gt;  &lt;p&gt;The task controller, as mentioned earlier, tracks the state of the Task it is added to. It keeps a reference to the task and acts as a wrapper for said class when dealing with all the “Is it finished? Is it ready to update?” kind of questions.&lt;/p&gt;  &lt;p&gt;This code is separated from the actual task for two reasons.&lt;/p&gt;  &lt;h4&gt;Reason One: Elegance of Design.&lt;/h4&gt;  &lt;p&gt;Once we separate the state control from the task class, we can create new subtasks paying no mind to the whole mechanism going on in the background to keep our task in harmony with it’s parent and children tasks. In the Task classes we just focus on specifying the specific logic for the task at hand.&lt;/p&gt;  &lt;h4&gt;Reason Two: Safety in the Decorators&lt;/h4&gt;  &lt;p&gt;If we take the utility/state logic out of the Task class, we can make all the methods abstract, and when we create the Decorator classes, the compiler checks to see if we have overridden all the abstract functions (if we forget to wrap any of the Task functions in the Decorator, we could introduce subtle but dangerous bugs). If we have the utility functions in the Task class we may or may not remember to make a wrapper for them, and if we have to change the interface of the Task class at some point, we could be in for a hell of pain.&lt;/p&gt;  &lt;h3&gt;Code&lt;/h3&gt;  &lt;p&gt;All said, some code on how the task controller works:&lt;/p&gt;  &lt;h4&gt;TaskController &lt;/h4&gt;  &lt;p&gt;Has the &lt;strong&gt;done&lt;/strong&gt; and &lt;strong&gt;success&lt;/strong&gt; flags, and all the utilities related with them.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:cd7cce92-6526-4722-bfc0-544ad747f807" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 3em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Class added by composition to any task, &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * to keep track of the Task state&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * and logic flow. &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * This state-control class is separated &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * from the Task class so the Decorators&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * have a chance at compile-time security.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; TaskController &lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Indicates whether the task is finished &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * or not&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; boolean done;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * If finished, it indicates if it has &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * finished with success or not&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; boolean sucess;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Indicates if the task has started &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * or not&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; boolean started;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Reference to the task we monitor&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; Task task;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Creates a new instance of the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * TaskController class&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param task Task to controll.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; TaskController(Task task)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        SetTask(task);&lt;/li&gt; &lt;li&gt;        Initialize();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Initializes the class data&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Initialize()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.done = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.sucess = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.started = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Sets the task reference&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param task Task to monitor&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; SetTask(Task task)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.task = task;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Starts the monitored class&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; SafeStart()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.started = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        task.Start();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Ends the monitored task&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; SafeEnd()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.done = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.started = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        task.End();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Ends the monitored class, with success&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; FinishWithSuccess()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.sucess = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.done = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        task.LogTask(&lt;span style="color:#a31515"&gt;"Finished with success"&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Ends the monitored class, with failure&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; FinishWithFailure()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.sucess = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.done = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        task.LogTask(&lt;span style="color:#a31515"&gt;"Finished with failure"&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Indicates whether the task &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * finished successfully&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * @return True if it did, false if it didn't&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; boolean Succeeded() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.sucess;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Indicates whether the task &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * finished with failure&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * @return True if it did, false if it didn't&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; boolean Failed()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; !&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.sucess;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Indicates whether the task finished&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @return True if it did, false if it didn't&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; boolean Finished() &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.done;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Indicates whether the class &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * has started or not&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @return True if it has, false if it hasn't&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; boolean Started()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.started;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Marks the class as just started.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Reset()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.done = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h4&gt;ParentTaskController&lt;/h4&gt;  &lt;p&gt;We add the subtasks vector, and curTask task to the previous responsibilities of the taskController. &lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:73547146-a01c-40e4-bef0-6c587e0e0222" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * This class extends the TaskController &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * class to add support for &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * child tasks and their logic. &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Used together with ParentTask.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; ParentTaskController extends TaskController &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Vector of child Task&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Vector&amp;lt;Task&amp;gt; subtasks;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Current updating task&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Task curTask;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Creates a new instance of the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * ParentTaskController class&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param task&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; ParentTaskController(Task task) &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        super(task);&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.subtasks = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; Vector&amp;lt;Task&amp;gt;();&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.curTask = null;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Adds a new subtask to the end &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * of the subtask list.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * @param task Task to add&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Add(Task task)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        subtasks.add(task);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Resets the task as if it had &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * just started.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Reset()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        super.Reset();&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.curTask = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            subtasks.firstElement();&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h2&gt;Decorator&lt;/h2&gt;  &lt;p&gt;The Decorators are used in the BT to add special functionality to any given task. They act as wrappers of the class, calling the class methods and adding the extra functionality where they deem it necessary. &lt;/p&gt;  &lt;h3&gt;Abstract Decorator Class&lt;/h3&gt;  &lt;p&gt;A Decorator, as explained in the &lt;a href="http://www.oodesign.com/decorator-pattern.html"&gt;Decorator Pattern&lt;/a&gt; has a reference to the class it “decorates” (in our case&lt;em&gt; private Task task&lt;/em&gt;), and also inherits from said class. &lt;/p&gt;  &lt;p&gt;Normally, the Decorator adds extra logic in the DoAction method of the task, which is why the base Decorator class presented only overrides the rest of the methods of Task, for simplicity. None the less, if any Decorator subclass needs to override any other method, it can do so with no problem. &lt;/p&gt;  &lt;p&gt;Here is the code&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2d9ecc49-2ade-423f-aafd-1405ea7c24e8" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Base class for the specific decorators.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * Decorates all the task methods except &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * for the DoAction, for commodity. &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * (Tough any method can be decorated in &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * the base classes with no problem, &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * they are decorated by default so the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * programmer does not forget)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; TaskDecorator extends Task &lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Reference to the task to decorate&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    Task task;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Creates a new instance of the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Decorator class&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param blackboard Reference to &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * the AI Blackboard data&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param task Task to decorate&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; TaskDecorator(Blackboard bb, Task task) &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        super(bb);&lt;/li&gt; &lt;li&gt;        InitTask(task);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Initializes the task reference&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param task Task to decorate&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; InitTask(Task task)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.task = task;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.task.GetControl().SetTask(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Decorate the CheckConditions&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    @Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; boolean CheckConditions() &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.task.CheckConditions();&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Decorate the end&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    @Override&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; End() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.task.End();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Decorate the request for the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Controll reference&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    @Override&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; TaskController GetControl() &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.task.GetControl();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Decorate the start&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    @Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Start() &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.task.Start();&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h3&gt;Examples&lt;/h3&gt;  &lt;p&gt;Here are some specific examples of Decorators.&lt;/p&gt;  &lt;h4&gt;ResetDecorator&lt;/h4&gt;  &lt;p&gt;Simply checks to see if the task it decorates has finished. If it has, it resets the task to “ready to execute again”.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2b457247-8c02-4120-a61d-810663b620bd" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Decorator that resets to "Started" the task &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * it is applied to, each time said task finishes.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; ResetDecorator extends TaskDecorator &lt;/li&gt; &lt;li&gt;{    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Creates a new instance of the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * ResetDecorator class&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * @param blackboard Reference to &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; *  the AI Blackboard data&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * @param task Task to decorate&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; ResetDecorator(Blackboard bb, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Task task) &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        super(bb, task);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Does the decorated task's action, &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * and if it's done, resets it.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    @Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoAction() &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.task.DoAction();&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.task.GetControl().Finished())&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.task.GetControl().Reset();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h4&gt;RegulatorDecorator&lt;/h4&gt;  &lt;p&gt;Updates the task it decorates at a specific speed. This case of Decorator also overrides the Start method. &lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:591c1058-ae45-47d2-b43a-1cda27459b83" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * Decorator that adds a update speed &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; * limit to the task it is applied to&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; * @author Ying&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt; *&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; RegulatorDecorator extends TaskDecorator &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Regulator to keep track of time&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; Regulator regulator;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Update time in seconds per frame&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;float&lt;/span&gt; updateTime;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Creates a new instance of the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * RegulatorDecorator class&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param blackboard Reference to the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; *  AI Blackboard data&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * @param task Task to decorate&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * @param updateTime Time between each &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; *  frame update&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; RegulatorDecorator(Blackboard bb, &lt;/li&gt; &lt;li&gt;        Task task, &lt;span style="color:#0000ff"&gt;float&lt;/span&gt; updateTime) &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        super(bb, task);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.updateTime = updateTime;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * Starts the task and the regulator&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    @Override &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Start()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        task.Start();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.regulator = &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; Regulator(1.0f/updateTime);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * Updates the decorated task only if the &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; * required time since the last update has &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt; * elapsed.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt; */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    @Override&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; DoAction() &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.regulator.IsReady())&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            task.DoAction();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h1&gt;Example&lt;/h1&gt;  &lt;h2&gt;Usage Example&lt;/h2&gt;  &lt;p&gt;With all the code mentioned earlier, it’s a little easy to get lost, so here is a reminder of how this is supposed to work. We are going to create a simple BT with just two basic behaviors, Chase and Flee. (This is taken straight from the game code).&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d9d2c428-6ede-461a-bb65-4144402c8dd8" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt;* Creates the behavior tree and populates &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;* the node hierarchy&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; CreateBehaviourTree()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Create a root node for the tree, that&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// resets itself and updates at 10 fps&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.planner = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; Selector(&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        blackboard, &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#a31515"&gt;"Planner"&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.planner = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; ResetDecorator(&lt;/li&gt; &lt;li&gt;        blackboard, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.planner, &lt;span style="color:#a31515"&gt;"Planner"&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.planner = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; RegulatorDecorator(&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        blackboard, &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.planner, &lt;span style="color:#a31515"&gt;"Planner"&lt;/span&gt;, 0.1f);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Create chase sequence&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    Task chase = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; Sequence(&lt;/li&gt; &lt;li&gt;        blackboard, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#a31515"&gt;"Chase sequence"&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    ((ParentTaskController)chase.GetControl()).&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; GetClosestEnemyCursorTask(&lt;/li&gt; &lt;li&gt;        blackboard, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#a31515"&gt;"GetClosestEnemyCursor"&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;    ((ParentTaskController)chase.GetControl()).&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; SetEnemyCursorAsDestinationTask(&lt;/li&gt; &lt;li&gt;        blackboard, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#a31515"&gt;"SetEnemyCursorAsDestination"&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;    ((ParentTaskController)chase.GetControl()).&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; MoveToDestinationTask(&lt;/li&gt; &lt;li&gt;        blackboard, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#a31515"&gt;"MoveToDestination"&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;    ((ParentTaskController)chase.GetControl()).&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; WaitTillNearDestinationTask(&lt;/li&gt; &lt;li&gt;        blackboard, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#a31515"&gt;"WaitTillNearDestination"&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// Create the flee sequence&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// It's a normal selector but with extra logic&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;// to see if we want to flee or not&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    Task flee = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; Sequence(blackboard, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#a31515"&gt;"Flee sequence"&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    flee = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; FleeDecorator(blackboard, flee, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#a31515"&gt;"Flee sequence"&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    ((ParentTaskController)flee.GetControl()).&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; CalculateFleeDestinationTask(&lt;/li&gt; &lt;li&gt;        blackboard, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#a31515"&gt;"CalculateFleeDestination"&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;    ((ParentTaskController)flee.GetControl()).&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; MoveToDestinationTask(&lt;/li&gt; &lt;li&gt;        blackboard, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#a31515"&gt;"MoveToDestination"&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;    ((ParentTaskController)flee.GetControl()).&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; WaitTillNearDestinationTask(&lt;/li&gt; &lt;li&gt;        blackboard, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#a31515"&gt;"WaitTillNearDestination"&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    ((ParentTaskController)&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.planner.&lt;/li&gt; &lt;li&gt;        GetControl()).Add(flee);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    ((ParentTaskController)&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.planner.&lt;/li&gt; &lt;li&gt;        GetControl()).Add(chase);        &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h2&gt;Diagram&lt;/h2&gt;  &lt;p&gt;The previous code generates the Behavior Tree shown in this diagram.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_vaakdclE4vY/TQfIYaVmvpI/AAAAAAAABJo/0SErS9srhQs/s1600-h/FlowDiagramBT%5B4%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="FlowDiagramBT" border="0" alt="FlowDiagramBT" src="http://lh4.ggpht.com/_vaakdclE4vY/TQfIZVEXueI/AAAAAAAABJs/41gOyssQkSs/FlowDiagramBT_thumb%5B2%5D.png?imgmax=800" width="326" height="549" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h1&gt;Conclusion&lt;/h1&gt;  &lt;p&gt;Behavior trees are a incredibly interesting tool for game AI. They can be applied to many different types of AI and their modularity makes them a blessing when it comes to extending or modifying an existing AI. &lt;/p&gt;  &lt;p&gt;I just hope that with this huge post I have provided the necessary tools for someone who is looking for a nice AI technique for his AI to start working and hopefully avoid the pitfalls I had to survive whilst doing this system.&lt;/p&gt;  &lt;p&gt;Happy coding.&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;PS: The full code for my game Behavior Tree can be found &lt;a href="http://code.google.com/p/daggame/source/browse/#svn/trunk/Code/src/com/game/AI"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-3325642493704880809?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/3325642493704880809/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2010/12/behavior-trees-by-example-ai-in-android.html#comment-form' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/3325642493704880809'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/3325642493704880809'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2010/12/behavior-trees-by-example-ai-in-android.html' title='Behavior Trees by Example. AI in an Android game.'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_vaakdclE4vY/TQfIX3ruauI/AAAAAAAABJk/r5XGZlYh5bY/s72-c/ClassDiagramBT_thumb%5B8%5D.png?imgmax=800' height='72' width='72'/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-1597072110999225145</id><published>2010-10-08T23:30:00.002+02:00</published><updated>2010-10-08T23:47:18.014+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='graphics'/><category scheme='http://www.blogger.com/atom/ns#' term='android'/><title type='text'>3D Picking in Android</title><content type='html'>&lt;h1&gt;Introduction&lt;/h1&gt;  &lt;p&gt;In this short tutorial I’m presenting something that’s made me loose weeks of work. How to implement picking with a perspective camera in the Android platform using OPENGL 1.0. &lt;/p&gt;  &lt;p&gt;The process of picking basically involves the user clicking a point in their device screen, we take that point and apply the inverse transforms that opengl applies to it’s 3D scene, and so get a point in the world coordinate system (wcs) that is where the player wanted to click. For the sake of simplicity, we will work on a simple 2D map, instead of having to cast a ray to intersect multiple objects.&lt;/p&gt;  &lt;p&gt;Usually, in opengl we would use the function glUnProject to un-project the point and so get the wcs equivalent point, but that function is plagued by errors on the Android platform and it’ very difficult to get the gl transformations for the projection and model matrixes. &lt;/p&gt;  &lt;h1&gt;Algorithm&lt;/h1&gt;  &lt;p&gt;So here is my solution. It might not be perfect, but it actually works.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6fcf3e7d-6048-4e0d-9571-d24ec58cc0f5" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* Calculates the transform from screen coordinate &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* system to world coordinate system coordinates &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* for a specific point, given a camera position.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* @param touch Vec2 point of screen touch, the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;  actual position on physical screen (ej: 160, 240)&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* @param cam camera object with x,y,z of the &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;  camera and screenWidth and screenHeight of &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;  the device.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* @return position in WCS.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;   &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Vec2 GetWorldCoords( Vec2 touch, Camera cam)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;   {  &lt;/li&gt; &lt;li&gt;       &lt;span style="color:#008000"&gt;// Initialize auxiliary variables.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       Vec2 worldPos = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; Vec2();&lt;/li&gt; &lt;li&gt;       &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;span style="color:#008000"&gt;// SCREEN height &amp;amp; width (ej: 320 x 480)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;       &lt;span style="color:#0000ff"&gt;float&lt;/span&gt; screenW = cam.GetScreenWidth();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;span style="color:#0000ff"&gt;float&lt;/span&gt; screenH = cam.GetScreenHeight();&lt;/li&gt; &lt;li&gt;              &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;span style="color:#008000"&gt;// Auxiliary matrix and vectors &lt;/span&gt;&lt;/li&gt; &lt;li&gt;       &lt;span style="color:#008000"&gt;// to deal with ogl.&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[] invertedMatrix, transformMatrix, &lt;/li&gt; &lt;li&gt;           normalizedInPoint, outPoint;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       invertedMatrix = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[16];&lt;/li&gt; &lt;li&gt;       transformMatrix = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[16];&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       normalizedInPoint = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[4];&lt;/li&gt; &lt;li&gt;       outPoint = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[4];&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;       &lt;span style="color:#008000"&gt;// Invert y coordinate, as android uses &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;span style="color:#008000"&gt;// top-left, and ogl bottom-left.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;       &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; oglTouchY = (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt;) (screenH - touch.Y());&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;/li&gt; &lt;li&gt;       &lt;span style="color:#008000"&gt;/* Transform the screen point to clip &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;   space in ogl (-1,1) */    &lt;/span&gt;   &lt;/li&gt; &lt;li&gt;       normalizedInPoint[0] = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        (&lt;span style="color:#0000ff"&gt;float&lt;/span&gt;) ((touch.X()) * 2.0f / screenW - 1.0);&lt;/li&gt; &lt;li&gt;       normalizedInPoint[1] = &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        (&lt;span style="color:#0000ff"&gt;float&lt;/span&gt;) ((oglTouchY) * 2.0f / screenH - 1.0);&lt;/li&gt; &lt;li&gt;       normalizedInPoint[2] = - 1.0f;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       normalizedInPoint[3] = 1.0f;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;span style="color:#008000"&gt;/* Obtain the transform matrix and &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;   then the inverse. */&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       Print(&lt;span style="color:#a31515"&gt;&amp;quot;Proj&amp;quot;&lt;/span&gt;, getCurrentProjection(gl));&lt;/li&gt; &lt;li&gt;       Print(&lt;span style="color:#a31515"&gt;&amp;quot;Model&amp;quot;&lt;/span&gt;, getCurrentModelView(gl));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       Matrix.multiplyMM(&lt;/li&gt; &lt;li&gt;           transformMatrix, 0, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;           getCurrentProjection(gl), 0, &lt;/li&gt; &lt;li&gt;           getCurrentModelView(gl), 0);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       Matrix.invertM(invertedMatrix, 0, &lt;/li&gt; &lt;li&gt;           transformMatrix, 0);       &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;       &lt;span style="color:#008000"&gt;/* Apply the inverse to the point &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;   in clip space */&lt;/span&gt;&lt;/li&gt; &lt;li&gt;       Matrix.multiplyMV(&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;           outPoint, 0, &lt;/li&gt; &lt;li&gt;           invertedMatrix, 0, &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;           normalizedInPoint, 0);&lt;/li&gt; &lt;li&gt;       &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (outPoint[3] == 0.0)&lt;/li&gt; &lt;li&gt;       {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;           &lt;span style="color:#008000"&gt;// Avoid /0 error.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;           Log.e(&lt;span style="color:#a31515"&gt;&amp;quot;World coords&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;ERROR!&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;           &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; worldPos;&lt;/li&gt; &lt;li&gt;       }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;/li&gt; &lt;li&gt;       &lt;span style="color:#008000"&gt;// Divide by the 3rd component to find &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;span style="color:#008000"&gt;// out the real position.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;       worldPos.Set(&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;           outPoint[0] / outPoint[3], &lt;/li&gt; &lt;li&gt;           outPoint[1] / outPoint[3]);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;         &lt;/li&gt; &lt;li&gt;       &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; worldPos;       &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;   }&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;In my case, I’ve got a render, a logic and an application thread, this function is a service provided by the render thread, because it needs the gl Projection and ModelView matrixes. &lt;/p&gt;  &lt;p&gt;What happens is the logic thread sends a touch (x,y) position, and the current camera (x,y,z, screenH, screenW), to the GetWorldCoords function, and expects the world position of that point taking into accound the camera position (x,y,z), and the view fustrum (represented by the projection and modelview matrixes).&lt;/p&gt;  &lt;p&gt;The first lines get the data ready, create auxiliary matrixes and access camera data.&lt;/p&gt;  &lt;p&gt;One important point is the line&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;int oglTouchY = (int) (screenH - touch.Y());&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This inversion is needed because android screen coordinates assume a top-left coordinate system, and opengl needs a bottom left. So we change it. And with that we can start doing the picking algorithm.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Transform the point from screen coordinates (ej: 120, 330) to clip space (for a 320 x 480 android, this would be –0.25, 0.375)&lt;/li&gt;    &lt;li&gt;Get the transformation matrix (projection * modelView), and invert it.&lt;/li&gt;    &lt;li&gt;Multiply the clip-space point times the inverse transformation.&lt;/li&gt;    &lt;li&gt;Divide the coordinates x,y,z (positions 0,1,2) times the w (position 3) &lt;/li&gt;    &lt;li&gt;You’ve got the world coordinates.&lt;/li&gt; &lt;/ol&gt;  &lt;h1&gt;Notes:&lt;/h1&gt;  &lt;p&gt;The z doesn’t appear because I don’t have need for it, but you can get it easily (outPoint[2] / outPoint[3]).&lt;/p&gt;  &lt;p&gt;The situation I’m working on is the following. The red and blue are the frustum limits, the green is the world map, at an arbitrary point in space, and the camera is at the tip of the view frustum.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_vaakdclE4vY/TK-NcoQYvBI/AAAAAAAAA4o/5kN0cSCcgc4/s1600-h/80880607%5B3%5D.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="80880607" border="0" alt="80880607" src="http://lh5.ggpht.com/_vaakdclE4vY/TK-NdAuVVUI/AAAAAAAAA4s/GzkC4Zh4bh8/80880607_thumb%5B1%5D.jpg?imgmax=800" width="244" height="230" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;There is one very special complication when doing this picking algorithm in the android platform and that is accessing the projection and model view matrixes opengl uses. We manage with the following code.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ca1a6bbe-7c0f-494f-83d4-8c92d8426876" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* Record the current modelView matrix &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* state. Has the side effect of&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* setting the current matrix state &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* to GL_MODELVIEW&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* @param gl context&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;   &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[] getCurrentModelView(GL10 gl) &lt;/li&gt; &lt;li&gt;   {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[] mModelView = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[16];&lt;/li&gt; &lt;li&gt;        getMatrix(gl, GL10.GL_MODELVIEW, mModelView);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; mModelView;&lt;/li&gt; &lt;li&gt;   }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;   &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* Record the current projection matrix &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* state. Has the side effect of&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* setting the current matrix state &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* to GL_PROJECTION&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* @param gl context&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;   &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[] getCurrentProjection(GL10 gl) &lt;/li&gt; &lt;li&gt;   {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[] mProjection = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[16];&lt;/li&gt; &lt;li&gt;       getMatrix(gl, GL10.GL_PROJECTION, mProjection);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; mProjection;&lt;/li&gt; &lt;li&gt;   }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;   &lt;span style="color:#008000"&gt;/**&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* Fetches a specific matrix from opengl&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* @param gl context&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* @param mode of the matrix&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;* @param mat initialized float[16] array &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;* to fill with the matrix&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;   &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; getMatrix(GL10 gl, &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; mode, &lt;span style="color:#0000ff"&gt;float&lt;/span&gt;[] mat) &lt;/li&gt; &lt;li&gt;   {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       MatrixTrackingGL gl2 = (MatrixTrackingGL) gl;&lt;/li&gt; &lt;li&gt;       gl2.glMatrixMode(mode);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       gl2.getMatrix(mat, 0);&lt;/li&gt; &lt;li&gt;   }&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The gl parameter passed to the getCurrent*(GL10 gl) functions is stored as a member variable in the class.&lt;/p&gt;  &lt;p&gt;The MatrixTrackingGL class is part of the android samples, and can be found &lt;a href="http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/OpenGLES/SpriteText/src/com/google/android/opengles/spritetext/MatrixTrackingGL.java"&gt;here&lt;/a&gt;. Some other classes must be included for it to work (mainly &lt;a href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/spritetext/MatrixStack.html"&gt;MatrixStack&lt;/a&gt;). The MatrixTrackingGL class acts as a wrapper for the gl context, but providing the data we need. For it to work, our custom GLSurfaceView class must have the GLWrapper call, something like this.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f0607beb-b2ec-4957-88f0-a791ed882314" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; DagGLSurfaceView(Context context) &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    super(context);       &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;/li&gt; &lt;li&gt;    setFocusable(&lt;span style="color:#0000ff"&gt;true&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Wrapper set so the renderer can &lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;//access the gl transformation matrixes.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    setGLWrapper(&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; GLSurfaceView.GLWrapper() &lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        @Override&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; GL wrap(GL gl) &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        { &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; MatrixTrackingGL(gl); &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    });  &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;/li&gt; &lt;li&gt;    mRenderer = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; DagRenderer();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    setRenderer(mRenderer);&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;(Where DagRender is my GLSurfaceView.Renderer, and DagGLSurfaceView is my GLSurfaceView)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-1597072110999225145?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/1597072110999225145/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2010/10/3d-picking-in-android.html#comment-form' title='16 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/1597072110999225145'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/1597072110999225145'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2010/10/3d-picking-in-android.html' title='3D Picking in Android'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_vaakdclE4vY/TK-NdAuVVUI/AAAAAAAAA4s/GzkC4Zh4bh8/s72-c/80880607_thumb%5B1%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>16</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-2561700954578488790</id><published>2010-01-30T22:05:00.001+01:00</published><updated>2010-01-30T22:05:09.041+01:00</updated><title type='text'>Holy Hole</title><content type='html'>&lt;p&gt;No. This is not the build up to a perverted pun. That said, let’s get on with the game!&lt;/p&gt;  &lt;p&gt;Holy Hole was our entry for the 2009 campus party quick-compo, a coding contest with just 3 day’s time from concept to finish. It came in 4th, not bad, but not great either.&lt;/p&gt;  &lt;p&gt;The game play is basic, you can only let the balls that match the criteria (color, shape, …) into the hole. Clicking at them sends them flying. The more balls of the correct criteria that get in, the more points you get. There are also different power-ups available.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_vaakdclE4vY/S2SesRTSx0I/AAAAAAAAAp0/X0VMbmb9b3U/s1600-h/Menu%5B3%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Menu" border="0" alt="Menu" src="http://lh4.ggpht.com/_vaakdclE4vY/S2SetMDvEKI/AAAAAAAAAp4/LDin1vRLhUc/Menu_thumb%5B1%5D.png?imgmax=800" width="244" height="190" /&gt;&lt;/a&gt; &lt;a href="http://lh6.ggpht.com/_vaakdclE4vY/S2Se0H8NK2I/AAAAAAAAAp8/5n_qlGcx960/s1600-h/Pause%5B3%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Pause" border="0" alt="Pause" src="http://lh3.ggpht.com/_vaakdclE4vY/S2Se58-nqVI/AAAAAAAAAqA/ZhbA23eNPQc/Pause_thumb%5B1%5D.png?imgmax=800" width="244" height="189" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Let’s get technical then. The game is completely coded in C# and XNA (which we learnt the day before the competition started, just for that purpose) and a bit of XML for the high score. There was no up-front design here (no time!), I did it with 2 people which I trust and have worked a lot with so communication was just sort of telepathy. &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_vaakdclE4vY/S2Se-nTYe4I/AAAAAAAAAqE/GPfZEYk6Ou0/s1600-h/Play%5B3%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="Play" border="0" alt="Play" src="http://lh6.ggpht.com/_vaakdclE4vY/S2Se_yU2dHI/AAAAAAAAAqM/YwRUzkcykXA/Play_thumb%5B1%5D.png?imgmax=800" width="244" height="191" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt; So, some specifics, here is the class diagram generated by visual studio.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_vaakdclE4vY/S2SfApJ7A7I/AAAAAAAAAqQ/0xzZyJwADK4/s1600-h/Diagram%5B8%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px auto; display: block; float: none; border-top: 0px; border-right: 0px" title="Diagram" border="0" alt="Diagram" src="http://lh5.ggpht.com/_vaakdclE4vY/S2SfAw5lKdI/AAAAAAAAAqU/n5-xvFAAOT0/Diagram_thumb%5B6%5D.png?imgmax=800" width="244" height="177" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Main Systems:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Ball Manager and Balls&lt;/li&gt;    &lt;li&gt;Scene Manager and Scenes&lt;/li&gt;    &lt;li&gt;Combo Manager and Combos&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Then the XML team:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;High Score&lt;/li&gt;    &lt;li&gt;Load/Save Data&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;And finally some nice graphics:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Spotlight &lt;/li&gt;    &lt;li&gt;Particle Emitter and Simple Particle&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So that’s another game done. The team this time was Maria (Chuel) Cabezuelo, Victor (CyanCrey) Villanueva and me, Jaime (Ying) Barrachina. Music credit goes to Pablo (Undi) De la Ossa.&lt;/p&gt;  &lt;p&gt;You can find the SVN &lt;a href="http://subversion.assembla.com/svn/Quickcompo2009"&gt;here&lt;/a&gt; and a direct download &lt;a href="http://code.assembla.com/Quickcompo2009/subversion/nodes/HolyHole.rar?_format=raw&amp;amp;rev=91"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-2561700954578488790?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/2561700954578488790/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2010/01/holy-hole.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2561700954578488790'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2561700954578488790'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2010/01/holy-hole.html' title='Holy Hole'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_vaakdclE4vY/S2SetMDvEKI/AAAAAAAAAp4/LDin1vRLhUc/s72-c/Menu_thumb%5B1%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-8945274305862187069</id><published>2010-01-19T08:44:00.002+01:00</published><updated>2010-01-19T08:45:55.778+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Completed'/><category scheme='http://www.blogger.com/atom/ns#' term='Caged'/><title type='text'>Caged</title><content type='html'>&lt;p&gt;Caged is a RPG game written by me and my team* in our first year (2007) at the university. The game development was difficult as hell, and fun as hell as well. I learnt to design the architecture and lead a team, while teaching the team to code. One of the most rewarding experiences in my life.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_vaakdclE4vY/S1VijEvUjLI/AAAAAAAAApQ/wpPmN6_ewsU/s1600-h/CagedMenu%5B2%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="CagedMenu" border="0" alt="CagedMenu" src="http://lh5.ggpht.com/_vaakdclE4vY/S1VilAmvjfI/AAAAAAAAApU/nZrH2alGsjU/CagedMenu_thumb.png?imgmax=800" width="244" height="184" /&gt;&lt;/a&gt; &lt;a href="http://lh6.ggpht.com/_vaakdclE4vY/S1VirZZDJUI/AAAAAAAAApY/betK56DuB68/s1600-h/CagedGame%5B2%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="CagedGame" border="0" alt="CagedGame" src="http://lh6.ggpht.com/_vaakdclE4vY/S1Vit2N9uxI/AAAAAAAAApc/H-PGzdBFoiw/CagedGame_thumb.png?imgmax=800" width="244" height="184" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The game is written C++, using SDL and TinyXML as only support libraries. We worked on the Dev-C++ IDE and SVN. It has 20+ game levels.&lt;/p&gt;  &lt;p&gt;Features&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;A scripting engine which feeds cut-scenes and dialogues from XML&lt;/li&gt;    &lt;li&gt;A sound engine based of SDL_mixer&lt;/li&gt;    &lt;li&gt;AI for the bots, with path finding, decision making and message system.&lt;/li&gt;    &lt;li&gt;Core, with entity manager, event system, clocks and timers, log system, resource manager, FSM…&lt;/li&gt;    &lt;li&gt;Physics with collision detection and some neat force driven steering behaviors.&lt;/li&gt;    &lt;li&gt;Graphics. SDL isn't meant to be used as a Gfx engine, so the camera,&amp;#160; the GUI, and so on was also fun to make.&lt;/li&gt;    &lt;li&gt;Integrated game editor, to make all the xml maps for the game.&lt;/li&gt;    &lt;li&gt;Life-saver debug mode xD&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_vaakdclE4vY/S1Vi0c7N0yI/AAAAAAAAApg/NMeorFAMEIo/s1600-h/CagedEditro%5B2%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="CagedEditro" border="0" alt="CagedEditro" src="http://lh6.ggpht.com/_vaakdclE4vY/S1Vi2IJcKHI/AAAAAAAAApk/Ohfy4m1rn4c/CagedEditro_thumb.png?imgmax=800" width="244" height="184" /&gt;&lt;/a&gt; &lt;a href="http://lh5.ggpht.com/_vaakdclE4vY/S1Vi6_dsX7I/AAAAAAAAApo/0VlyxYiun3I/s1600-h/CagedDebug%5B2%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="CagedDebug" border="0" alt="CagedDebug" src="http://lh4.ggpht.com/_vaakdclE4vY/S1Vi8chRFGI/AAAAAAAAAps/UAg4yR4fYec/CagedDebug_thumb.png?imgmax=800" width="244" height="191" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In the end, one year and 200k lines of code later, the game was completed. It won 2nd place in two different contests, “&lt;a href="http://tutambienpuedes.net/"&gt;tu tambien puedes&lt;/a&gt;” and “&lt;a href="http://videojuegos.webs.upv.es/Concurso.html"&gt;CDV 2008&lt;/a&gt;”.&lt;/p&gt;  &lt;p&gt;Just wanted to share my first game with the world :) Game SVN &lt;a href="http://svn.assembla.com/svn/caged11"&gt;here&lt;/a&gt; and direct download &lt;a href="http://svn.assembla.com/svn/caged11/CagedDirectDownload.rar"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;h2&gt;Team*&lt;/h2&gt;  &lt;h5&gt;Programming&lt;/h5&gt;  &lt;ul&gt;   &lt;li&gt;Jaime (Ying) Barrachina&lt;/li&gt;    &lt;li&gt;Victor (CyanCrey) Villanueva&lt;/li&gt;    &lt;li&gt;Maria (Chuel) Cabezuelo&lt;/li&gt;    &lt;li&gt;Catalin (K_ta) Costin&lt;/li&gt;    &lt;li&gt;David (Coper) Garcia&lt;/li&gt; &lt;/ul&gt;  &lt;h5&gt;Art&lt;/h5&gt;  &lt;p&gt;(You know who you are ;) )&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Jusep&lt;/li&gt;    &lt;li&gt;Alberto&lt;/li&gt;    &lt;li&gt;Kiwi&lt;/li&gt;    &lt;li&gt;Yuna (Script writer)&lt;/li&gt; &lt;/ul&gt;  &lt;h5&gt;Special thanks&lt;/h5&gt;  &lt;ul&gt;   &lt;li&gt;Javier (NeoM) Belenguer (Our life saver, really)&lt;/li&gt;    &lt;li&gt;Wynter (Zerotri) Woods&lt;/li&gt; &lt;/ul&gt;  &lt;div style="padding-bottom: 0px; padding-left: 0px; width: 400px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:7805143e-ce43-4907-a6f2-eb43b92a7a18" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;object width="400" height="300"&gt;&lt;param name="allowfullscreen" value="true" /&gt;&lt;param name="allowscriptaccess" value="always" /&gt;&lt;param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=2178281&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=&amp;amp;fullscreen=1" /&gt;&lt;embed src="http://vimeo.com/moogaloop.swf?clip_id=2178281&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=&amp;amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="300"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;p&gt;&lt;a href="http://vimeo.com/2178281"&gt;Caged&lt;/a&gt; from &lt;a href="http://vimeo.com/user911476"&gt;CyanCrey&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-8945274305862187069?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/8945274305862187069/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2010/01/caged.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/8945274305862187069'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/8945274305862187069'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2010/01/caged.html' title='Caged'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_vaakdclE4vY/S1VilAmvjfI/AAAAAAAAApU/nZrH2alGsjU/s72-c/CagedMenu_thumb.png?imgmax=800' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-5881804082504181594</id><published>2010-01-18T15:45:00.002+01:00</published><updated>2010-01-18T15:50:10.867+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Meanies'/><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='Architecture'/><title type='text'>Meanies</title><content type='html'>&lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;What's this game about?&lt;/strong&gt; &lt;/p&gt;    &lt;p&gt;You control a evil mastermind that's bent on taking over the world! Or at least his city. &lt;/p&gt;    &lt;p&gt;To accomplish that, he will find and recruit as many minions as he can, and send them or lead them on missions, for the good of mankind. And of course, himself.&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Where is it set?&lt;/strong&gt; &lt;/p&gt;    &lt;p&gt;This game is set in a small city in our age and time. You will move all over it, it's almost completely visitable, and under it, as your base is in the sewers.&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;What do you control?&lt;/strong&gt; &lt;/p&gt;    &lt;p&gt;You control small, big headed minions, each with his unique personality and stats.&lt;/p&gt;    &lt;p&gt;     &lt;br /&gt;&lt;strong&gt;How many do you control?&lt;/strong&gt; &lt;/p&gt;    &lt;p&gt;At any given moment, you can control up to 8 minions, tough you will usually be controlling your avatar, the evil mastermind.&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;What's the main objective?&lt;/strong&gt; &lt;/p&gt;    &lt;p&gt;Conquer the world!&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This is one of the games I’m currently developing. I’m not going to say anymore about the general game design, as it’s an idea I hope to use for personal stuff, and because this blog is about the technical side.&lt;/p&gt;  &lt;p&gt;So, let’s get technical then. The game is written in C# and XNA. It might or might not be the definite language for the game, as right now I’m using the whole thing as a prototype. The game is for windows, I’ll worry about porting and the like when I’ve actually got something.&lt;/p&gt;  &lt;p&gt;Hum, the architecture then.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_vaakdclE4vY/S1Rz8_X51DI/AAAAAAAAApA/TgyCwWzjuNo/s1600-h/Main%5B1%5D.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="Main" border="0" alt="Main" src="http://lh6.ggpht.com/_vaakdclE4vY/S1Rz9acYfsI/AAAAAAAAApE/_4otcymgYsE/Main_thumb%5B1%5D.jpg?imgmax=800" width="244" height="179" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Ignore the green boxes. Right now I’ve got, starting from Game, the branches that end at: EntityManager, MapGrid and Modes done, including the Entities (as far as collision and animations are concerned), and all that hangs from MapGrid.&lt;/p&gt;  &lt;p&gt;A quick overview of the stuff so far, and I’ll update as I code along.&lt;/p&gt;  &lt;h3&gt;SceneManager&lt;/h3&gt;  &lt;p&gt;A state machine that changes Scenes. It has only one scene active at any given time, and Scenes are discarded, not saved, when a new one is selected.&lt;/p&gt;  &lt;h3&gt;Scene&lt;/h3&gt;  &lt;p&gt;States the game can be in. Structural purpose. &lt;/p&gt;  &lt;h3&gt;PlayScene&lt;/h3&gt;  &lt;p&gt;The main scene of the game, &lt;em&gt;it provides content, NOT logic&lt;/em&gt;. This might be a bit hard to understand, but basically, once playing, we need a second state machine to keep track of &lt;em&gt;how&lt;/em&gt; we play. More often than not, from one play mode we don’t change the content&amp;#160; but instead the changes are more subtle, and while we don’t want to just cram all the possible options for every situation in the PlayScene, it doesn’t make sense to swap to a new Scene and reload all assets, logic or whatever.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;For example, in the game I could have the fighting, normal and dialog modes. In all of them the same graphics are still loaded, the player still is on screen, etc… The changes here are more along the lines of player control, dialogs shown, Bot decision paths…&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;That’s why I introduce…&lt;/p&gt;  &lt;h3&gt;Mode&lt;/h3&gt;  &lt;p&gt;Mode is the thread of your process, so to speak. PlayScene is always in a Mode, and modes can be swapped in and out easily. Modes dictate the logic the resources of the PlayScene must follow. They hold all the logic of PlayScene, and are used as different “logic paths” for the game components to follow.&lt;/p&gt;  &lt;p&gt;And logically, Modes don’t have any “content” themselves, apart of the needed to run their own logic. You could say they are the cartridges in your gameboy, and call the shots in all the components of PlayScene.&lt;/p&gt;  &lt;h3&gt;CityMap&lt;/h3&gt;  &lt;p&gt;It’s a collection of zones, as well as an actual map where to choose a location. Only basic functionality to support one scene is implemented right now.&lt;/p&gt;  &lt;h3&gt;Zone&lt;/h3&gt;  &lt;p&gt;It’s a place with a map, scenario items and entities, so the class has the necessary tools to handle them.&lt;/p&gt;  &lt;h2&gt;MapGrid&lt;/h2&gt;  &lt;p&gt;Holds the tiles, the scenario items and a tile-collision map generated from the load files of terrain and scenarios.&lt;/p&gt;  &lt;p&gt;And… that’s all for now. Entity deserves his own post, and the rest isn’t really developed yet. I’m still deciding what to code next… maybe some input, or perhaps some basic path finding and random movement.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-5881804082504181594?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/5881804082504181594/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2010/01/meanies.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/5881804082504181594'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/5881804082504181594'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2010/01/meanies.html' title='Meanies'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_vaakdclE4vY/S1Rz9acYfsI/AAAAAAAAApE/_4otcymgYsE/s72-c/Main_thumb%5B1%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-2750586755704557393</id><published>2009-06-24T19:11:00.015+02:00</published><updated>2010-01-13T15:12:59.935+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AI'/><category scheme='http://www.blogger.com/atom/ns#' term='Planner'/><title type='text'>AI - Planner Approach</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_vaakdclE4vY/SkNBrqszUgI/AAAAAAAAAVM/Fs-cU10R11c/s1600-h/xml.jpg"&gt;&lt;/a&gt;  &lt;div style="text-align: center"&gt;   &lt;br /&gt;&lt;/div&gt; There are may ways of coding the AI for a game, each game definitely needs it's custom made AI but that doesn't mean that there aren't certain patterns to apply for similar AI in different games.  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;The technique I'm about to expose is primarily focused for strategy games of a reduced scope, but works wonders in them. It's basically a Planner with 3 distinct levels to control the global AI of the game (NOT the specific units).&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;The beauty of the system is that it's simple, elegant and robust. Simple because each module is simple. Elegant because the interaction of the simple elements makes a complex behavior, and robust because adding or modifying the game design at any level has no impact whatsoever on the general AI architecture, only in the corresponding module.&lt;/div&gt;  &lt;div style="text-align: center"&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;h2&gt;Overview:&lt;/h2&gt;  &lt;div&gt;The technique consists of 3 levels. &lt;/div&gt;  &lt;div&gt;First there is the data generators, that calculate all the data the planner needs. Then there is the actual planner, who calculates the probability of executing each action and calls them. Finally there are the individual actions, that produce different results based on the actual game situation.&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;&lt;img style="text-align: center; margin: 0px auto 10px; width: 400px; display: block; height: 313px; cursor: hand" id="BLOGGER_PHOTO_ID_5350949197774700754" border="0" alt="" src="http://3.bp.blogspot.com/_vaakdclE4vY/SkJj8eCFwNI/AAAAAAAAAUc/PrOvGhR_wOU/s400/GeneralPlanner.jpg" /&gt;&lt;/div&gt;  &lt;div style="text-align: center"&gt;&lt;span style="font-size: small" class="Apple-style-span"&gt;The above diagram shows an example of the Planner strategy. &lt;/span&gt;&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;A PlayerAI class has references to external data (player and map), the blue methods are data generators, the red object is the planner, and the yellow objects are actions.&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;Data generators: &lt;/div&gt;  &lt;div&gt;These are usually part of the general AI class, and game specific (you won't change throughout the game because they provide general info of the game the AI needs, regardless of the actual situation).&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;blockquote&gt;   &lt;p&gt;In the example: &lt;/p&gt;    &lt;p&gt;They provide two datum. The % of the battle that is won (to know if the AI is wining and must press the advantage being aggressive, or is losing and must quickly rally to defend) which it calculates from the number of units, defenses and overall power of both contestants. The budget the AI can spend in that turn, cause in the beginning of the game it can spend freely to create cheap units, but later on it needs to save up to create more powerful ones.&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Planners:&lt;/h2&gt;  &lt;div&gt;The planner is a object the AI has. A base Planner class exists to derive different planners from, and could look something like this:&lt;/div&gt;  &lt;div&gt;&lt;span style="color: rgb(0,0,238); -webkit-text-decorations-in-effect: underline" class="Apple-style-span"&gt;     &lt;br /&gt;&lt;img style="text-align: center; margin: 0px auto 10px; width: 260px; display: block; height: 180px; cursor: pointer" id="BLOGGER_PHOTO_ID_5350952982603315298" border="0" alt="" src="http://3.bp.blogspot.com/_vaakdclE4vY/SkJnYxnjOGI/AAAAAAAAAUs/zbtNDe2OpqM/s400/AIPlanner.jpg" /&gt;&lt;/span&gt;&lt;/div&gt;  &lt;div&gt;It's the planner's job to decide what to do, and here is how it goes about it. &lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;h2&gt;Creation:&lt;/h2&gt;  &lt;div&gt;For each kind of scenario we have a different planner, specified in the external game data files. When we load a PlayerAI data file, we create a specific Planner object specified in that file.&lt;/div&gt;  &lt;blockquote&gt;   &lt;p&gt;In the example:     &lt;br /&gt;We have a PlayerAI xml file with this structure: &lt;span style="color: #99ff99" class="Apple-style-span"&gt;       &lt;br /&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;div&gt;&lt;span style="color: #99ff99" class="Apple-style-span"&gt;&lt;span style="color: rgb(0,0,0)" class="Apple-style-span"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_vaakdclE4vY/SkNBrqszUgI/AAAAAAAAAVM/Fs-cU10R11c/s1600-h/xml.jpg"&gt;&lt;img style="text-align: center; margin: 0px auto 10px; width: 379px; display: block; height: 258px; cursor: pointer" id="BLOGGER_PHOTO_ID_5351193000698663426" border="0" alt="" src="http://3.bp.blogspot.com/_vaakdclE4vY/SkNBrqszUgI/AAAAAAAAAVM/Fs-cU10R11c/s400/xml.jpg" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;  &lt;div&gt;   &lt;div&gt;     &lt;br /&gt;&lt;/div&gt;    &lt;blockquote&gt;     &lt;div&gt;So we would create for our playerAI a new Planner of type &amp;quot;somePlanner&amp;quot;.&lt;/div&gt;   &lt;/blockquote&gt;    &lt;div&gt;Then for each Action assigned to the Planner in the data file, we create a new Action object of type &amp;quot;T&amp;quot; and add it to the planner's action vector, with it's corresponding base percentage, which represents the probability of choosing that particular action.&lt;/div&gt;    &lt;blockquote&gt;     &lt;p&gt;In the example:       &lt;br /&gt;We would create a Order, CastSpell, CreateUnit, CreateBuilding and Wait actions and add them to m_vActions.&lt;/p&gt;   &lt;/blockquote&gt;    &lt;h2&gt;Execution:&lt;/h2&gt;    &lt;div&gt;Each time the AI updates, we update the Planner passing it the data provided by the data generators. Taking those into account, it calculates the new % of choosing each action, from their base %.&lt;/div&gt;    &lt;div&gt;     &lt;br /&gt;&lt;/div&gt;    &lt;div&gt;Now, knowing what the chances are of executing each action, it chooses those most probable ones and executes them.&lt;/div&gt;    &lt;div&gt;&amp;#160;&lt;/div&gt;    &lt;blockquote&gt;     &lt;p&gt;In the example:       &lt;br /&gt;We would get the BattleStatus and Budget, and for each of the 5 actions, update their probability of being executed according to how much money we have and wherever we are winning the battle or not.&lt;/p&gt;   &lt;/blockquote&gt;    &lt;div&gt;&amp;#160;&lt;/div&gt;    &lt;div&gt;     &lt;br /&gt;&lt;/div&gt;    &lt;h2&gt;Actions:&lt;/h2&gt;    &lt;div&gt;All the actions are objects that take the same parameters, and provide some sort of output to the rest of the game system.&lt;/div&gt;    &lt;div&gt;     &lt;div&gt;&lt;span style="color: rgb(0,0,238); -webkit-text-decorations-in-effect: underline" class="Apple-style-span"&gt;&lt;img style="text-align: center; margin: 0px auto 10px; width: 260px; display: block; height: 150px; cursor: pointer" id="BLOGGER_PHOTO_ID_5351188404186175570" border="0" alt="" src="http://2.bp.blogspot.com/_vaakdclE4vY/SkM9gHW02FI/AAAAAAAAAU8/DB5aoFTQHTQ/s400/AIAction.jpg" /&gt;&lt;/span&gt;&lt;/div&gt;   &lt;/div&gt;    &lt;div&gt;They are modular, so you can add new actions, remove actions (changing the xml file of that planner) or modify particular actions (changing the code of that particular action class) and the rest of the system never needs to know.&lt;/div&gt;    &lt;div&gt;&amp;#160;&lt;/div&gt;    &lt;blockquote&gt;     &lt;p&gt;In the example:       &lt;br /&gt;The Order action tells units to attack, stop or retreat, depending on the BattleStatus. The Create Units action chooses one unit or another depending on the need for offensive or defensive units. ... Finally the Wait action puts the AI on idle, so it doesn't do anything.&lt;/p&gt;   &lt;/blockquote&gt;    &lt;h2&gt;Notes:&lt;/h2&gt;    &lt;div&gt;The Planners and actions are implemented by a base Planner (or Action) class and derived classes for each specific Planner (or action) and using those by polymorphism.&lt;/div&gt;    &lt;div&gt;     &lt;br /&gt;&lt;/div&gt;    &lt;div&gt;A Player AI class might look something like this:&lt;/div&gt;    &lt;div&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" style="text-decoration: none" href="http://2.bp.blogspot.com/_vaakdclE4vY/SkM_EjPCh3I/AAAAAAAAAVE/OuNuFF3RjXk/s1600-h/PlayerAI.jpg"&gt;&lt;img style="text-align: center; margin: 0px auto 10px; width: 330px; display: block; height: 240px; cursor: pointer" id="BLOGGER_PHOTO_ID_5351190129656629106" border="0" alt="" src="http://2.bp.blogspot.com/_vaakdclE4vY/SkM_EjPCh3I/AAAAAAAAAVE/OuNuFF3RjXk/s400/PlayerAI.jpg" /&gt;&lt;/a&gt;&lt;/div&gt; &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-2750586755704557393?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/2750586755704557393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2009/06/scroll-of-ai-planner-aproach.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2750586755704557393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/2750586755704557393'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2009/06/scroll-of-ai-planner-aproach.html' title='AI - Planner Approach'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_vaakdclE4vY/SkJj8eCFwNI/AAAAAAAAAUc/PrOvGhR_wOU/s72-c/GeneralPlanner.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-4506778342451946060</id><published>2009-06-12T23:45:00.005+02:00</published><updated>2010-01-13T15:19:13.813+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Design'/><title type='text'>Engine vs. Game</title><content type='html'>&lt;div&gt;When immersed in amateur game programming, there's 2 ways to go. The game of the game-making, or getting skill points on engine programming. &lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;There's plenty of strong opinions of what's better and what's not, but the truth is they both have their strong points and appeals for the beginner programmer, this must be so, or else natural selection would have claimed one of these two ways long ago. &lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;&lt;b&gt;As for what I think... &lt;/b&gt;&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;Before going in depth on either of them, I'll do a fast forward and give the definite answer to life, the universe, and everything: &lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div style="text-align: center"&gt;&lt;i&gt;Whatever you have most fun making. &lt;/i&gt;&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;That's one of the few pros of being an amateur, you can actually code just for fun, whatever goes your way. Putting that aside, lets have a look at some real arguments:&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;&lt;span style="font-size: large" class="Apple-style-span"&gt;&lt;b&gt;Engine:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;&lt;b&gt;Pros:&lt;/b&gt;&lt;/div&gt;  &lt;div&gt;   &lt;ul&gt;     &lt;li&gt;&lt;span style="text-decoration: underline" class="Apple-style-span"&gt;You learn&lt;/span&gt;, not more (quantity of &lt;span id="SPELLING_ERROR_0" class="blsp-spelling-corrected"&gt;knowledge&lt;/span&gt; is in direct correlation to, basically, quantity of work, &lt;span id="SPELLING_ERROR_1" class="blsp-spelling-corrected"&gt;wherever&lt;/span&gt; making engines or games), but more basic. You go down a deeper level and fight with elemental bugs. As with everything in life, the more you work on the basics, the more skill you'll achieve in the long run. &lt;/li&gt;      &lt;li&gt;You get a head's up on&lt;span style="text-decoration: underline" class="Apple-style-span"&gt; engine design and architecture&lt;/span&gt;, &lt;span id="SPELLING_ERROR_2" class="blsp-spelling-corrected"&gt;which&lt;/span&gt; will make using any 3rd party engine much easier, cause after figuring out the logical way to design that entity class, you won't be surprised when you find a &lt;span id="SPELLING_ERROR_3" class="blsp-spelling-error"&gt;&lt;span id="SPELLING_ERROR_0" class="blsp-spelling-error"&gt;physx&lt;/span&gt;&lt;/span&gt;::Actor and see some of the physical methods you did for your engine. &lt;/li&gt;      &lt;li&gt;As you probably won't be writing the whole engine from the most basic level (&lt;span id="SPELLING_ERROR_4" class="blsp-spelling-error"&gt;&lt;span id="SPELLING_ERROR_1" class="blsp-spelling-error"&gt;bliting&lt;/span&gt;&lt;/span&gt; pixels to screen using hardware functionality for example, is too basic level) you'll&lt;span style="text-decoration: underline" class="Apple-style-span"&gt; get the hang of a lot of &lt;span id="SPELLING_ERROR_5" class="blsp-spelling-error"&gt;&lt;span id="SPELLING_ERROR_2" class="blsp-spelling-error"&gt;auxiliar&lt;/span&gt;&lt;/span&gt; libraries&lt;/span&gt;. Maybe &lt;span id="SPELLING_ERROR_6" class="blsp-spelling-error"&gt;&lt;span id="SPELLING_ERROR_3" class="blsp-spelling-error"&gt;opengl&lt;/span&gt;&lt;/span&gt; for &lt;span id="SPELLING_ERROR_7" class="blsp-spelling-error"&gt;&lt;span id="SPELLING_ERROR_4" class="blsp-spelling-error"&gt;gfx&lt;/span&gt;&lt;/span&gt;, &lt;span id="SPELLING_ERROR_8" class="blsp-spelling-error"&gt;&lt;span id="SPELLING_ERROR_5" class="blsp-spelling-error"&gt;tinyXML&lt;/span&gt;&lt;/span&gt; for those weapon data files you wanted, or &lt;span id="SPELLING_ERROR_9" class="blsp-spelling-error"&gt;&lt;span id="SPELLING_ERROR_6" class="blsp-spelling-error"&gt;sdlMixer&lt;/span&gt;&lt;/span&gt; for making your speakers blast some sound. &lt;/li&gt;   &lt;/ul&gt; &lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;&lt;b&gt;Cons:&lt;/b&gt;&lt;/div&gt;  &lt;div&gt;   &lt;ul&gt;     &lt;li&gt;&lt;span style="text-decoration: underline" class="Apple-style-span"&gt;Its boring&lt;/span&gt;, up to a degree. Sure, you always have the hard-core programmer's joy of implementing a new feature here, tweaking a &lt;span id="SPELLING_ERROR_10" class="blsp-spelling-corrected"&gt;system&lt;/span&gt; there, but there's always a point when not &lt;span id="SPELLING_ERROR_11" class="blsp-spelling-corrected"&gt;seeing&lt;/span&gt; results starts eroding morale. It's not too bad per-&lt;span id="SPELLING_ERROR_12" class="blsp-spelling-error"&gt;&lt;span id="SPELLING_ERROR_7" class="blsp-spelling-error"&gt;se&lt;/span&gt;&lt;/span&gt;, but it can &lt;span id="SPELLING_ERROR_13" class="blsp-spelling-corrected"&gt;quickly&lt;/span&gt; kill a project, and &lt;i&gt;that &lt;/i&gt;is something to be avoided. &lt;/li&gt;      &lt;li&gt;&lt;span style="text-decoration: underline" class="Apple-style-span"&gt;It's harder&lt;/span&gt;, &lt;span id="SPELLING_ERROR_14" class="blsp-spelling-corrected"&gt;which&lt;/span&gt; to some translates to boring, &lt;span id="SPELLING_ERROR_15" class="blsp-spelling-corrected"&gt;which&lt;/span&gt; takes us to the above logic. &lt;/li&gt;      &lt;li&gt;When all is said and done, you actually &lt;span style="text-decoration: underline" class="Apple-style-span"&gt;have nothing to show&lt;/span&gt;. No game here to play and &lt;span id="SPELLING_ERROR_17" class="blsp-spelling-corrected"&gt;show&lt;/span&gt; of to &lt;span id="SPELLING_ERROR_18" class="blsp-spelling-corrected"&gt;family&lt;/span&gt; and friends, nothing visible to put on your &lt;span id="SPELLING_ERROR_19" class="blsp-spelling-corrected"&gt;portfolio&lt;/span&gt;. &lt;/li&gt;   &lt;/ul&gt; &lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;&lt;b&gt;&lt;span style="font-size: large" class="Apple-style-span"&gt;Game:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;&lt;b&gt;Pros:&lt;/b&gt;&lt;/div&gt;  &lt;div&gt;   &lt;ul&gt;     &lt;li&gt;&lt;span style="text-decoration: underline" class="Apple-style-span"&gt;You learn t&lt;/span&gt;o make a game, &lt;span id="SPELLING_ERROR_20" class="blsp-spelling-corrected"&gt;which&lt;/span&gt; is a obscure science that &lt;span id="SPELLING_ERROR_21" class="blsp-spelling-corrected"&gt;no one&lt;/span&gt; can teach. Something like &lt;span id="SPELLING_ERROR_22" class="blsp-spelling-corrected"&gt;illumination&lt;/span&gt;, but with a lot more networking involved. &lt;/li&gt;      &lt;li&gt;You have a shinny new gem to &lt;span style="text-decoration: underline" class="Apple-style-span"&gt;add to your portfolio&lt;/span&gt;, and to impress chicks at bars (well, some chicks, at some bars at least). &lt;/li&gt;      &lt;li&gt;You get your hands dirty with a full featured engine, and&lt;span style="text-decoration: underline" class="Apple-style-span"&gt; learn from their design &lt;/span&gt;and their functionality. &lt;/li&gt;      &lt;li&gt;It's &lt;span style="text-decoration: underline" class="Apple-style-span"&gt;easier, and faster, make that much much faster&lt;/span&gt;. &lt;/li&gt;      &lt;li&gt;You have &lt;span style="text-decoration: underline" class="Apple-style-span"&gt;things visible from near the start,&lt;/span&gt; and &lt;span id="SPELLING_ERROR_23" class="blsp-spelling-error"&gt;there is&lt;/span&gt; nothing for keeping &lt;span id="SPELLING_ERROR_24" class="blsp-spelling-corrected"&gt;morale&lt;/span&gt; up like seeing &lt;span id="SPELLING_ERROR_25" class="blsp-spelling-corrected"&gt;little&lt;/span&gt; everyday improvements to your project. &lt;/li&gt;      &lt;li&gt;It&lt;span style="text-decoration: underline" class="Apple-style-span"&gt; gives you security&lt;/span&gt;, as you finish one game after another, and that in turn makes you push a bit further, take bigger challenges. &lt;/li&gt;   &lt;/ul&gt; &lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;&lt;b&gt;Cons:&lt;/b&gt;&lt;/div&gt;  &lt;div&gt;   &lt;ul&gt;     &lt;li&gt;Half the work is done already. So that's one part you won't be needing to sweat, but it's also &lt;span style="text-decoration: underline" class="Apple-style-span"&gt;a part you're not gonna learn how it works&lt;/span&gt;. &lt;/li&gt;   &lt;/ul&gt; &lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;&lt;span style="font-size: large" class="Apple-style-span"&gt;&lt;b&gt;Conclusion:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;  &lt;div&gt;The above is my personal experience, so the conclusion will be my personal advice:&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;Make some games, get the feel of some engines, see &lt;span id="SPELLING_ERROR_26" class="blsp-spelling-corrected"&gt;what&lt;/span&gt; works and what doesn't. Those structures and systems you build time and again (entity manager, log system, menu state machine, any of those ring a bell?) save them up and &lt;span id="SPELLING_ERROR_27" class="blsp-spelling-error"&gt;re-utilize&lt;/span&gt; them from game to game, that way you'll be making them generic.&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;When you've done that, make an engine. Learn from the bottom up, get a paper tablecloth and sketch a full blown design of the engine, and implement it with care and dedication.&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;That over with, get back to making games, and if the engine works fine, remember to release it to the nice folks of the &lt;span id="SPELLING_ERROR_28" class="blsp-spelling-error"&gt;&lt;span id="SPELLING_ERROR_8" class="blsp-spelling-error"&gt;interwebs&lt;/span&gt;&lt;/span&gt;, so we can learn from you!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-4506778342451946060?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/4506778342451946060/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2009/06/when-immersed-in-amateur-game.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/4506778342451946060'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/4506778342451946060'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2009/06/when-immersed-in-amateur-game.html' title='Engine vs. Game'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-6138274344240011377</id><published>2009-06-10T19:04:00.006+02:00</published><updated>2010-01-13T12:21:24.280+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Game'/><category scheme='http://www.blogger.com/atom/ns#' term='ActionScript'/><category scheme='http://www.blogger.com/atom/ns#' term='Completed'/><category scheme='http://www.blogger.com/atom/ns#' term='Flash'/><title type='text'>Game - Phagocitosis</title><content type='html'>&lt;div style="text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_0"&gt;Phagocitosis&lt;/span&gt; is a 1 week, simple, small &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_1"&gt;puzzle&lt;/span&gt; game I created some months back.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It was developed in &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_2"&gt;ActionScript&lt;/span&gt; with Adobe Flash CS4, using the &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_3"&gt;KeyObject&lt;/span&gt; library as the only external help. A friend proposed the subject and I &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_4"&gt;couldn't&lt;/span&gt; resist the challenge of coding a game and learning a new &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_5"&gt;IDE&lt;/span&gt; and language at the same time, so here's how I went about  it.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;img src="http://3.bp.blogspot.com/_vaakdclE4vY/Si_40Hvxc0I/AAAAAAAAAUU/B_P6bK6MpbI/s320/Phago.jpg" style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 232px;" border="0" alt="" id="BLOGGER_PHOTO_ID_5345764857028375362" /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;Design:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;The Game Design was informal and written in &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_6"&gt;miscellaneous&lt;/span&gt; pieces of paper, napkins, and the back of envelopes. It was &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_7"&gt;mainly&lt;/span&gt; done by my buddy &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_8"&gt;Undi&lt;/span&gt;. It took &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_9"&gt;several&lt;/span&gt; months of sporadic work to get the general idea working.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Once the game design was over with, the Investigation and Technical Design phases began, in &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_10"&gt;parallel&lt;/span&gt;. As it happens with new technologies, you can't design the technical aspect &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_11"&gt;until&lt;/span&gt; you know how they work, and you can't know what you need to investigate &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_12"&gt;until&lt;/span&gt; you know what you need. &lt;/div&gt;&lt;div&gt;The result of that phase was this &lt;a href="http://docs.google.com/View?id=dhj9jxv7_107d45vkkhn"&gt;Technical Design Document (TDD),&lt;/a&gt; (well, actually a draft, it was a quick development so it didn't need detail) specked with investigation notes, and this list of references:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://www.8bitrocket.com/newsdisplay.aspx?newspage=6077"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_13"&gt;Tilemaps&lt;/span&gt; with &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_14"&gt;Mappy&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.actionscript.org/forums/showthread.php3?t=169570"&gt;Event listeners (or AS2.0 is NOT AS3.0)&lt;/a&gt; and &lt;a href="http://board.flashkit.com/board/showthread.php?t=787325"&gt;here&lt;/a&gt;&lt;/li&gt;&lt;li&gt;God sent&lt;a href="http://blip.tv/file/354374"&gt; Flash Game University&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.flex888.com/598/using-keycodes-in-actionscript-3.html"&gt;Key Input&lt;/a&gt; and &lt;a href="http://www.actionscript.org/forums/showthread.php3?p=805657"&gt;here&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://blogs.adobe.com/pdehaan/2006/07/creating_new_movieclips_in_act.html"&gt;Movie Clip Creation&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://asgamer.com/2009/flash-game-design-basics-adding-library-objects-to-stage-with-as3"&gt;THE LIFE SAVER TUTORIALS&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;Also, some interesting book titles:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Game Development With &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_15"&gt;ActionScript&lt;/span&gt;&lt;/li&gt;&lt;li&gt;Essential &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_16"&gt;ActionScript&lt;/span&gt; 3.0 &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold; "&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;Development:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;The very first thing you come to realize when jumping into the Flash bandwagon is that there's &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_17"&gt;ActionScript&lt;/span&gt; 2.0 and &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_18"&gt;ActionScript&lt;/span&gt; 3.0 and that alone can shoot everything to hell.&lt;/div&gt;&lt;div&gt;I chose AS 3.0 on the grounds that, tough it might have a smaller &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_19"&gt;support&lt;/span&gt; base, for &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_20"&gt;learning&lt;/span&gt; purposes it's &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_21"&gt;always&lt;/span&gt; a good idea to go for the next thing, as technologies &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_22"&gt;outdate&lt;/span&gt; incredibly fast in this field.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;After a few false starts, getting Adobe Flash working and &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_23"&gt;setting&lt;/span&gt; up a &lt;a href="http://code.assembla.com/phago/subversion/nodes"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_24"&gt;SVN&lt;/span&gt;&lt;/a&gt; got me ready to start coding.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The game was built based on 3 things, the scenario, the player (a slimy Amoeba) and the enemy (a simple hole, if you fell in, you died).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;The scenario&lt;/b&gt; was set up as a series of &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_25"&gt;keyframes&lt;/span&gt; in the main timeline of Adobe Flash, with each &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_26"&gt;keyframe&lt;/span&gt; having an &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_27"&gt;associated&lt;/span&gt; Enter, Execute and Exit functions, creating a Finite State Machine to provide for all the screens of the scenario.&lt;/div&gt;&lt;div&gt;Scenes:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Menu&lt;/li&gt;&lt;li&gt;Scene Selection&lt;/li&gt;&lt;li&gt;Play&lt;/li&gt;&lt;li&gt;Pause&lt;/li&gt;&lt;li&gt;Game Over&lt;/li&gt;&lt;li&gt;Game Won&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;And you can guess what each one represents ;)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Each scene had it's own &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_28"&gt;challenges&lt;/span&gt;, but the 2 biggest ones were:&lt;/div&gt;&lt;div&gt;&lt;i&gt;Making buttons: &lt;/i&gt;5 out of the 6 Scenes needed buttons, and those introduced me to the world of callbacks in AS, coupled with event handling and &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_29"&gt;corresponding&lt;/span&gt; objects from the &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_30"&gt;IDE&lt;/span&gt; to code entities.&lt;/div&gt;&lt;div&gt;&lt;i&gt;The map&lt;/i&gt;: I wanted to have a  easy way to create new levels and add them, and as I was using &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_31"&gt;mappy&lt;/span&gt; to generate &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_32"&gt;xml&lt;/span&gt; files of the levels, it stood to reason I had to have a in-game &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_33"&gt;xml&lt;/span&gt; interpreter that also had &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_34"&gt;access&lt;/span&gt; to the &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_35"&gt;tilemaps&lt;/span&gt; and rendered the complete map. Now &lt;i&gt;that&lt;/i&gt; was fun.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;And everywhere, I had to take out my magical &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_36"&gt;photoshop&lt;/span&gt; and produce crappy graphic after crappy graphic to fill my scene. I tried for a simple animation of the Amoeba and failed dramatically, after &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_37"&gt;which&lt;/span&gt; I gave up and focused on static &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_38"&gt;gfx&lt;/span&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;The Amoeba and the Hole&lt;/b&gt;:&lt;/div&gt;&lt;div&gt;Each of these elements had to move &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_39"&gt;around&lt;/span&gt; the map without charging trough walls. The pixel perfect &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_40"&gt;collision&lt;/span&gt; detection was a real bugger, even tough the game only has 4 directions available.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The Amoeba was player controlled, so it needed a bit of input-handling magic, &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_41"&gt;which&lt;/span&gt; had me fighting key input for more than a few hours. &lt;/div&gt;&lt;div&gt;The Hole on the other hand, was AI &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_42"&gt;driven&lt;/span&gt;, and it uses a special &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_43"&gt;pathfinding&lt;/span&gt; &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_44"&gt;approximation&lt;/span&gt; &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_45"&gt;algorithm&lt;/span&gt; based on it's relative position to the amoeba, instead of going for a full &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_46"&gt;pathfinding&lt;/span&gt; with &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_47"&gt;Dijkstra&lt;/span&gt; or A*. Not too &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_48"&gt;surprisingly&lt;/span&gt;, this hack, &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_49"&gt;apart&lt;/span&gt; from making the game run MUCH faster, made it more fun, as you could bait the hole to come out of &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_50"&gt;certain&lt;/span&gt; areas instead of just waiting for it to chase you.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;Game Play and Map-&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_51"&gt;Makin&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;g&lt;/span&gt;&lt;/b&gt;:&lt;/div&gt;&lt;div&gt;Final logic was added, specifically:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_52"&gt;Collision&lt;/span&gt; between Hole and Amoeba results on player death&lt;/li&gt;&lt;li&gt;&lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_53"&gt;Collision&lt;/span&gt; between player and Objective results on loading next map&lt;/li&gt;&lt;li&gt;Map loading and changing&lt;/li&gt;&lt;li&gt;Game Over and Game Win&lt;/li&gt;&lt;li&gt;Select scene (start the game from a specific map)&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;Map after map was produced (special thanks to my brother there) each with &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_54"&gt;different&lt;/span&gt; &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_55"&gt;challenges&lt;/span&gt;, and tweaked to make them have a smooth &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_56"&gt;difficulty&lt;/span&gt; curve (tough different players disagree on that one).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;&lt;b&gt;The End:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Final touches where made on the last wee hours of the morning in the last day of the development week, and a html file created for quick play without having to &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_57"&gt;payload&lt;/span&gt; it to a server.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The game &lt;a href="http://code.assembla.com/phago/subversion/nodes"&gt;can be found at the &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_58"&gt;SVN&lt;/span&gt;&lt;/a&gt;, along with all the code. Or if you prefer, a &lt;a href="http://code.assembla.com/phago/subversion/nodes/Phagocitosis%201.0.rar"&gt;direct download&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Enjoy!&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-6138274344240011377?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/6138274344240011377/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2009/06/game-spell-phagocitosis.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/6138274344240011377'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/6138274344240011377'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2009/06/game-spell-phagocitosis.html' title='Game - Phagocitosis'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_vaakdclE4vY/Si_40Hvxc0I/AAAAAAAAAUU/B_P6bK6MpbI/s72-c/Phago.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-8559430693668391885</id><published>2009-06-10T12:50:00.008+02:00</published><updated>2010-01-13T11:45:10.235+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Cocos2D'/><category scheme='http://www.blogger.com/atom/ns#' term='Library'/><category scheme='http://www.blogger.com/atom/ns#' term='iPhone'/><category scheme='http://www.blogger.com/atom/ns#' term='ObjectiveC'/><title type='text'>Cocos2D iPhone Research</title><content type='html'>&lt;a href="http://lh6.ggpht.com/_vaakdclE4vY/S02kMymtf5I/AAAAAAAAAow/TM9OGmFnqCc/s1600-h/logo%5B3%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="logo" border="0" alt="logo" align="left" src="http://lh3.ggpht.com/_vaakdclE4vY/S02kNfF8H1I/AAAAAAAAAo0/Anxn9mC4tvA/logo_thumb%5B1%5D.png?imgmax=800" width="59" height="59" /&gt;&lt;/a&gt;&amp;#160; &lt;a href="http://www.cocos2d-iphone.com/"&gt;Cocos2D &lt;/a&gt;is one of the top choices when choosing an iPhone game development engine, &lt;b&gt;THE &lt;/b&gt;top choice if you're looking for something free. Works great in 2D and has one of the best scene graphs I’ve seen out there.&amp;#160; &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;First up, a list of &lt;a href="http://docs.google.com/View?id=dhj9jxv7_165f9cnb2hg"&gt;interesting links&lt;/a&gt; to guide the efforts of the aspiring Cocos2D programmer. &lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;For starters, here is &lt;a href="http://monoclestudios.com/cocos2d_whitepaper.html"&gt;&lt;span id="SPELLING_ERROR_0" class="blsp-spelling-error"&gt;monoclestudios&lt;/span&gt;&lt;/a&gt;. This company decided to give a &lt;span id="SPELLING_ERROR_1" class="blsp-spelling-corrected"&gt;little&lt;/span&gt; bit back to cocos2D, as a thanks for using it for one of their games writing a &lt;span id="SPELLING_ERROR_2" class="blsp-spelling-corrected"&gt;walk trough&lt;/span&gt; that explains how to get a Cocos2D &lt;span id="SPELLING_ERROR_3" class="blsp-spelling-corrected"&gt;project&lt;/span&gt; working.&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;Once you've got that over with, you still need some preparation before heading on &lt;span id="SPELLING_ERROR_4" class="blsp-spelling-error"&gt;the&lt;/span&gt; path to adding cocos2D to your basic arsenal. The &lt;a href="http://www.sapusmedia.com/cocos2d-iphone-api-doc/inherits.html"&gt;class hierarchy&lt;/a&gt; of the &lt;a href="http://www.sapusmedia.com/cocos2d-iphone-api-doc/"&gt;documentation &lt;/a&gt;must be read carefully to get a general idea of the engine works. Making your &lt;span id="SPELLING_ERROR_5" class="blsp-spelling-corrected"&gt;synapses&lt;/span&gt; comprehend the connections between the different &lt;span id="SPELLING_ERROR_6" class="blsp-spelling-corrected"&gt;classes&lt;/span&gt; will give you a good foothold.&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;With that bit of theory as reference, you can now enjoy the &lt;span id="SPELLING_ERROR_7" class="blsp-spelling-corrected"&gt;wonderful&lt;/span&gt; &lt;a href="http://lethain.com/entry/2008/oct/03/notes-on-cocos2d-iphone-development/"&gt;article &lt;/a&gt;written by Will Larson, showing the idea of how to work with the basic &lt;span id="SPELLING_ERROR_8" class="blsp-spelling-corrected"&gt;pillars&lt;/span&gt; of Cocos2D. After that one, you should have enough idea of the engine to draft a basic game architecture.&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;What our life needs right now, apart of a nice cup of &lt;span id="SPELLING_ERROR_9" class="blsp-spelling-corrected"&gt;coffee&lt;/span&gt;, is a&lt;a href="http://morethanmachine.com/macdev/?tag=cocos2d"&gt; step-by-step, code spiced, dummy game&lt;/a&gt;, to show how everything actually works, and to provide some working code for us to play with. Start reading those articles from the bottom, you won't be &lt;span id="SPELLING_ERROR_10" class="blsp-spelling-corrected"&gt;disappointed&lt;/span&gt;. Of special interest, I would point out the detection of user input, physics with &lt;span id="SPELLING_ERROR_11" class="blsp-spelling-corrected"&gt;chipmunk&lt;/span&gt; and a custom made particle &lt;span id="SPELLING_ERROR_12" class="blsp-spelling-corrected"&gt;effect&lt;/span&gt; in that series of tutorials.&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;That's about it! With that nice &lt;span id="SPELLING_ERROR_13" class="blsp-spelling-corrected"&gt;collection&lt;/span&gt; you can get well on your way with your iPhone game coding skills.&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;Code hard and have fun!&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-8559430693668391885?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/8559430693668391885/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2009/06/engine-artifact-cocos2d-investigation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/8559430693668391885'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/8559430693668391885'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2009/06/engine-artifact-cocos2d-investigation.html' title='Cocos2D iPhone Research'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_vaakdclE4vY/S02fTPhRy8I/AAAAAAAAAoQ/Ez6NZKskPw4/S220/CaraFriki_bigger.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/_vaakdclE4vY/S02kNfF8H1I/AAAAAAAAAo0/Anxn9mC4tvA/s72-c/logo_thumb%5B1%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1014947009705527212.post-386574640637776669</id><published>2009-06-09T18:55:00.001+02:00</published><updated>2010-01-13T11:28:21.564+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Misc'/><title type='text'>The WHY.</title><content type='html'>This blog is, to be frank, just an excuse to record the &lt;span id="SPELLING_ERROR_0" class="blsp-spelling-corrected"&gt;knowledge&lt;/span&gt; &lt;span id="SPELLING_ERROR_1" class="blsp-spelling-corrected"&gt;acquired&lt;/span&gt; as a game programmer. Essays, documentation, &lt;span id="SPELLING_ERROR_2" class="blsp-spelling-corrected"&gt;projects&lt;/span&gt;, libraries... it all needs to be recorded somewhere so people can use it, and this is going to be that place.  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;It will be updated &lt;span id="SPELLING_ERROR_3" class="blsp-spelling-corrected"&gt;regularly&lt;/span&gt; every weekend, and sporadically as need &lt;span id="SPELLING_ERROR_4" class="blsp-spelling-corrected"&gt;arises&lt;/span&gt;.&lt;/div&gt;  &lt;div&gt;   &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;I hope the arcane texts will be useful to all those who have the skill to read them :)&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1014947009705527212-386574640637776669?l=magicscrollsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://magicscrollsofcode.blogspot.com/feeds/386574640637776669/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://magicscrollsofcode.blogspot.com/2009/06/why.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/386574640637776669'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1014947009705527212/posts/default/386574640637776669'/><link rel='alternate' type='text/html' href='http://magicscrollsofcode.blogspot.com/2009/06/why.html' title='The WHY.'/><author><name>Jaime Barrachina Verdia</name><uri>http://www.blogger.com/profile/09340880625384452520</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnai
