<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Doolwind&#039;s Game Coding Blog &#187; Scripting</title>
	<atom:link href="http://www.doolwind.com/blog/tag/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.doolwind.com/blog</link>
	<description>Pragmatic Thoughts On Game Development</description>
	<lastBuildDate>Sun, 08 Jan 2012 08:15:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Fluent Game Design With Fluent Interfaces</title>
		<link>http://www.doolwind.com/blog/fluent-game-design-with-fluent-interfaces/</link>
		<comments>http://www.doolwind.com/blog/fluent-game-design-with-fluent-interfaces/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 08:00:08 +0000</pubDate>
		<dc:creator>Doolwind</dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Game Coding]]></category>
		<category><![CDATA[Game Designer]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Scripting Language]]></category>

		<guid isPermaLink="false">http://www.doolwind.com/blog/?p=419</guid>
		<description><![CDATA[Game designers often find themselves writing code in modern games.  Often, they have little to no programming experience and therefore must be taught the basics of programming (sequence, conditionals and loops).  I propose utilizing a technique that simplifies the code written by game designers in their games.  This technique is known as “Fluent Interfaces”. What [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.doolwind.com/images/blog/fluentinterfaces.jpg"><img class="alignright" title="Fluent Interfaces" src="http://www.doolwind.com/images/blog/fluentinterfacessm.jpg" alt="" width="200" height="125" /></a>Game designers often find themselves writing code in modern games.  Often, they have little to no programming experience and therefore must be taught the basics of programming (sequence, conditionals and loops).  I propose utilizing a technique that simplifies the code written by game designers in their games.  This technique is known as “<a href="http://en.wikipedia.org/wiki/Fluent_interface">Fluent Interfaces</a>”.</p>
<p><span id="more-419"></span></p>
<p><strong>What is Fluent?</strong></p>
<p>Fluent interfaces allow game designers to <span class="pullquote pqRight">write more fluid and readable code</span>.  Through the use of method chaining, English like sentences can be written to express game functionality.  Fluent interfaces can be implemented in any object oriented programming language.  Below is an example of a line in the game design document, a standard implementation example and a fluent example:</p>
<p><span style="text-decoration: underline;">Game Design Document:</span></p>
<p>&#8220;Do 5 damage to all enemy tanks within range 2 of an entity&#8221;</p>
<p><span style="text-decoration: underline;">Standard Example:</span></p>
<pre class="csharpcode"><span class="csharp05">foreach</span><span class="csharp10">(</span><span class="csharp00"> </span>var<span class="csharp00"> </span>unit<span class="csharp00"> </span><span class="csharp05">in</span><span class="csharp00"> </span>player<span class="csharp10">.</span>UnitsWithinRange<span class="csharp10">(</span><span class="csharp04">2</span><span class="csharp10">)</span><span class="csharp00"> </span><span class="csharp10">)</span><span class="csharp00">
</span><span class="csharp10">{</span><span class="csharp00">
    </span><span class="csharp05">if</span><span class="csharp10">(!</span>unit<span class="csharp10">.</span>IsType<span class="csharp10">(</span>Enemy<span class="csharp10">)</span><span class="csharp00"> </span><span class="csharp10">||</span><span class="csharp00"> </span><span class="csharp10">!</span>unit<span class="csharp10">.</span>IsType<span class="csharp10">(</span>Tank<span class="csharp10">))</span><span class="csharp00">
        </span><span class="csharp05">continue</span><span class="csharp10">;</span><span class="csharp00">

    </span>unit<span class="csharp10">.</span>Damage<span class="csharp10">(</span>5<span class="csharp10">);</span><span class="csharp00">
</span><span class="csharp10">}</span><span class="csharp00">
</span></pre>
<p><span style="text-decoration: underline;">Fluent Example:</span></p>
<pre class="csharpcode">player<span class="csharp10">.</span>UnitsWithinRange<span class="csharp10">(</span><span class="csharp04">2</span><span class="csharp10">)</span><span class="csharp00">
  </span><span class="csharp10">.</span>Where<span class="csharp10">(</span>UnitIs<span class="csharp10">.</span>Enemy<span class="csharp10">)</span><span class="csharp00">
  </span><span class="csharp10">.</span>Where<span class="csharp10">(</span>UnitIs<span class="csharp10">.</span>Tank<span class="csharp10">)</span><span class="csharp00">
  </span><span class="csharp10">.</span>DoDamage<span class="csharp10">(</span><span class="csharp04">5</span><span class="csharp10">);</span></pre>
<p>This fluent example closely matches the design document and is easier to read.  It also uses less language constructs like loops and conditions.  With Intellisense, designers are given a context sensitive list of operations they can perform.  Designers simply build up the expression that describes the original line in the game design document they are implementing.</p>
<p><strong>How to implement Fluent Interfaces </strong></p>
<p>Fluent interfaces are actually quite easy to implement.  Objects expose methods that return a reference to the object itself allowing method chaining.  The best way to describe this is by showing the implementation required for the examples above.</p>
<p>First, we create the object we will be working on (I’ve called it UnitsList in my examples).  This gives us the first part of the fluent call (Get.UnitsWithinRange(2)).</p>
<pre class="csharpcode"><span class="csharp05">public</span><span class="csharp00"> </span><span class="csharp05">class</span><span class="csharp00"> </span>ScriptObject<span class="csharp00">
</span><span class="csharp10">{</span><span class="csharp00">
    </span>UnitsList<span class="csharp00"> </span>UnitsWithinRange<span class="csharp10">(</span><span class="csharp05">int</span><span class="csharp00"> </span>range<span class="csharp10">)</span><span class="csharp00">
    </span><span class="csharp10">{</span><span class="csharp00">
        </span><span class="csharp05">return</span><span class="csharp00"> </span><span class="csharp05">new</span><span class="csharp00"> </span>UnitsList<span class="csharp10">(</span>range<span class="csharp10">);</span><span class="csharp00">
    </span><span class="csharp10">}</span><span class="csharp00">
</span><span class="csharp10">}</span><span class="csharp00">
</span></pre>
<p>The UnitsList object must have a set of methods that return references to the object itself allowing method chanining:</p>
<pre class="csharpcode"><span class="csharp05">public</span><span class="csharp00"> </span><span class="csharp05">class</span><span class="csharp00"> </span>UnitsList<span class="csharp00">
</span><span class="csharp10">{</span><span class="csharp00">
    </span><span class="csharp05">public</span><span class="csharp00"> </span>UnitsList<span class="csharp00"> </span>Where<span class="csharp10">(</span>UnitIs<span class="csharp00"> </span>condition<span class="csharp10">)</span><span class="csharp00">
    </span><span class="csharp10">{</span><span class="csharp00">
        </span><span class="csharp05">this</span><span class="csharp10">.</span>conditions<span class="csharp10">.</span>Add<span class="csharp10">(</span>condition<span class="csharp10">);</span><span class="csharp00">
        </span><span class="csharp05">return</span><span class="csharp00"> </span><span class="csharp05">this</span><span class="csharp10">;</span><span class="csharp00">
    </span><span class="csharp10">}</span></pre>
<p>The fluent expressions are terminated by returning void from a function.  This lets the designer know they have no more options and often that the actual operation will be performed.</p>
<pre class="csharpcode"><span class="csharp05">public</span><span class="csharp00"> </span><span class="csharp05">class</span><span class="csharp00"> </span>UnitsList<span class="csharp00">
</span><span class="csharp10">{</span><span class="csharp00">
    </span><span class="csharp05">public</span><span class="csharp00"> </span><span class="csharp05">void</span><span class="csharp00"> </span>DoDamage<span class="csharp10">(</span><span class="csharp05">int</span><span class="csharp00"> </span>damage<span class="csharp10">)</span><span class="csharp00">
    </span><span class="csharp10">{</span><span class="csharp00">
        </span><span class="csharp05">foreach</span><span class="csharp10">(</span><span class="csharp00"> </span>var<span class="csharp00"> </span>unit<span class="csharp00"> </span><span class="csharp05">in</span><span class="csharp00"> </span><span class="csharp05">this</span><span class="csharp10">.</span>mainUnit<span class="csharp10">.</span>UnitsWithinRange<span class="csharp10">(</span>range<span class="csharp10">)</span><span class="csharp00"> </span><span class="csharp10">)</span><span class="csharp00">
        </span><span class="csharp10">{</span><span class="csharp00">
            </span><span class="csharp05">if</span><span class="csharp10">(!</span>PassesConditions<span class="csharp10">(</span>unit<span class="csharp10">))</span><span class="csharp00">
                </span><span class="csharp05">continue</span><span class="csharp10">;</span><span class="csharp00">

            </span>unit<span class="csharp10">.</span>Damage<span class="csharp10">(</span>damage<span class="csharp10">);</span><span class="csharp00">
        </span><span class="csharp10">}</span><span class="csharp00">
    </span><span class="csharp10">}</span><span class="csharp00">

    </span><span class="csharp05">private</span><span class="csharp00"> </span><span class="csharp05">bool</span><span class="csharp00"> </span>PassesConditions<span class="csharp10">(</span>Unit<span class="csharp00"> </span>unit<span class="csharp10">)</span><span class="csharp00">
    </span><span class="csharp10">{</span><span class="csharp00">
        </span><span class="csharp05">foreach</span><span class="csharp10">(</span><span class="csharp00"> </span>var<span class="csharp00"> </span>condition<span class="csharp00"> </span><span class="csharp05">in</span><span class="csharp00"> </span>conditions<span class="csharp10">)</span><span class="csharp00">
        </span><span class="csharp10">{</span><span class="csharp00">
            </span><span class="csharp05">if</span><span class="csharp10">(!</span>unit<span class="csharp10">.</span>IsType<span class="csharp10">(</span>condition<span class="csharp10">))</span><span class="csharp00">
                </span><span class="csharp05">return</span><span class="csharp00"> </span><span class="csharp05">false</span><span class="csharp10">;</span><span class="csharp00">
        </span><span class="csharp10">}</span><span class="csharp00">
        </span><span class="csharp05">return</span><span class="csharp00"> </span><span class="csharp05">true</span><span class="csharp10">;</span><span class="csharp00">
    </span><span class="csharp10">}</span><span class="csharp00">
</span><span class="csharp10">}</span><span class="csharp00">
</span></pre>
<p>There are a couple of key things to notice:</p>
<ol>
<li>This last code example      looks a lot like the original standard example</li>
<li>A lot more “engine” code      is required to setup a fluent interface than a standard interface</li>
</ol>
<p>So, in effect, an extra layer of abstraction is being placed over the original code.  Rather than designers working on loops and conditions, they are calling (well named) methods.  This makes their life a lot easier and simplifies maintenance of their gameplay code.  It pushes the burden of maintenance down from the gameplay to the engine level and therefore on programmers, who are better suited to maintaining code.  If there is a change in the engine or game, this extra level of abstraction serves to buffer the designers and reduce the amount of code that needs to be written.</p>
<p><strong>Good Interface Design</strong></p>
<p>Care needs to be taken when designing the interfaces and exposing methods to the designers.  Rather than exposing all functions off a single object my recommendation is to define different objects for different situations.  The example provided starts with the player and retrieves a list of units around it.  Filters are then added before the final operation is performed on the resulting list.  By limiting the methods available to the designer to simple filters there is little risk of them making a mistake.  Also, the fact that the &#8220;DoDamage&#8221; function returns void stops them from chaining anything further.</p>
<p><strong>Other Syntaxes</strong></p>
<p>One small point is that designers can structure their fluent “sentences” in any way they please:</p>
<pre class="csharpcode">player<span class="csharp10">.</span>UnitsWithinRange<span class="csharp10">(</span><span class="csharp04">2</span><span class="csharp10">).</span>Where<span class="csharp10">(</span>UnitIs<span class="csharp10">.</span>Enemy<span class="csharp10">).</span>Where<span class="csharp10">(</span>UnitIs<span class="csharp10">.</span>Tank<span class="csharp10">).</span>DoDamage<span class="csharp10">(</span><span class="csharp04">5</span><span class="csharp10">);</span></pre>
<pre class="csharpcode">player<span class="csharp10">.</span>UnitsWithinRange<span class="csharp10">(</span><span class="csharp04">2</span><span class="csharp10">)</span><span class="csharp00">
  </span><span class="csharp10">.</span>Where<span class="csharp10">(</span>UnitIs<span class="csharp10">.</span>Enemy<span class="csharp10">)</span><span class="csharp00">
  </span><span class="csharp10">.</span>Where<span class="csharp10">(</span>UnitIs<span class="csharp10">.</span>Tank<span class="csharp10">)</span><span class="csharp00">
  </span><span class="csharp10">.</span>DoDamage<span class="csharp10">(</span><span class="csharp04">5</span><span class="csharp10">);</span></pre>
<p>There is no difference between the above two examples.  It’s simply a matter of coding style preferred by the writer.</p>
<p><strong>Conclusion</strong></p>
<p>What do you think of Fluent Interfaces?  Have you used a similar technique before?  Do you think the extra engine code and maintenance is too much hassle for the gain in clarity to designers?</p>
<p>As an experiment, try exposing a small set of functionality through a fluent interface and see whether your designers like working with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doolwind.com/blog/fluent-game-design-with-fluent-interfaces/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Why You Should Use C# For Your Scripting Language</title>
		<link>http://www.doolwind.com/blog/why-you-should-use-csharp-for-your-scripting-language/</link>
		<comments>http://www.doolwind.com/blog/why-you-should-use-csharp-for-your-scripting-language/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 21:37:59 +0000</pubDate>
		<dc:creator>Doolwind</dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Game Engine]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Scripting Language]]></category>

		<guid isPermaLink="false">http://www.doolwind.com/blog/?p=240</guid>
		<description><![CDATA[I&#8217;ve used a lot of scripting languages over the years when developing games.  For my latest engine, I decided I’d use C# as the scripting language.  I’ve been amazed by how well C# works as a scripting language so I thought I’d share my experiences.  The technique I’m about to discuss gives game designers access to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.doolwind.com/images/blog/csharp.jpg"><img class="alignright" title="C#" src="http://www.doolwind.com/images/blog/csharp.jpg" alt="" width="154" height="96" /></a>I&#8217;ve used a lot of scripting languages over the years when developing games.  For my latest engine, I decided I’d use C# as the scripting language.  I’ve been amazed by how well C# works as a scripting language so I thought I’d share my experiences.  The technique I’m about to discuss gives game designers access to a fully-featured language and IDE with compile-time checking all while allowing run-time changes.  I use run-time compilation to achieve the flexibility of a scripting language combined with the power of a .NET language.</p>
<p><span id="more-240"></span></p>
<p><strong>Advantages</strong></p>
<p>There are a number of key advantages to using C# as a scripting language in your game engine.  I’ll list these first then describe how I achieve them.</p>
<ul>
<li><strong>Designers use Visual Studio.</strong> Most designers are stuck using notepad (or similar) when writing      their scripts.  With my technique,      designers can use Visual Studio, gaining the full power of the IDE.  Including:<strong> </strong>
<ul>
<li><strong>Intellisense</strong>. Letting them know what functions exist on all       of the objects you’ve exposed, as well as documentation you’ve provided       in code.<strong> </strong></li>
<li><strong>Compilation</strong>.<strong> </strong>Designers can compile their scripts       while making changes to catch any compile-time problems.<strong> </strong></li>
<li><strong>Full Debugging Support. </strong>Another       area lacking in most scripting languages is the debugging support.  Designers have access to all of Visual       Studios debugging functionality to help them find and fix issues with       their code.<strong> </strong></li>
</ul>
</li>
<li><strong>Run-Time Changes.</strong> Designers still have complete control to change their scripts at      run-time.  Scripts can be reloaded      automatically when files are changed, or (more likely) a “reset” button      can be exposed in the game engine that allows them to reset the level and reload      all scripts.<strong></strong></li>
<li><strong>Compile scripts into the executable for release.</strong> You have the option to use the scripts      compiled into the executable if you prefer, protecting your files.<strong></strong></li>
<li><strong>Complete Access to game objects, or whatever you want designers to      access.</strong> Designers are working      in the same language you are developing in.  They have full access to the game      objects or to a subset you decide on.       I use an interface and expose what I want them to use.<strong></strong></li>
<li><strong>FAST Run-time performance. </strong> The C# code is compiled not interpreted      meaning the run-time performance is as good as the rest of the engine.  The slight performance bottleneck of      reflecting the function can be removed by compiling scripts into      executable if required.<strong></strong></li>
<li><strong>No Binding Required.</strong> Binding is automatic between the game engine and the script.  You don’t need to write any plumbing and      exposing a new function is as easy as adding it to the engine.<strong></strong></li>
</ul>
<p><strong>How It Works</strong></p>
<p>The setup is surprisingly easy, designers follow these steps and their up and running:</p>
<ol>
<li>Add the script file to the      Visual Studio project (preferably in its own directory)</li>
<li>Create an Init() function      in the script class which will automatically be called by the engine</li>
<li>Create callbacks for events      and implement the game logic</li>
<li>Run the game and make      changes to scripts in real-time as needed</li>
</ol>
<p>I use the run-time compilation functionality of the .NET framework to compile and run the code on- the-fly.  This has a small performance hit during compilation however the run-time performance is excellent.</p>
<p><strong>Some Examples</strong></p>
<p>Below are a few examples I put together today to show the power of the system.</p>
<p>The first script below creates an entity at the beginning of the game and then sets up a callback that will fire every 5 seconds.  The TestScript object’s lifetime is until the scripting system is reset allowing stateful object creation.</p>
<pre class="csharpcode"><span class="csharp05">public</span><span class="csharp00"> </span><span class="csharp05">class</span><span class="csharp00"> </span>TestScript<span class="csharp00"> </span><span class="csharp10">:</span><span class="csharp00"> </span>BaseScript<span class="csharp00">
</span><span class="csharp10">{</span><span class="csharp00">
    </span><span class="csharp05">int</span><span class="csharp00"> </span>team<span class="csharp00"> </span><span class="csharp10">=</span><span class="csharp00"> </span><span class="csharp04">1</span><span class="csharp10">;</span><span class="csharp00">
    </span><span class="csharp05">int</span><span class="csharp00"> </span>xPos<span class="csharp00"> </span><span class="csharp10">=</span><span class="csharp00"> </span><span class="csharp04">15</span><span class="csharp10">;</span><span class="csharp00">
    </span><span class="csharp05">int</span><span class="csharp00"> </span>yPos<span class="csharp00"> </span><span class="csharp10">=</span><span class="csharp00"> </span><span class="csharp04">2</span><span class="csharp10">;</span><span class="csharp00">

    </span><span class="csharp05">public</span><span class="csharp00"> </span><span class="csharp05">void</span><span class="csharp00"> </span>Init<span class="csharp10">()</span><span class="csharp00">
    </span><span class="csharp10">{</span><span class="csharp00">
        </span>script<span class="csharp10">.</span>CreateEntity<span class="csharp10">(</span><span class="csharp06">"Healer"</span><span class="csharp10">,</span><span class="csharp00"> </span>team<span class="csharp10">,</span><span class="csharp00"> </span>xPos<span class="csharp10">,</span><span class="csharp00"> </span>yPos<span class="csharp10">);</span><span class="csharp00">
        </span>script<span class="csharp10">.</span>CallEveryXSeconds<span class="csharp10">(</span>MyCallback<span class="csharp10">,</span><span class="csharp00"> </span><span class="csharp04">5</span><span class="csharp10">);</span><span class="csharp00">
    </span><span class="csharp10">}</span><span class="csharp00">

    </span><span class="csharp05">public</span><span class="csharp00"> </span><span class="csharp05">void</span><span class="csharp00"> </span>MyCallback<span class="csharp10">()</span><span class="csharp00">
    </span><span class="csharp10">{</span><span class="csharp00">
        </span>script<span class="csharp10">.</span>CreateEntity<span class="csharp10">(</span><span class="csharp06">"Tank"</span><span class="csharp10">,</span><span class="csharp00"> </span>team<span class="csharp10">,</span><span class="csharp00"> </span>xPos<span class="csharp10">,</span><span class="csharp00"> </span>yPos<span class="csharp10">++);</span><span class="csharp00">
    </span><span class="csharp10">}</span><span class="csharp00">
</span><span class="csharp10">}</span><span class="csharp00">
</span></pre>
<p>The next script shows how simple customizing a game entity can be.  Whenever an effect is added or removed from a game entity, the following two functions are called allowing designers complete control over gameplay.  Notice the designer is given access only to what they need while still having complete control to change any settings or values of the entity.  An interface could be used to restrict what operations can be performed on entities as required.</p>
<pre class="csharpcode"><span class="csharp05">class</span><span class="csharp00"> </span>SpeedUpEffect<span class="csharp00">
</span><span class="csharp10">{</span><span class="csharp00">
    </span><span class="csharp05">public</span><span class="csharp00"> </span><span class="csharp05">void</span><span class="csharp00"> </span>AddEffect<span class="csharp10">(</span>GameEntity<span class="csharp00"> </span>entity<span class="csharp10">)</span><span class="csharp00">
    </span><span class="csharp10">{</span><span class="csharp00">
        </span>entity<span class="csharp10">.</span>Speed<span class="csharp00"> </span><span class="csharp10">+=</span><span class="csharp00"> </span><span class="csharp04">1</span><span class="csharp10">;</span><span class="csharp00">
    </span><span class="csharp10">}</span><span class="csharp00">

    </span><span class="csharp05">public</span><span class="csharp00"> </span><span class="csharp05">void</span><span class="csharp00"> </span>RemoveEffect<span class="csharp10">(</span>GameEntity<span class="csharp00"> </span>entity<span class="csharp10">)</span><span class="csharp00">
    </span><span class="csharp10">{</span><span class="csharp00">
        </span>entity<span class="csharp10">.</span>Speed<span class="csharp00"> </span><span class="csharp10">-=</span><span class="csharp00"> </span><span class="csharp04">1</span><span class="csharp10">;</span><span class="csharp00">
    </span><span class="csharp10">}</span><span class="csharp00">
</span><span class="csharp10">}</span><span class="csharp00">
</span></pre>
<p><strong>Conclusion</strong></p>
<p>As I said, I was amazed both at how simple all of this was to set up (only a few hours) and how powerful the system is.  If anyone is interested I’d be happy to share the source code for how I achieved this.  As I continue development of my engine I will be experimenting more with this scripting system and will release more information as I develop it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doolwind.com/blog/why-you-should-use-csharp-for-your-scripting-language/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Model View Controller (MVC) Game Engine</title>
		<link>http://www.doolwind.com/blog/model-view-controller-mvc-game-engine/</link>
		<comments>http://www.doolwind.com/blog/model-view-controller-mvc-game-engine/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 21:38:47 +0000</pubDate>
		<dc:creator>Doolwind</dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Game Engine]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.doolwind.com/blog/?p=186</guid>
		<description><![CDATA[The Model View Controller (MVC) pattern has been used with great success in business software development for years.  Despite this, it has never fully been picked up by the games industry.  Today I&#8217;m going to discuss why the MVC pattern is perfect for game development. Introduction The Model View Controller pattern isolates gameplay logic from [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.doolwind.com/images/blog/MVC.png"><img class="alignright" title="MVC" src="http://www.doolwind.com/images/blog/MVC.png" alt="" width="130" height="133" /></a>The Model View Controller (MVC) pattern has been used with great success in business software development for years.  Despite this, it has never fully been picked up by the games industry.  Today I&#8217;m going to discuss why the MVC pattern is perfect for game development.<span id="more-186"></span></p>
<h2><strong>Introduction</strong></h2>
<p>The Model View Controller pattern isolates gameplay logic from input and rendering.  This &#8220;Separation of Concerns&#8221; allows each layer to be developed, tested and maintained independently.  Graphics programmers work solely on rendering, gameplay programmers or designers work on gameplay and whoever&#8217;s left can work on input.</p>
<p>Model &#8211; Gameplay (game entities, eg. Player, Sword)<br />
View &#8211; Rendering<br />
Controller &#8211; Input and non-gameplay flow (menu&#8217;s etc)</p>
<p>The controller takes input from the player and changes the model.  The controller then passes the model (and any other relevant information) to the view to be rendered.</p>
<h2><strong>Advantages</strong></h2>
<p><strong>Cleaner Code</strong>.  Large teams can work independently on each layer of the application without conflict.  Clear interfaces are used to define communication across layer boundaries.  As the engine grows, this separation of concerns helps to minimize complexity.</p>
<p><strong>Better Cross Platform Support</strong>.  Gameplay is separate and not reliant on platform specific technology.  Rendering and input (both heavily platform specific) are separate and can easily be changed.</p>
<p><strong>Decoupled Rendering.</strong>  In most engines the game world and renderer are tightly coupled.  MVC decouples the game world (and input) from the rendering.  Rather than calling &#8220;Render&#8221; of each game entity the rendering system uses the model when rendering.  This also simplifies the process of adding multi-threaded support to the game or renderer.</p>
<p>I&#8217;ve used this technique in my latest engine and it feels so much more natural.  Rather than blurring the line between gameplay and rendering they are distinct and the renderer is responsible for bringing them together.</p>
<p><strong>Scripting/Designers</strong>.  All changes made to the model by the controller are through a known interface.  This interface can be exposed to designers/modders through a scripting language giving them greater flexibility and control.</p>
<h2><strong>Conclusion</strong></h2>
<p>I won&#8217;t go into implementation details right now.  If anyone is interested, I&#8217;ll put together a blog on implementation and look at releasing the MVC framework I created in XNA.  Let me know your thoughts, where you see there might be problems and if you&#8217;re interested in me discussing this further.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doolwind.com/blog/model-view-controller-mvc-game-engine/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

