<?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>#AltDevBlogADay &#187; Richard Fine</title>
	<atom:link href="http://www.altdevblogaday.com/author/richard-fine/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.altdevblogaday.com</link>
	<description>Each day a little more #gamedev love</description>
	<lastBuildDate>Thu, 17 May 2012 03:06:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>The Many-Worlds Interpretation of Game Development</title>
		<link>http://www.altdevblogaday.com/2011/11/19/the-many-worlds-interpretation-of-game-development/</link>
		<comments>http://www.altdevblogaday.com/2011/11/19/the-many-worlds-interpretation-of-game-development/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 21:33:23 +0000</pubDate>
		<dc:creator>Richard Fine</dc:creator>
				<category><![CDATA[General Interest]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=13357</guid>
		<description><![CDATA[<em>To any physicists reading: Sorry, there's going to be next to no quantum mechanics in this post.</em>

A widely-understood idea in games is the idea of the 'game world.' Yet while this world is presented to the player as a single cohesive, orchestrated whole, <a href="http://altdevblogaday.com/2011/11/19/the-many-worlds-interpretation-of-game-development/">the reality of virtuality isn't so singular...</a>]]></description>
			<content:encoded><![CDATA[<p><em>To any physicists reading: Sorry, there&#8217;s going to be next to no quantum mechanics in this post. I&#8217;m just riffing on the name of Everett&#8217;s theory, and will be using &#8216;worlds&#8217; in a different sense to him. (Though by all means, talk to me about quantum mechanics &#8211; it&#8217;s an interesting topic. Do get in touch if you can cogently explain how the Copenhagen interpretation can function without suffering from some serious anthropic issues).</em></p>
<p>A widely-understood idea in games is the idea of the &#8216;game world.&#8217; It is the virtual space in which the game takes place; it is the venue, the stage and backdrop. It&#8217;s full of interesting systems and processes: visuals, sound, motion, gravity, autonomous agents&#8230;</p>
<p>Yet while this world is presented to the player as a single cohesive, orchestrated whole, the reality of virtuality isn&#8217;t so singular. These different processes are frequently happening in their own independent worlds &#8211; like parallel dimensions that are deliberately overlapped. For example, what appears to be a highly detailed, carefully sculpted object in the visual world, may only be a flat-faced cube in the physics world.</p>
<div id="attachment_20325" class="wp-caption alignleft" style="width: 308px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/11/visual-world.png"><img src="http://altdevblogaday.com/wp-content/uploads/2011/11/visual-world-298x300.png" alt="" width="298" height="300" class="size-medium wp-image-20325" /></a><p class="wp-caption-text">The visual world</p></div>
<div id="attachment_20326" class="wp-caption alignright" style="width: 308px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/11/physics-world.png"><img src="http://altdevblogaday.com/wp-content/uploads/2011/11/physics-world-298x300.png" alt="" width="298" height="300" class="size-medium wp-image-20326" /></a><p class="wp-caption-text">The physics world (artist's impression)</p></div>
<div style="clear: both"></div>
<p>We consider them the same object because they share certain pieces of information. The position of the sculpture and the cube, for example, may be shared, such that as the cube is moved around by physical processes, the player perceives the sculpture to be moving. The sculpture itself has no mass or velocity; indeed it exists in a world where nothing has mass or velocity, and no processes exist that move objects around. But similarly, while the cube has mass and velocity, it has no textures or shaders, and so there is no way to really describe how it &#8220;looks.&#8221;</p>
<p>So objects in the game will have different representations in different &#8216;worlds.&#8217; There may even be some worlds in which they have no representation at all. Invisible walls, for example, have a representation in the physics world, but no representation in the visual world; while individual particles in a particle effect often exist only in the visual world, playing no part in physics, AI or sound. In reality, sound would bounce off the particles of dust from an explosion just as any other surface, but in our simulations the dust just doesn&#8217;t exist from the sound&#8217;s point of view.</p>
<p>Why do we do this? Performance and simplicity, mostly. By setting up different &#8216;worlds&#8217; for the different processes in the game, we can present those processes with data that is more suited to them. We <em>could</em> present the sculpture to the physics system in all its finely-detailed glory, but computing the collisions of such a complex object is a lot harder than computing collisions for a simple cube &#8211; and, if we&#8217;re honest, treating the sculpture as if it were a cube produces physical interactions that look good <em>enough</em>. By designing our different worlds carefully, we can simplify some of the problems that we face, turning intractable problems into tractable ones.</p>
<p>Take AI movement, for example. Imagine that you&#8217;re trying to write AI behaviour that&#8217;s based in the physics world. It&#8217;s logically <em>possible</em> &#8211; you <em>can</em> work out, from the physical representations of objects, which places your character can and can not be, and thus you <em>can</em> find a path to a target that accounts for obstacles like walls&#8230; but it would be very, very slow.</p>
<p>So instead, the AI world is very simple: it&#8217;s just a flat, connected mesh of triangles &#8211; a &#8220;navigation mesh&#8221; &#8211; coupled with a law that says that all AI agents must be located on the surface of one of the triangles at any time. There are no walls in the AI world &#8211; only the edges of the world itself. And now that we only need to obey the law about staying on the mesh, pathfinding and movement become a lot simpler; movement is effectively 2D within the surface of a single triangle, with a little more complexity at triangle borders. (There are more complications in practice, of course, like dynamic obstacle avoidance, and characters that jump, but they become special cases &#8211; the vast majority of agent movement is simplified).</p>
<div id="attachment_20334" class="wp-caption aligncenter" style="width: 308px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/11/ai-world.png"><img src="http://altdevblogaday.com/wp-content/uploads/2011/11/ai-world-298x300.png" alt="" width="298" height="300" class="size-medium wp-image-20334" /></a><p class="wp-caption-text">The AI world - basic navigation mesh, perhaps with a couple of &quot;cover points&quot; marked for agents to use</p></div>
<p>How is it that doing AI computations in a world <em>without</em> walls is able to correctly move characters in a world <em>with</em> walls? Because we, as the architects of these worlds, carefully designed them to overlap in particular ways. The AI world&#8217;s navigation mesh, for example, does not have any triangles that would overlap with the Physics world&#8217;s collision mesh. And the result? AI agents are prohibited &#8211; by their own world&#8217;s laws &#8211; from being in any position that would see them stuck in a wall, because such a position would not be on the surface of the AI world&#8217;s navigation mesh.</p>
<p>They <i>appear</i> to be exercising collision detection in the physics world &#8211; but in fact, their inability to walk through walls is almost coincidental. </p>
<p>When the worlds don&#8217;t overlap in the right way, or when one world lacks the laws required for a particular object to exist in it, we can get some odd results. For example, it&#8217;s common that the audio world in a game is a simple, infinite void, populated by a collection of point-based sound emitters and, somewhere, a &#8216;listener.&#8217; While this is sufficient to model some of the behaviour we expect from sound &#8211; for example, as I walk closer to a sound source, the sound gets louder &#8211; it&#8217;s also lacking the ability to model sound occlusion. If I&#8217;m standing 20 metres from an explosion, it&#8217;ll sound the same regardless of whether those 20 metres are occupied by air or by concrete. (I understand that the Call of Duty games shipped with an implementation of sound occlusion, but beyond that I don&#8217;t see many people talking about it. Anyone know how their implementation works?)</p>
<p>Oddly, players are used to a certain amount of discrepancy between worlds &#8211; they don&#8217;t expect particles to get in the way of bullets, or for the player character to bob up and down while walking over a pile of gibs. But many other situations are considered problematic. For example, walking against a wall that results in the player&#8217;s mesh intersecting the wall, because the physics representation is too tight relative to the visual representation; or a protruding bit of floor that the player appears to be able to stand on but which they actually fall straight through because it&#8217;s absent from the physics world.</p>
<p>Ensuring that these worlds are correlated in a way that makes sense to the player is an important task. Much of the time I spent working in QA was dealing with failures of this type. </p>
<p>So, given that managing that correlation is important, how can we do it most effectively?</p>
<p>At the most basic level we can simply test the game as a whole, exploring the superposition of all universes and their corresponding processes. This is a natural part of game testing, but it&#8217;s slow and error-prone: Walking into every corner of every level to ensure that the player cannot &#8220;fall out of the world&#8221; is not a very fun task, I assure you.</p>
<p>The problem is that the information we can gather about the different worlds in their &#8220;natural&#8221; states is frequently limited. We can <i>see</i> several hundred meters ahead of us in the <i>visual</i> world, but we can only <i>touch</i> the objects immediately next to us in the <i>physics</i> world. So we can inspect the visuals of the game world en masse, but inspecting the physics requires touching everything in turn, kicking every rock to check whether it kicks back. It&#8217;s even worse with things like the AI world &#8211; you&#8217;d have to coax and push agents around the world to check that they don&#8217;t suddenly behave strangely in one place.</p>
<p>So the first obvious solution is to transpose one world into another: to visualise worlds that aren&#8217;t usually visual. Many game engines do this: rendering all the physics polygons, or the AI&#8217;s navigation mesh, as a translucent overlay over the regular visual world. Different colours are used to indicate different properties, such as &#8220;sticky&#8221; or &#8220;only traversable by crawling agents.&#8221; Transposing the other worlds into the visual world is usually the most convenient way to work, as we can gather a large amount of precise information quickly in that world, but it&#8217;s not the only transposition available: we could use audio, too. Imagine making physics surfaces close behind the camera emit a low hum, so you can tell when the camera is backing into something without having to look at it. There are some interesting possibilities here for enabling game development for the visually impaired, too.</p>
<p>The next solution is to try to ensure that all the worlds are being furnished with data from a common source. We <i>can</i> hand-craft collision and navigation meshes, but aside from the risk of human error, this is very prone to resynchronisation &#8211; someone changes the render mesh and doesn&#8217;t update the collision mesh. Instead, we can seek to generate our collision and navigation meshes from our render meshes. Tools exist for this already: libraries like <a href="http://www.qhull.org/">QHull</a> can be used to generate simple convex hulls for collision, or more advanced tools like <a href="http://www.donyalabs.com/">Simplygon</a> can produce more accurate low-polygon meshes suitable for use in the physics world. For navigation meshes, the <a href="http://code.google.com/p/recastnavigation/">Recast</a> toolkit is excellent (and free!). Depending on your game&#8217;s needs, you&#8217;ll likely need to write some custom analysis and generation methods, but existing tech can be used to provide a lot of the infrastructure for that.</p>
<p>The most important thing, though, is to just be very clear about what the different worlds your game takes place in <i>are</i>, and which data and processes are present in each. Ensure that your processes don&#8217;t allow anyone to confuse a render mesh with a collision mesh. Ensure that your codebase doesn&#8217;t try to perform AI walkability tests to points that are on the render mesh but not on the AI mesh. And so on.</p>
<p>Understanding and embracing the segregation of processes into multiple, cohabiting game worlds can be a very useful tool for breaking down what your game is doing into manageable pieces. Once you&#8217;ve got it, you can zero in much more quickly on the causes of problems; and even, when faced with some new and difficult problem, spin off an entirely new world to contain, reduce, and resolve it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/11/19/the-many-worlds-interpretation-of-game-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I am a slave to the groove</title>
		<link>http://www.altdevblogaday.com/2011/10/21/i-am-a-slave-to-the-groove/</link>
		<comments>http://www.altdevblogaday.com/2011/10/21/i-am-a-slave-to-the-groove/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 03:52:53 +0000</pubDate>
		<dc:creator>Richard Fine</dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=19037</guid>
		<description><![CDATA[<p>I&#8217;m surprised by how quickly music can change my mood.</p>
<p>I was walking in town today, anxious. The photography class I was headed to had started almost a half hour ago; I&#8217;d just had a slightly surreal, less-than-satisfactory experience trying to buy something; I was walking, rushing, pushing past students and tourists and everyone else as I traversed the streets of Oxford to get where I was going. To both further my isolation from the moving meat obstructing my path, and to give myself a beat to march to, I put on my headphones and set my iPhone to shuffle.</p>
<p><a href="http://www.altdevblogaday.com/2011/10/21/i-am-a-slave-to-the-groove/" class="more-link">Read more on I am a slave to the groove&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m surprised by how quickly music can change my mood.</p>
<p>I was walking in town today, anxious. The photography class I was headed to had started almost a half hour ago; I&#8217;d just had a slightly surreal, less-than-satisfactory experience trying to buy something; I was walking, rushing, pushing past students and tourists and everyone else as I traversed the streets of Oxford to get where I was going. To both further my isolation from the moving meat obstructing my path, and to give myself a beat to march to, I put on my headphones and set my iPhone to shuffle.</p>
<p>Two minutes later, I was listening to Muse&#8217;s <em>Stockholm Syndrome</em>, smashing my hand around in time to the beat and mouthing the words to the chorus. And in between the choruses, I couldn&#8217;t stop smiling.</p>
<p>Why is music so effective at altering my mood?</p>
<p>I mean, it&#8217;s <i>weirdly</i> effective. Few other things can bring about such a marked change so quickly. If I&#8217;m feeling a bit blurgh, I can watch a TV show, or play a game, or eat a nice meal, and I&#8217;ll feel better&#8230; but not <i>that</i> much better. It&#8217;ll relax me, distance me from my suffering and strife, and return me to a comfortable medium &#8211; but music energizes me, commands me, like nothing else.</p>
<p>It&#8217;s not like listening to a song that I&#8217;ve listened to many times before is teaching me anything new, or achieving anything in particular &#8211; yet it can elicit a reaction as strong as discovering that I&#8217;d been accepted to Oxford Uni. An odd equality, don&#8217;t you think?</p>
<p>I find it with games, very often, as well. I loved the Ace Attorney games, for example &#8211; but the parts of the game where I&#8217;d be most excited, where I&#8217;d shut out all distractions, live and die with every right and wrong answer&#8230; was whenever <a href="http://www.youtube.com/watch?v=WHhMH3__74M">the &#8220;Cornered&#8221; theme was playing.</a> Sure, it was cool to see the witness &#8211; who we all knew was secretly the killer, but hadn&#8217;t quite proved it yet &#8211; start to sweat, to watch their story break down and their web of lies unravel, but it just wasn&#8217;t the same when playing on mute. </p>
<p>Without the music, there was no urgency, no drama. I could understand the tension on an intellectual level, I knew what was happening and what it meant for the storyline, and so on. But with the music, I was tapping through the dialogue as quickly as the characters seemed, to me, to be speaking; gasping at each reveal or new ploy by the enemy, marveling at Phoenix&#8217;s quick moves to confound them (&#8220;&#8230;unless you have evidence that this man somehow flew through the air, which I very much doubt!&#8221; &#8220;I have evidence.&#8221; &#8220;WHA-!!&#8221;). I was aware how viscerally I was responding to the game, and I was aware that it was largely the music causing it, but it didn&#8217;t matter; in that moment, I wanted that response, I wanted to put myself in the game&#8217;s hands. Had the music risen in key partway through the sequence, I suspect I would have had an orgasm. Or a heart attack. Or both.</p>
<p>Other games have had effects of similar strength, if not always directed towards the same euphoria. Introversion&#8217;s <a href="http://www.introversion.co.uk/darwinia/">Darwinia</a> has some excellent tracks by <a href="http://trash80.net">Trash80/Tresk</a> (such as <a href="http://trash80.net/content/38/faces-of-a-fashion">Faces Of A Fashion</a> and <a href="http://www.youtube.com/watch?v=qh-SLLG6rxE">Excuses</a> &#8211; the latter conveys a kind of isolated desolation that majorly frames the way I think about the game world). Terry Cavanagh&#8217;s <a href="http://thelettervsixtim.es/">VVVVVV</a> has an <a href="http://www.souleye.se/pppppp">excellent soundtrack</a>, from which tracks like <a href="http://www.youtube.com/watch?v=Tp3qiOKuEBM">Positive Force</a> and <a href="http://www.youtube.com/watch?v=Uj8MsbgpjaQ">Potential For Anything</a> tend to get me every time (Positive Force even moreso in its <a href="http://www.youtube.com/watch?v=y0jLV5DprZY">trance remix format</a>). I should note, it&#8217;s not all chiptune and indie stuff; I will always love Marty O&#8217;Donnell&#8217;s <a href="http://www.youtube.com/watch?v=Sjvo0SPAOiQ">Halo</a> theme (especially when standing in a bus, which I am then inclined to imagine is some kind of military dropship), or Stuart Chatwood&#8217;s <a href="http://www.youtube.com/watch?v=qxF6pd-bcHk">Tower of Dawn</a> from the first of the new Prince of Persia games.</p>
<p>It&#8217;s not that listening to those tracks remind me of fun experiences &#8211; they do, but I reacted to them in the same way the very first time I had them, before I&#8217;d had the experiences to be reminded of. It&#8217;s qualities of the music itself, the structures and patterns and shapes and balances, that affect me. The slow build of a rising sequence, or the way a chaotic, swirling mass of mini-themes resolve gradually into a single magnificent musical megastructure &#8211; or, alternatively, the way that structure breaks back down into chaos. It may well not even be the entire song, but just some particular moment in it where something wonderful happens.</p>
<p>It&#8217;s almost never lyrics. Even overlooking the fact that I personally am very bad at hearing what lyrics are actually being sung, I&#8217;ve never understood why people are so obsessed with lyrics. Well, that&#8217;s not true &#8211; I think I understand it, but it seems like a misplaced focus. There are interesting things that can be done with the voice as an instrument, creating particular sounds and rhythms through choice words, but beyond that, isn&#8217;t it just poetry? To listen to the lyrics, rather than the music, seems to me like ordering a steak and then only eating the salad garnish. The meaning of lyrics can enhance the music, but in my experience, that&#8217;s rare; creators who focus on interesting lyrics tend to set them to boring music.</p>
<p>(Song titles are related. I think I do enjoy the VVVVVV soundtrack a little more as a result of the optimistic, forward-looking ideas conjured up by the track titles).</p>
<p>But what puzzles me is that music is the only thing that seems to have this kind of effect on me in such a cheap and immediate manner. Short of actual life-changing events (which are hard to set up, in any case), I don&#8217;t find myself overcome with emotion when walking through an art gallery, and I tend only to respond to pathos in fiction when it&#8217;s part of the payoff for something very long-established. Music is, I think, the only thing you could use if you needed to set my mood in 30 seconds. And if you picked the right music, you&#8217;d be guaranteed success.</p>
<p>Am I alone in this? I&#8217;m not sure. There are definitely people for whom music seems to do nothing. I wonder if there&#8217;s something else they respond to instead. Or maybe they&#8217;re just dead inside.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/10/21/i-am-a-slave-to-the-groove/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending the Unity3D editor</title>
		<link>http://www.altdevblogaday.com/2011/08/22/extending-the-unity3d-editor/</link>
		<comments>http://www.altdevblogaday.com/2011/08/22/extending-the-unity3d-editor/#comments</comments>
		<pubDate>Mon, 22 Aug 2011 17:57:36 +0000</pubDate>
		<dc:creator>Richard Fine</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[UI and UX]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=14618</guid>
		<description><![CDATA[<p>One of the handy things about <a href="http://unity3d.com/">Unity3D</a> is that it&#8217;s very easy to extend the editor suite. Every game has unique requirements for tooling, and being able to build those up quickly and in a fully integrated manner can make a world of difference to your development speed. A number of pretty sophisticated packages exist that offer complex tools on top of the base Unity featureset, from visual script editors, to in-editor navigation mesh generation. The documentation for how you might build such a thing yourself, however, is a bit thin. Here&#8217;s a whirlwind tour of some of the most useful bits of info about editor customization that I&#8217;ve found in the course of my work.</p>
<p><a href="http://www.altdevblogaday.com/2011/08/22/extending-the-unity3d-editor/" class="more-link">Read more on Extending the Unity3D editor&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>One of the handy things about <a href="http://unity3d.com/">Unity3D</a> is that it&#8217;s very easy to extend the editor suite. Every game has unique requirements for tooling, and being able to build those up quickly and in a fully integrated manner can make a world of difference to your development speed. A number of pretty sophisticated packages exist that offer complex tools on top of the base Unity featureset, from visual script editors, to in-editor navigation mesh generation. The documentation for how you might build such a thing yourself, however, is a bit thin. Here&#8217;s a whirlwind tour of some of the most useful bits of info about editor customization that I&#8217;ve found in the course of my work.</p>
<h4>How Editor scripts are built</h4>
<div id="attachment_14781" class="wp-caption alignright" style="width: 327px"><img src="http://altdevblogaday.com/wp-content/uploads/2011/08/unity-projectlist.png" alt="" width="317" height="318" class="size-full wp-image-14781" /><p class="wp-caption-text">The different projects generated by Unity 3.4. The assets folder in each project is the same physical folder on disk; each project includes different files within it. Note that this only shows the C# projects - if you&#039;re using UnityScript, you get another four.</p></div>
<p>Because you don&#8217;t want all your editor customization to be included in the game that you ship, and because you don&#8217;t want your shipping game to have any dependencies on things in the Unity editor, Unity keeps runtime and editor code in separate assemblies. <a href="http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html">The compilation order</a> is such that runtime code is compiled before editor code, so that your editor classes can safely refer to runtime components (otherwise it&#8217;d be difficult to edit them) &#8211; but it does mean that your runtime components can&#8217;t reference any of your editor code. You have to maintain a strict layering. Unity&#8217;s gotten a bit more explicit about this in 3.4 &#8211; now the project files it generates (for VS/MonoDevelop) clearly correspond to the four compilation stages it provides, so there&#8217;s no confusion about which files will get build at which times.</p>
<p>The documentation is a little unclear on one particular point. When I first started, I thought that I had to have a single &#8216;Editor&#8217; folder at the top of my project, and that all editor classes had to go inside it. The system&#8217;s actually a bit more flexible than that; you can have as many &#8216;Editor&#8217; folders as you want in your project, buried wherever you like inside your Assets folder, and all of them can contain editor code. So now it&#8217;s very common that I&#8217;ll have a folder for one particular feature (e.g. &#8216;AI&#8217;) that contains all the components for that feature, with an Editor folder alongside (e.g. &#8216;AI/Editor&#8217;) that contains the editor extensions for working with those components.</p>
<p>As far as Unity&#8217;s built-in types go, runtime types all live in the UnityEngine namespace (in the UnityEngine assembly), while editor types all live in the UnityEditor namespace (in the UnityEditor assembly).</p>
<h4>The UnityEditor.Editor class</h4>
<p>By far the most common kind of customization I set up is a custom inspector. Unity&#8217;s Inspector panel provides your window into a component&#8217;s state, but in its base form it only understands a limited set of types, and will only expose public fields (no properties). Custom inspectors give you the opportunity to completely control how users view and edit your components; for example, they let you display read-only properties, enforce value constraints, or just change the way an option is presented &#8211; for example, replacing a [0..1] float field with a percentage slider.</p>
<p>Inspectors in Unity are all subclasses of the <a href="http://unity3d.com/support/documentation/ScriptReference/Editor.html">Editor</a> class, so that&#8217;s where you start. One thing I don&#8217;t like about the editor class, though, is the way it handles types: it has a &#8216;target&#8217; member that refers to the object the inspector is editing, but it&#8217;s of the base &#8216;Object&#8217; type, so you keep on having to cast it to a more useful type. To get around this I use a very simple generic class:</p>
<p><code><br />
public class InspectorBase&lt;T&gt; : Editor where T : UnityEngine.Object<br />
{<br />
   protected T Target { get { return (T) target; } }<br />
}<br />
</code></p>
<p>Now, if I want to create an inspector for MyCustomComponent, I can derive the inspector from InspectorBase&lt;MyCustomComponent&gt;, and use the &#8216;Target&#8217; member instead of the untyped &#8216;target&#8217; member, and I don&#8217;t have to keep casting everywhere.</p>
<p>Note that you also need to attach the <code>[<a href="http://unity3d.com/support/documentation/ScriptReference/CustomEditor.html">CustomEditor</a>]</code> attribute to your inspector classes for Unity to actually pick them up and use them.</p>
<h4>Editor GUI</h4>
<p>Once you&#8217;ve created your custom inspector, the method you usually want to implement is <a href="http://unity3d.com/support/documentation/ScriptReference/Editor.OnInspectorGUI.html">OnInspectorGUI()</a>. OnInspectorGUI() is responsible for specifying everything shown in the inspector, using Unity&#8217;s immediate-mode GUI system. Because this is editor code, we can use <a href="http://unity3d.com/support/documentation/ScriptReference/20_class_hierarchy.Editor_Classes.html">the types in the UnityEditor namespace</a>, which includes <a href="http://unity3d.com/support/documentation/ScriptReference/EditorGUILayout.html">EditorGUILayout</a>. EditorGUILayout provides a bunch of really simple controls for use in the editor, over and above what Unity&#8217;s regular runtime GUI system offers. For example, say I want to show the user a field for entering a 3D position. I could use EditorGUILayout.Vector3Field():</p>
<p><code>Target.somePosition = EditorGUILayout.Vector3Field("Some position", Target.somePosition);</code></p>
<p>This results in a line in your inspector that looks like this:</p>
<p><img src="http://altdevblogaday.com/wp-content/uploads/2011/08/unity-vec3field.png" width="289" height="49" class="aligncenter size-full wp-image-14777" /></p>
<p>The immediate-mode GUI system works such that if I change the values in the UI, Vector3Field will return the new values, and Target.somePosition gets updated. You&#8217;re free to manipulate the value &#8211; for example, clamping it to a range &#8211; before assigning it to the target; and you&#8217;re free to ignore the return value completely (which would effectively make the field read-only). The values don&#8217;t have to be coming to/from fields, either &#8211; you can expose properties in the inspector by adding lines for them in this way, or call a function to get the current value and another function to save it, whatever you like.</p>
<p>Of course, Unity does this stuff by default for any public members. If you just want to build on top of what Unity&#8217;s already showing, you don&#8217;t have to reimplement all those fields &#8211; Editor has a <a href="http://unity3d.com/support/documentation/ScriptReference/Editor.DrawDefaultInspector.html">DrawDefaultInspector()</a> method that tells Unity to draw all the controls it would usually draw, but when it&#8217;s finished, you&#8217;ve still got the opportunity to add a few extra fields and buttons yourself.</p>
<p>Speaking of buttons&#8230; EditorGUILayout is pretty comprehensive, but you might notice that there are some things missing &#8211; what if I want to put a &#8220;recalculate&#8221; button on my navigation mesh component, for example? The trick is that EditorGUILayout is still built on top of the regular runtime <a href="http://unity3d.com/support/documentation/ScriptReference/GUILayout.html">GUILayout</a>, so everything in GUILayout is available to you as well.</p>
<p>As you make changes to the fields in the inspector, and assign new values to the fields of your target object, Unity detects that you&#8217;re changing the object and flags it as &#8216;dirty&#8217; so that it will be written out to disk the next time you save the scene or project. This detection is limited: it only picks up direct assignment to public properties. If you&#8217;re changing the target object through properties or through calling methods on it, you may need to call <a href="http://unity3d.com/support/documentation/ScriptReference/EditorUtility.SetDirty.html">EditorUtility.SetDirty</a> yourself.</p>
<h4>Extending the component context menu</h4>
<p>It&#8217;s often useful to be able to manually trigger certain behavior when testing things out. You could do this by putting a button on the custom inspector that triggers the behavior:</p>
<p><code><br />
if(GUILayout.Button("Explode now!")) Target.ExplodeNow();<br />
</code></p>
<p>but there&#8217;s an even easier way &#8211; one that doesn&#8217;t require a custom inspector at all. What you can do instead is use the <a href="http://unity3d.com/support/documentation/ScriptReference/ContextMenu.html">UnityEngine.ContextMenu attribute</a>:</p>
<p><code><br />
/* In the target class... */<br />
[ContextMenu("Explode now!")]<br />
public void ExplodeNow() { ... }<br />
</code></p>
<p>Right-clicking in the component&#8217;s inspector &#8211; whether you&#8217;ve customized it or not &#8211; will then show you a context menu, with the extra item in there. Very handy for quickly rigging things up for testing.</p>
<h4>Extending the main menus</h4>
<p>Everything I&#8217;ve talked about so far has been about customization centered around a particular component. What about other kinds of extension, like general utilities?</p>
<p>The animation system in my game stores its assets in a folder structure, such that each folder corresponds to one entry in an enum. When I change the enum, it&#8217;s useful to be able to synchronize that folder structure, adding any missing folders and deleting any obsolete ones. So I&#8217;ve got this class, with a simple method:</p>
<p><code><br />
public class AnimationSystem<br />
{</p>
<p>public static void SyncFolderStructure() { ... }</p>
<p>}<br />
</code></p>
<p>But how and when do I call it? What I&#8217;ve done is to wire it up to a menu item in the Assets menu, using the <a href="http://unity3d.com/support/documentation/ScriptReference/MenuItem.html">MenuItem attribute</a>:<br />
<code><br />
[MenuItem("Assets/Sync folder structure")]<br />
public static void SyncFolderStructure() { ... }<br />
</code></p>
<p>Clicking the menu item calls the function. Note that the function needs to be static &#8211; but the class it&#8217;s in can be anything, and can derive from anything (including nothing).</p>
<h4>Wizards</h4>
<p>Editor GUI elements don&#8217;t only have to live inside the Inspector. It&#8217;s also possible to create arbitrary editor windows, that can be moved and docked like any of Unity&#8217;s built-in windows, and that are populated using GUI commands just like in an inspector. One of the simplest ways of doing this is to use a <a href="http://unity3d.com/support/documentation/ScriptReference/ScriptableWizard.html">ScriptableWizard</a>, which is like a dialogue box &#8211; you display it, set some values in it, then hit a button to make it work its magic.</p>
<div id="attachment_14837" class="wp-caption aligncenter" style="width: 366px"><img src="http://altdevblogaday.com/wp-content/uploads/2011/08/unity-ragdollwizard.png" alt="" width="356" height="289" class="size-full wp-image-14837" /><p class="wp-caption-text">Unity&#039;s built-in Create Ragdoll wizard (GameObject-&gt;Create Other-&gt;Ragdoll). You drag and drop the relevant bones and bits into the slots in the window, and then hit the &#039;create&#039; button to have it rig your ragdoll with rigidbodies and joints.</p></div>
<p>ScriptableWizard, by default, works almost like an inspector: any public fields in your derived class will be automatically shown in the wizard window. Your wizard might be as simple as a bunch of public fields, plus an OnWizardCreate() method, which Unity will call when the user hits the &#8216;Create&#8217; button. Worth noting that you can change the text on that button, too, if &#8216;Apply&#8217; or &#8216;OK&#8217; or similar would be more intuitive.</p>
<p>The only other aspect of the wizard is deciding how the user will launch it; the usual approach is to use a menu item bound to a static function, as shown above:</p>
<p><code><br />
[MenuItem("GameObject/Create Other/Explosion")]<br />
public static void CreateExplosion()<br />
{<br />
   ScriptableWizard.DisplayWizard&lt;ExplosionWizard&gt;("Create explosion");<br />
}<br />
</code></p>
<h4>Conclusion</h4>
<p>There&#8217;s loads more &#8211; I&#8217;ve not even touched on custom asset types yet, or asset import postprocessors, or scene view customization. They&#8217;ll wait for another day&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/08/22/extending-the-unity3d-editor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When they said &#8220;numbers don&#8217;t lie,&#8221; they were lying</title>
		<link>http://www.altdevblogaday.com/2011/07/23/lying-numbers/</link>
		<comments>http://www.altdevblogaday.com/2011/07/23/lying-numbers/#comments</comments>
		<pubDate>Sat, 23 Jul 2011 02:07:52 +0000</pubDate>
		<dc:creator>Richard Fine</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=12227</guid>
		<description><![CDATA[<p>Just a quick one this fortnight, I&#8217;ve not got much time to spare&#8230;</p>
<p>The big, unannounced project I&#8217;m working on is reaching a point where most of the first-pass features are in, and now we need the game to run at an acceptable speed. It turns out that trying to render 8 million normal-mapped triangles 60 times a second on a GeForce 8800 is asking a little much of the poor thing. Who knew?</p>
<p><a href="http://www.altdevblogaday.com/2011/07/23/lying-numbers/" class="more-link">Read more on When they said &#8220;numbers don&#8217;t lie,&#8221; they were lying&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Just a quick one this fortnight, I&#8217;ve not got much time to spare&#8230;</p>
<p>The big, unannounced project I&#8217;m working on is reaching a point where most of the first-pass features are in, and now we need the game to run at an acceptable speed. It turns out that trying to render 8 million normal-mapped triangles 60 times a second on a GeForce 8800 is asking a little much of the poor thing. Who knew?</p>
<div id="attachment_12232" class="wp-caption aligncenter" style="width: 610px"><img src="http://altdevblogaday.com/wp-content/uploads/2011/07/emmett-300x166.jpg" alt="" width="600" height="332" class="size-medium wp-image-12232" /><p class="wp-caption-text">Rendering 8 million triangles at 60Hz on a GeForce 8800 requires 1.21 jiggawatts of power.</p></div>
<p>So, a large chunk of the past couple of weeks has been spent getting various performance-improving systems in place: a level-of-detail system, some simple area-based culling, and so on. Unity&#8217;s built-in profiling tools range from &#8220;rudimentary&#8221; to &#8220;undocumented,&#8221; so I&#8217;ve supplemented them with some of my own &#8211; the most useful of which, so far, has been a simple tool that generates a list of all the meshes presently being rendered in the scene, ordered by their total contribution to the polygon count. Very helpful for telling us where to focus our art optimization efforts.</p>
<p>One of the things my polygon-counter tool doesn&#8217;t do is generate percentages.</p>
<p>It would be easy to add, but I consciously decided against it, because it&#8217;s very easy to be misled by them. I&#8217;d have thought that most people in game development know how percentages work, but it seems to happen all the time when people look over my shoulder at the profiler graph: &#8220;Hey, physics is using 30% of your frame time &#8211; it was only using 10% when I looked a few hours ago. Did you break something?&#8221; </p>
<p>&#8230;</p>
<div id="attachment_12235" class="wp-caption aligncenter" style="width: 310px"><img src="http://altdevblogaday.com/wp-content/uploads/2011/07/wolverine-not-impressed-1024x768-300x225.jpg" alt="" width="300" height="225" class="size-medium wp-image-12235" /><p class="wp-caption-text">No, I optimized the renderer, dropping the total frame time from 20ms to 6.6ms. The physics is taking 2ms just like it was before. Kthx.</p></div>
<p>If you&#8217;re focusing your optimization on decreasing the percentage that a particular component takes up, then you can always achieve that by making everything <i>else</i> slower&#8230; whenever you&#8217;re dealing with a percentage, you need to remember to ask: A percentage of <i>what</i>? And whenever you&#8217;re comparing two percentages, you need to remember to ask: Are they percentages of the same thing?</p>
<p>Frames-per-second values are similarly deceptive, because the same piece of work will have a different effect on the number at different points. (Also, shouldn&#8217;t they be measured in Hertz (Hz)? Physics, people!) </p>
<p>Say I&#8217;m running at 30FPS in a development build, and I&#8217;ve written a new feature. I turn it on, and the framerate drops to 28FPS &#8211; that&#8217;s not so bad, right? Only 2 frames per second dropped? Why, that&#8217;s only 6% of the original framerate&#8230;</p>
<p>Later on in the project, though, when the art&#8217;s been optimized and we&#8217;re running at more like 80FPS, I turn the feature on again &#8211; and this time the framerate drops from 80FPS to 67FPS. Whoa! 13FPS drop! That&#8217;s more than six times what it was before. Is the code slower?</p>
<p>The code&#8217;s taking exactly the same amount of time (2.38ms per frame). Most processes in a game will take the same amount of time regardless of what your framerate is; what determines their time is how complex they are and how much data they&#8217;ve got to process, <i>not</i> how fast the overall system is. And because the FPS figure is the reciprocal of the frame time (i.e. it&#8217;s 1/{frame time in seconds}), adding or removing milliseconds at a low FPS will produce a much smaller change than adding or removing milliseconds at a high FPS.</p>
<p>If you want your game to run at a solid framerate of X frames per second, then you&#8217;ve got 1000/X seconds to burn each frame. Here&#8217;s a handy table for you:</p>
<p><img src="http://altdevblogaday.com/wp-content/uploads/2011/07/Handy_Table-214x300.jpg" alt="" width="214" height="300" class="aligncenter size-medium wp-image-12238" /></p>
<p>No, wait, sorry, my bad. (Seriously, Google Images? That&#8217;s the handiest table you could give me?)</p>
<table border="1" cellpadding="2" cellspacing="2">
<tr>
<td>Desired framerate</td>
<td>Milliseconds to burn</td>
<td>Hz at 1ms under</td>
<td>Hz at 1ms over</td>
</tr>
<tr>
<td>30Hz</td>
<td>33.3ms</td>
<td>31Hz</td>
<td>29Hz</td>
</tr>
<tr>
<td>50Hz</td>
<td>20ms</td>
<td>52Hz</td>
<td>47Hz</td>
</tr>
<tr>
<td>60Hz</td>
<td>16.6ms</td>
<td>64Hz</td>
<td>57Hz</td>
</tr>
<tr>
<td>100Hz</td>
<td>10ms</td>
<td>111Hz</td>
<td>90Hz</td>
</tr>
</table>
<p>See how adding or removing a single millisecond to the per-frame workload makes a much bigger difference to a 100Hz framerate than to a 30Hz framerate?</p>
<p>Now, pick a target framerate, write down how many milliseconds per frame you have accordingly, and then <b>forget about the framerate.</b> Focus on concrete time. The framerate lies to you, but concrete&#8217;s always been supportive.</p>
<p>This is, of course, all quite apart from the fact that in modern multicore games, it may not even make <i>sense</i> to be talking about one single &#8220;framerate.&#8221; You&#8217;ve got to start separating it out into render frames, physics frames, input frames&#8230;</p>
<p>So, anyway, those are two numbers that deceive you: profiler percentages and framerates. Anyone got other frequently-misinterpreted, pathologically misleading, or <i>just plain wrong</i> numbers that they&#8217;d like to share with the class?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/07/23/lying-numbers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity3D coroutines in detail</title>
		<link>http://www.altdevblogaday.com/2011/07/07/unity3d-coroutines-in-detail/</link>
		<comments>http://www.altdevblogaday.com/2011/07/07/unity3d-coroutines-in-detail/#comments</comments>
		<pubDate>Thu, 07 Jul 2011 11:36:47 +0000</pubDate>
		<dc:creator>Richard Fine</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=10667</guid>
		<description><![CDATA[<p>Many processes in games take place over the course of multiple frames. You&#8217;ve got &#8216;dense&#8217; processes, like pathfinding, which work hard each frame but get split across multiple frames so as not to impact the framerate too heavily. You&#8217;ve got &#8216;sparse&#8217; processes, like gameplay triggers, that do nothing most frames, but occasionally are called upon to do critical work. And you&#8217;ve got assorted processes between the two.</p>
<p><a href="http://www.altdevblogaday.com/2011/07/07/unity3d-coroutines-in-detail/" class="more-link">Read more on Unity3D coroutines in detail&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Many processes in games take place over the course of multiple frames. You&#8217;ve got &#8216;dense&#8217; processes, like pathfinding, which work hard each frame but get split across multiple frames so as not to impact the framerate too heavily. You&#8217;ve got &#8216;sparse&#8217; processes, like gameplay triggers, that do nothing most frames, but occasionally are called upon to do critical work. And you&#8217;ve got assorted processes between the two.</p>
<p>Whenever you&#8217;re creating a process that will take place over multiple frames &#8211; without multithreading &#8211; you need to find some way of breaking the work up into chunks that can be run one-per-frame. For any algorithm with a central loop, it&#8217;s fairly obvious: an A* pathfinder, for example, can be structured such that it maintains its node lists semi-permanently, processing only a handful of nodes from the open list each frame, instead of trying to do all the work in one go. There&#8217;s some balancing to be done to manage latency &#8211; after all, if you&#8217;re locking your framerate at 60 or 30 frames per second, then your process will only take 60 or 30 steps per second, and that might cause the process to just take too long overall. A neat design might offer the smallest possible unit of work at one level &#8211; e.g. process a single A* node &#8211; and layer on top a way of grouping work together into larger chunks &#8211; e.g. keep processing A* nodes for X milliseconds. (Some people call this &#8216;timeslicing&#8217;, though I don&#8217;t).</p>
<p>Still, allowing the work to be broken up in this way means you have to transfer state from one frame to the next. If you&#8217;re breaking an iterative algorithm up, then you&#8217;ve got to preserve all the state shared across iterations, as well as a means of tracking which iteration is to be performed next. That&#8217;s not usually too bad &#8211; the design of an &#8216;A* pathfinder class&#8217; is fairly obvious &#8211; but there are other cases, too, that are less pleasant. Sometimes you&#8217;ll be facing long computations that are doing different kinds of work from frame to frame; the object capturing their state can end up with a big mess of semi-useful &#8216;locals,&#8217; kept for passing data from one frame to the next. And if you&#8217;re dealing with a sparse process, you often end up having to implement a small state machine just to track when work should be done at all.</p>
<p>Wouldn&#8217;t it be neat if, instead of having to explicitly track all this state across multiple frames, and instead of having to multithread and manage synchronization and locking and so on, you could just write your function as a single chunk of code, and mark particular places where the function should &#8216;pause&#8217; and carry on at a later time?</p>
<p>Unity &#8211; along with a number of other environments and languages &#8211; provides this in the form of Coroutines.</p>
<h2>How do they look?</h2>
<p>In &#8220;Unityscript&#8221; (Javascript):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> LongComputation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>someCondition<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #009966; font-style: italic;">/* Do a chunk of work */</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Pause here and carry on next frame</span>
        yield<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In C#:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">IEnumerator LongComputation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span>someCondition<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">/* Do a chunk of work */</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Pause here and carry on next frame</span>
        <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h2>How do they work?</h2>
<p>Let me just say, quickly, that I don&#8217;t work for Unity Technologies. I&#8217;ve not seen the Unity source code. I&#8217;ve never seen the guts of Unity&#8217;s coroutine engine. However, if they&#8217;ve implemented it in a way that is radically different from what I&#8217;m about to describe, then I&#8217;ll be quite surprised. If anyone from UT wants to chime in and talk about how it <i>actually</i> works, then that&#8217;d be great.</p>
<p>The big clues are in the C# version. Firstly, note that the return type for the function is <code>IEnumerator</code>. And secondly, note that one of the statements is <code>yield return</code>. This means that <code>yield</code> must be a keyword, and as Unity&#8217;s C# support is vanilla C# 3.5, it must be a vanilla C# 3.5 keyword. Indeed, <a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0(v=vs.80).aspx">here it is in MSDN</a> &#8211; talking about something called &#8216;iterator blocks.&#8217; So what&#8217;s going on?</p>
<p>Firstly, there&#8217;s this IEnumerator type. The IEnumerator type acts like a cursor over a sequence, providing two significant members: <code>Current</code>, which is a property giving you the element the cursor is presently over, and <code>MoveNext()</code>, a function that moves to the next element in the sequence. Because <code>IEnumerator</code> is an interface, it doesn&#8217;t specify exactly how these members are implemented; <code>MoveNext()</code> could just add one to <code>Current</code>, or it could load the new value from a file, or it could download an image from the Internet and hash it and store the new hash in <code>Current</code>&#8230; or it could even do one thing for the first element in the sequence, and something entirely different for the second. You could even use it to generate an infinite sequence if you so desired. <code>MoveNext()</code> calculates the next value in the sequence (returning false if there are no more values), and <code>Current</code> retrieves the value it calculated.</p>
<p>Ordinarily, if you wanted to implement an interface, you&#8217;d have to write a class, implement the members, and so on. Iterator blocks are a convenient way of implementing <code>IEnumerator</code> <i>without</i> all that hassle &#8211; you just follow a few rules, and the <code>IEnumerator</code> implementation is generated automatically by the compiler.</p>
<p>An iterator block is a regular function that (a) returns <code>IEnumerator</code>, and (b) uses the <code>yield</code> keyword. So what does the yield keyword actually <i>do</i>? It declares what the next value in the sequence is &#8211; or that there are no more values. The point at which the code encounters a <code>yield return X</code> or <code>yield break</code> is the point at which <code>IEnumerator.MoveNext()</code> should stop; a <code>yield return X</code> causes <code>MoveNext()</code> to return <code>true</code> and <code>Current</code> to be assigned the value <code>X</code>, while a <code>yield break</code> causes <code>MoveNext()</code> to return <code>false</code>.</p>
<p>Now, here&#8217;s the trick. It doesn&#8217;t have to matter what the actual values returned by the sequence are. You can call <code>MoveNext()</code> repeatly, and ignore <code>Current</code>; the computations will still be performed. Each time MoveNext() is called, your iterator block runs to the next &#8216;yield&#8217; statement, regardless of what expression it actually yields. So you can write something like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">IEnumerator TellMeASecret<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  PlayAnimation<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;LeanInConspiratorially&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span>playingAnimation<span style="color: #008000;">&#41;</span>
    <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
  Say<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;I stole the cookie from the cookie jar!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span>speaking<span style="color: #008000;">&#41;</span>
    <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
  PlayAnimation<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;LeanOutRelieved&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span>playingAnimation<span style="color: #008000;">&#41;</span>
    <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>and what you&#8217;ve actually <i>written</i> is an iterator block that generates a long sequence of <code>null</code> values, but what&#8217;s significant is the side-effects of the work it does to calculate them. You could run this coroutine using a simple loop like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">IEnumerator e <span style="color: #008000;">=</span> TellMeASecret<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">MoveNext</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Or, more usefully, you could mix it in with other work:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">IEnumerator e <span style="color: #008000;">=</span> TellMeASecret<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">MoveNext</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span> 
  <span style="color: #008080; font-style: italic;">// If they press 'Escape', skip the cutscene</span>
  <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>Input<span style="color: #008000;">.</span><span style="color: #0000FF;">GetKeyDown</span><span style="color: #008000;">&#40;</span>KeyCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Escape</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h2>It&#8217;s all in the timing</h2>
<p>As you&#8217;ve seen, each <code>yield return</code> statement must provide an expression (like <code>null</code>) so that the iterator block has something to actually assign to <code>IEnumerator.Current</code>. A long sequence of nulls isn&#8217;t exactly useful, but we&#8217;re more interested in the side-effects. Aren&#8217;t we?</p>
<p>There&#8217;s something handy we can do with that expression, actually. What if, instead of just yielding <code>null </code>and ignoring it, we yielded something that indicated when we expect to need to do more work? Often we&#8217;ll need to carry straight on the next frame, sure, but not always: there will be plenty of times where we want to carry on after an animation or sound has finished playing, or after a particular amount of time has passed. Those <code>while(playingAnimation) yield return null;</code> constructs are bit tedious, don&#8217;t you think?</p>
<p>Unity declares the <code>YieldInstruction</code> base type, and provides a few concrete derived types that indicate particular kinds of wait. You&#8217;ve got <code>WaitForSeconds</code>, which resumes the coroutine after the designated amount of time has passed. You&#8217;ve got <code>WaitForEndOfFrame</code>, which resumes the coroutine at a particular point later in the same frame. You&#8217;ve got the <code>Coroutine</code> type itself, which, when coroutine A yields coroutine B, pauses coroutine A until after coroutine B has finished.</p>
<p>What does this look like from a runtime point of view? As I said, I don&#8217;t work for Unity, so I&#8217;ve never seen their code; but I&#8217;d imagine it might look a little bit like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span>IEnumerator<span style="color: #008000;">&gt;</span> unblockedCoroutines<span style="color: #008000;">;</span>
List<span style="color: #008000;">&lt;</span>IEnumerator<span style="color: #008000;">&gt;</span> shouldRunNextFrame<span style="color: #008000;">;</span>
List<span style="color: #008000;">&lt;</span>IEnumerator<span style="color: #008000;">&gt;</span> shouldRunAtEndOfFrame<span style="color: #008000;">;</span>
SortedList<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">float</span>, IEnumerator<span style="color: #008000;">&gt;</span> shouldRunAfterTimes<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span>IEnumerator coroutine <span style="color: #0600FF; font-weight: bold;">in</span> unblockedCoroutines<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>coroutine<span style="color: #008000;">.</span><span style="color: #0000FF;">MoveNext</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008080; font-style: italic;">// This coroutine has finished</span>
        <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>coroutine<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span> <span style="color: #008000;">is</span> YieldInstruction<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// This coroutine yielded null, or some other value we don't understand; run it next frame.</span>
        shouldRunNextFrame<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>coroutine<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>coroutine<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span> <span style="color: #008000;">is</span> WaitForSeconds<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        WaitForSeconds wait <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>WaitForSeconds<span style="color: #008000;">&#41;</span>coroutine<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">;</span>
        shouldRunAfterTimes<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>Time<span style="color: #008000;">.</span><span style="color: #0000FF;">time</span> <span style="color: #008000;">+</span> wait<span style="color: #008000;">.</span><span style="color: #0000FF;">duration</span>, coroutine<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>coroutine<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span> <span style="color: #008000;">is</span> WaitForEndOfFrame<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        shouldRunAtEndOfFrame<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>coroutine<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008080; font-style: italic;">/* similar stuff for other YieldInstruction subtypes */</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
unblockedCoroutines <span style="color: #008000;">=</span> shouldRunNextFrame<span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>It&#8217;s not difficult to imagine how more <code>YieldInstruction</code> subtypes could be added to handle other cases &#8211; engine-level support for signals, for example, could be added, with a <code>WaitForSignal("SignalName")</code> <code>YieldInstruction</code> supporting it. By adding more <code>YieldInstruction</code>s, the coroutines themselves can become more expressive &#8211; <code>yield return new WaitForSignal("GameOver")</code> is nicer to read than <code>while(!Signals.HasFired("GameOver")) yield return null</code>, if you ask me, quite apart from the fact that doing it in the engine could be faster than doing it in script.</p>
<h2>A couple of non-obvious ramifications</h2>
<p>There&#8217;s a couple of useful things about all this that people sometimes miss that I thought I should point out.</p>
<p>Firstly, <code>yield return</code> is just yielding an expression &#8211; any expression &#8211; and <code>YieldInstruction</code> is a regular type. This means you can do things like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">YieldInstruction y<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>something<span style="color: #008000;">&#41;</span>
 y <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>somethingElse<span style="color: #008000;">&#41;</span>
 y <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WaitForEndOfFrame<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">else</span>
 y <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WaitForSeconds<span style="color: #008000;">&#40;</span>1<span style="color: #008000;">.</span>0f<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> y<span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The specific lines <code>yield return new WaitForSeconds()</code>, <code>yield return new WaitForEndOfFrame()</code>, etc, are common, but they&#8217;re not actually special forms in their own right.</p>
<p>Secondly, because these coroutines are just iterator blocks, you can iterate over them yourself if you want &#8211; you don&#8217;t have to have the engine do it for you. I&#8217;ve used this for adding interrupt conditions to a coroutine before:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">IEnumerator DoSomething<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">/* ... */</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
IEnumerator DoSomethingUnlessInterrupted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  IEnumerator e <span style="color: #008000;">=</span> DoSomething<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">bool</span> interrupted <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>interrupted<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">MoveNext</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">;</span>
    interrupted <span style="color: #008000;">=</span> HasBeenInterrupted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Thirdly, the fact that you can yield on other coroutines can sort of allow you to implement your own YieldInstructions, albeit not as performantly as if they were implemented by the engine. For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">IEnumerator UntilTrueCoroutine<span style="color: #008000;">&#40;</span>Func fn<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>fn<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Coroutine UntilTrue<span style="color: #008000;">&#40;</span>Func fn<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> StartCoroutine<span style="color: #008000;">&#40;</span>UntilTrueCoroutine<span style="color: #008000;">&#40;</span>fn<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
IEnumerator SomeTask<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">/* ... */</span>
  <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> UntilTrue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> _lives <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008080; font-style: italic;">/* ... */</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>however, I wouldn&#8217;t really recommend this &#8211; the cost of starting a Coroutine is a little heavy for my liking.</p>
<h2>Conclusion</h2>
<p>I hope this clarifies a little some of what&#8217;s really happening when you use a Coroutine in Unity. C#&#8217;s iterator blocks are a groovy little construct, and even if you&#8217;re not using Unity, maybe you&#8217;ll find it useful to take advantage of them in the same way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/07/07/unity3d-coroutines-in-detail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Designers are Descriptive; Programmers are Procedural</title>
		<link>http://www.altdevblogaday.com/2011/06/07/designers-are-descriptive-programmers-are-procedural/</link>
		<comments>http://www.altdevblogaday.com/2011/06/07/designers-are-descriptive-programmers-are-procedural/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 19:42:48 +0000</pubDate>
		<dc:creator>Richard Fine</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://altdevblogaday.org/?p=7694</guid>
		<description><![CDATA[TL;DR version: Programmers and designers have different habits for thinking about problems. When programmers create tools that designers will use, they should present those tools in ‘design language’ – the language of the end-user experience – rather than just exposing the flags and values that the implementation works with. It’s better for them to do that, than for the designers to learn how to manipulate the implementation, because figuring out how to set the implementation for a particular design goal is what the programmers are good at.]]></description>
			<content:encoded><![CDATA[<blockquote><p>A designer, a programmer, and a producer walk into a cocktail bar. The barman asks, &#8220;What can I get for you?&#8221;</p>
<p>The designer says, &#8220;Give me something fruity, with an umbrella in. No gin though, it makes me violent.&#8221;</p>
<p>The programmer says, &#8220;Mix two parts vodka with one part white rum, shake it with ice. Pour it into a glass with strawberry syrup, and top up with sprite. Stick an umbrella in the top and hand it to me.&#8221;</p>
<p>The producer, who only came along because they needed a third person to make up the joke, sighs and says, &#8220;I&#8217;ll have what these guys are having.&#8221;</p>
<p>The bartender begins making the drinks, &#8220;OK, three Equivalent Expressions, coming up&#8230;&#8221;</p></blockquote>
<h3>Anyway, folks&#8230;</h3>
<p>There&#8217;s a fundamental difference between the kind of problems that designers and programmers spend their time solving, and as such, I think there&#8217;s a difference in their habitual approach to solving problems, and the way they structure and explain their solutions.</p>
<p>Designers are fundamentally interested in the properties of the end result &#8211; the visible &#8216;outer shell&#8217; of the product. They don&#8217;t care about the technology inside it, beyond the observable effects (like it being fast enough, not crashing, etc). Their descriptions of the product, and how it should be, are thus all written in terms of these visible properties: which things the player experiences, when and where the player experiences them, and so on.</p>
<p>Programmers, by contrast, are focused on the internals of the product &#8211; they are tasked with filling in the gap between the outer shell provided by the designers, and the solid core of functionality provided by the platform. It&#8217;s like a translation process &#8211; translating high-level game concepts into low-level hardware operations &#8211; and so their explanations are in similar terms: which lower-level concepts are in use, how they are combined into large structures, and so on.</p>
<h3>Speaking in tongues</h3>
<p>This mismatch in modes of thinking requires extra effort to &#8216;bridge the gap.&#8217; However, it&#8217;s not symmetrical.</p>
<p>As we&#8217;ve said, the entire focus of a programmer is on constructing these onion-layers of high-level to low-level concepts, composing and decomposing functionality to create the desired result. Connecting concepts between different layers of abstraction <em>is</em> programming. So when a designer talks to a programmer in the highest-level terms, mapping those terms down to the level that the programmer is working at is employing the programmer&#8217;s core skill.</p>
<p>The same is not so true for designers. Taking information about the implementation, and deducing from that the visible effects and constraints upon the outer shell, is not actually a part of design. Design is making decisions about the outer shell, within the constraints imposed by the implementation; figuring out what those constraints <em>are</em> is something different. Good designers <em>can do</em> it, of course, but it&#8217;s not their core skill.</p>
<p>To put this in slightly more concrete terms: When a designer tells a programmer that he wants a particular enemy to shout something when he sees the player, figuring out the points in the code that will need to be modified to support that is what programming&#8217;s all about. But when a programmer tells a designer that he&#8217;s added sound hooks to the base enemy class upon selecting a new object of interest, figuring out what this will actually allow the designer to do is <em>not</em> what design&#8217;s all about; it&#8217;s deciding which enemies should play sounds, and which sounds, that employs their core skill.</p>
<p>Another example, from outside the games industry, is CSS. Of all the designers I know who&#8217;ve done work with CSS, there are a few particular parts that they <em>always</em> have trouble with &#8211; display, position, and float. Together, these properties are extremely powerful for producing fluid layouts &#8211; but using them the way you want requires that you stop thinking like a visual designer, and start thinking like a web page renderer layout system. First this box will be laid out, <em>then</em> this one will be floated to the left inside it, <em>then</em> this other one will be positioned relative to it&#8230; it&#8217;s a procedural combination of pieces, compensating for the fact that CSS is lacking some useful layout concepts, like built-in support for columns. Knowledge of how to achieve things like an equal-height 3-column layout is passed around as a bit of arcane &#8216;magic&#8217; that designers just learn and apply, understanding it only in as far as is necessary to use it.</p>
<p>So, I think that when it comes to interfacing design and programming, it would be most efficient to get the programmers to learn to &#8216;speak designer,&#8217; rather than getting the designers to learn to &#8216;speak programmer.&#8217; It&#8217;s a better match to their core skills, to their habitual modes of thinking.</p>
<h3>Designing for designers</h3>
<p>Most established programmers already do this in direct communication, I think. When explaining a new system to the design team, you <em>don&#8217;t</em> tell them all the class names and algorithms used; you tell them about the features and behaviors that they will use, phrased in their terms, the terms of the end user&#8217;s experience. Sure, sometimes you slip up &#8211; be it through absent-mindedly forgetting that you&#8217;re not talking to fellow programmers, or through enthusiasm about the elegance of what you built, or whatever &#8211; but even then, the designers usually speak enough Programmer to follow along.</p>
<p>Where I think this happens less is in tool design.</p>
<p>Internal tools frequently present their concepts and functionality in implementation terms, rather than in end-user-experience terms. Designers get settings with names like &#8216;Collision Detection mode&#8217; and options like &#8216;Discrete&#8217; and &#8216;Continuous,&#8217; which only make sense if you know about how collision detection systems work. So what happens is they learn that they need to set it to &#8216;Continuous&#8217; for fast-moving objects and &#8216;Discrete&#8217; for slow-moving objects, and at that point the setting has basically become equivalent to a &#8216;Movement class&#8217; setting with &#8216;Fast&#8217; and &#8216;Slow&#8217; options, except that it includes this extra pointless translation step that needs to be learnt and applied.</p>
<p>It&#8217;s not hard to imagine how the tools end up like this. Many of them start out almost as testing rigs for the programmers; the tool is there to allow the programmer to quickly configure and test the system&#8217;s features and interactions, so they write it in Programmer, and when they&#8217;ve done that it&#8217;s <em>technically</em> possible for the designer to set it up however they need it to be, so why make further changes? Yet while it may be <em>technically</em> possible for for the designers to set things up the way they&#8217;re needed, it&#8217;s not <em>easy</em>, which means that it requires training, takes longer, distracts the designer, and is more error-prone. These costs are often not taken fully into account. The tool has effectively been designed by a programmer, and if that&#8217;s OK, why do you have designers on your team at all?</p>
<p><em>&#8220;Well, what if someday there&#8217;s an object that is slow-moving but needs Continuous collision detection for some reason nobody can think of right now? Better to keep the flexibility, right? Also, while it might make it easier for the designer to set things up, it makes it harder for the programmer, because now they&#8217;ve got to keep remembering that &#8216;Fast movement mode&#8217; means &#8216;Continuous collision detection.&#8217; It&#8217;s not saved any work, it&#8217;s just moved it around.&#8221;</em></p>
<p>Firstly, bear in mind that remembering things like &#8220;Fast movement mode is implemented using continuous collision detection&#8221; is a programming core skill &#8211; just like remembering things such as &#8220;CPU Texture reads are implemented using readback-from-video-memory, which is slow, and so should be avoided.&#8221; It might be the same amount of work, but programmers have more practice at it.</p>
<p>Secondly, though: simplicity and flexibility aren&#8217;t mutually exclusive! Presenting designers with options &#8220;phrased in Designer&#8221; doesn&#8217;t have to mean taking away the options &#8220;phrased in Programmer.&#8221; The implementation-level options can be hidden away in &#8216;Advanced Settings&#8217; sections, or in a &#8216;Programmer View&#8217; mode. The designer-friendly options can be provided as presets &#8211; a quick means for designers to automatically set the implementation-level options, while still being able to keep thinking in terms of the end user experience, and without losing the flexibility of tweaking things more directly when required. These are solvable problems!</p>
<h3>TL;DR version:</h3>
<p>Programmers and designers have different habits for thinking about problems. When programmers create tools that designers will use, they should present those tools in &#8216;design language&#8217; &#8211; the language of the end-user experience &#8211; rather than just exposing the flags and values that the implementation works with. It&#8217;s better for them to do that, than for the designers to learn how to manipulate the implementation, because figuring out how to set the implementation for a particular design goal is what the programmers are good at.</p>
<p><span style="font-size: 6pt">Blame <a href="http://twitter.com/bobvodka">@bobvodka</a> if you didn&#8217;t think the opening joke worked. If he&#8217;s not escaped already, that is.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/06/07/designers-are-descriptive-programmers-are-procedural/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Top 6 Misconceptions I Had About Unity</title>
		<link>http://www.altdevblogaday.com/2011/05/23/the-top-6-misconceptions-i-had-about-unity/</link>
		<comments>http://www.altdevblogaday.com/2011/05/23/the-top-6-misconceptions-i-had-about-unity/#comments</comments>
		<pubDate>Mon, 23 May 2011 14:21:45 +0000</pubDate>
		<dc:creator>Richard Fine</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://altdevblogaday.org/?p=6409</guid>
		<description><![CDATA[The <a href="http://www.unity3d.com/">Unity 3D</a> toolkit is a great platform for development – but it can be quite confusing to a developer coming from a more traditional, ‘code it all up’ kind of background. I thought I’d write down a few notes about the things I’ve learned - the things I wish someone had told me when I first started getting into it.]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.unity3d.com/">Unity 3D</a> toolkit is a great platform for development – but it can be quite confusing to a developer coming from a more traditional, ‘code it all up’ kind of background. I remember my first experiences with early (1.x) versions of Unity; launching it, I was confronted with something that looked like the level editors I was used to. How was I supposed to build an entire game in just a level editor? Was it visual scripting or something, like the Half-Life modding I was used to? How could it possibly be fast enough? Maybe it was like the ‘Game Maker’ packages I’d used – great as a toy, but a painful experience if you wanted to build an actual polished, shippable game?</p>
<p>The truth is that Unity is an incredibly powerful, versatile tool; however, for a person used to a more traditional development process, it can be a bit difficult to grasp at first. I&#8217;ve been working extensively with Unity for about 8 months now, and I&#8217;m at a point where I’m really comfortable with it – to the extent that I start prototyping all new ideas in Unity, unless there’s a really good reason not to – and I’ve started extolling its virtues to others. Frequently, I see those people then run into exactly the same culture shock I first experienced with it. So, I thought I’d write down a few notes about the things I’ve learned &#8211; the things I wish someone had told me when I first started getting into it.</p>
<h2>Misconception 1: It’s all about the Scene View</h2>
<p>By far the most visually impressive and dominating part of Unity – especially if you load it up with the example project that ships with it – is the Scene View, presenting one or more 3D views of the game in &#8216;editing mode.&#8217;</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/image001.png"><img class="aligncenter size-full wp-image-6410" src="http://altdevblogaday.com/wp-content/uploads/2011/05/image001.png" alt="" width="658" height="524" /></a></p>
<p>This is all very bright and exciting, and tends to be the first thing people want to interact with. Anyone with a basic knowledge of 3D editing programs figures out how to fly the camera around pretty quickly, and starts clicking on things. It becomes very easy to start out by assuming that the scene view is at the center of your development process – that everything will be drag and drop, and that code is a rare and distant sideline.</p>
<p>In practice, while the scene view is <em>important</em>, it’s by no means the center of the development process. It’s highly significant when you’re doing ‘level editing’ type tasks – laying out the geometry that makes up your game world – and it also works as a nice intermediary environment for building up prefabs (more on those in a minute), but <em>that is all</em>. You’ll still be writing code in a code editor which you can then make use of in the scene view, just as a traditional project would write code that the designers could then make use of in their level editor. Though I’m working with Unity as a development platform, <strong>I still spend most of my time in Visual Studio.</strong></p>
<h2>Misconception 2: Every kind of game object will have a distinct, concrete class</h2>
<p>Unity has been my first substantial experience with a component-based architecture.</p>
<p>Traditional games tend to have a concrete class for each kind of entity. Need a gun? Make a class for it, derived from ‘PhysicalObject.’ Need a special kind of gun that also fires grenades? Subclass your ‘gun’ class. One of the commercial games I worked on had an entity inheritance hierarchy that must have been at least 10 levels deep; each level added certain bits of functionality, like ‘is renderable’ and ‘plays pre-recorded animations’ and ‘generates physics impulses’ and ‘is considered friendly to the player.’ Inheritance-heavy architectures tend to be very popular with object-orientation newbies; they grok the meaning of an ‘is-a’ relationship, and then build massive towers out of it.</p>
<p>More savvy – or more jaded – software architects know the value of composition over inheritance. It’s a lot less fragile. Instead of saying that an enemy ‘is-a’ skinned, animated mesh, they say that an enemy ‘<em>has-a’</em> skinned, animated mesh. Instead of the HugeSpiderEnemy class inheriting from SkinnedMesh, it has a SkinnedMesh member variable. Following this approach, these towering inheritance hierarchies turn into fairly simple classes that just contain a bunch of relevant components: a SkinnedMesh, an AnimationPlayer, a PhysicsInteractor, and so on, with a bit of glue in the class itself to keep all the different bits in sync. There&#8217;s often still a bit of inheritance, as a neat way to encapsulate the glue code without sacrificing type interchangeability, but it&#8217;s shallow. This makes it much easier to swap systems in and out, and removes the risk of changes in one system percolating down through everything via inheritance.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/image003.png"><img class="aligncenter size-full wp-image-6412" src="http://altdevblogaday.com/wp-content/uploads/2011/05/image003.png" alt="" width="716" height="336" /></a></p>
<p style="text-align: center">&nbsp;</p>
<p>Unity takes this composition-based approach to its logical conclusion by having you build all your game objects <em>entirely</em> using composition – even the ‘glue code.’ Every game object consists <em>purely</em> of composed parts. The end result is that if you <em>did</em> still have concrete classes for each kind of game object, the only difference between them would be the components that make them up. As such, you only need one concrete class for a game object – Unity appropriately calls it GameObject – and it acts as little more than a container for components.</p>
<p>What about the ‘glue’ code, the stuff that would normally tie all the components together? You can build it as a component too. With the GameObject acting only as a component container, it’s up to the individual components to sort things out between themselves. The glue component isn’t one that you might expect to use on any objects other than this one – but the same was true of the dedicated class, too. Note too that component inheritance is still perfectly normal, so your ‘glue’ code components can participate in an inheritance hierarchy if appropriate.</p>
<p>Components make it very easy to build objects that interact with a wide number of systems, without introducing strong dependencies between these systems. Some care must be taken to ensure that component interactions are handled nicely, but no more than previous approaches.</p>
<h2>Misconception 3: Prefabs are only for making repetitive levels</h2>
<p>One of the ideas I encountered quite early on was the idea of the prefab. Prefabs are “reusable GameObjects,” according to the manual. When I first saw them, and saw that they were called prefabs, I thought: great! I can use those to quickly build up large scenes by effectively copying-and-pasting objects around. If I want to dot trees all over my terrain, I can use a bunch of tree prefabs. If I want buildings, or bits of buildings, I can create prefabs for those, too. Great! But that’s really just a time-saving tool for the artists and level designers, surely…</p>
<p>I’d missed the real point of prefabs. It’s true that you can use them for easily copying objects around, but the really vital thing is that you can insert copies of them <em>at runtime</em>, too. They’re not only about composing the scene in the editor. They’re also about composing the scene, from code, at runtime, too. (For the more pattern-oriented amongst you, what I’m driving at here is the Prototype pattern. Feel free to refresh your memory so you can read the next bit with a knowing smile).</p>
<p>Naturally there are <em>many</em> objects in your game that aren’t in the scene when you start. Bullets in motion. Gibs. Enemies that haven’t spawned yet. Sometimes, you could put these things into the scene and just mark them as inactive, but not always; you <em>need</em> the option to create things dynamically. Unity has that, of course: GameObject has a public constructor, so you can create the GameObject, set its name and layer, then use GameObject.AddComponent&lt;&gt;() to add all the components you need, then set up all the parameters, etc. There’s a twofold problem with this approach, though: firstly, it’s incredibly tedious, and secondly, it makes it really difficult for non-coders to change the way things are set up. If an artist or designer wants bullets to play a different sound or use a slightly different color tracer, they need a coder to go in and alter the spawn code.</p>
<p>This is where prefabs come in. Instead of building a new object from scratch, what you can do is to create a clone of the prefab. Cloning an object duplicates all of its components, along with all of its children and their components, so you don’t have to attach anything or set anything else up; the prefab acts like a <em>prototype</em> for the kind of object you want, a ‘reference copy.’ Furthermore, as the prefab is just a GameObject (or tree of GameObjects), it can be edited by artists and designers in exactly the same way as anything else in the scene; if the artist wants to make your plasma bullets glow more blue, he can click the prefab, find the relevant component, adjust the value, save the prefab, and run the game – no code changes required.</p>
<p>As noted before, this is actually a well-established software pattern, known as the Prototype pattern.</p>
<h2>Misconception 4: You need to write debugging visualizers</h2>
<p>It’s quite common to want to annotate the screen with extra information while your game is in development. Take AI as an example: it’s often very useful to be able to see the path that your agent is trying to follow, or the state he’s presently in, while the game is running. So you write a little bit of code that dumps the information into the top left corner of the screen using the GUI renderer, and watch it change as you go. That’s what I was doing to start with, even in Unity – the ease of access to the GUI system from any component made it very easy – but I don’t anymore, because I’ve realized that Unity already has a method for inspecting such data, and it’s far superior: You can just use the editing tools as if the game weren’t running.</p>
<p>Say you’re in the middle of the game, and one of the AIs is doing something funny. You can pause the game (if you like), pull up the hierarchy window, and click on him to view his inspector. Maybe you’ve got hundreds of AIs and it’s not clear which one is him; in that case, you can switch to the Scene view, navigate to where you paused the game, and click on him to select him. The inspector panel will then let you look into the present state of all components, in exactly the same way as it will let you look at the initial state of them when the game&#8217;s not running.</p>
<p>Maybe the default information displayed isn’t enough. In that case you’ve got two choices: firstly, you can try toggling the inspector into ‘debug’ mode, which causes various private members to show up (usually desirable when you’re trying to inspect internal state); and secondly, you can write a custom inspector class for the component you’re interested in, one that displays exactly the information you want in exactly the way you want it. There&#8217;s a simple function call &#8211; DrawDefaultInspector() &#8211; that you can use to make sure that all Unity&#8217;s existing information continues to be displayed, and then you can augment that information with any calculated or private values you might want. (Notably, Unity&#8217;s default inspector does not show properties, only fields, so I often find myself just adding a couple of entries to show the present values of properties while the game is running).</p>
<p>You can even pull the Game tab out from the Unity interface and drop it onto a second monitor. Now you can play the game on one monitor, and watch the scene view, hierarchy, and inspectors all on the other. Pausing the game isn’t even necessary; all the views can update in real-time, while the game is running, quite happily.</p>
<h2>Misconception 5: There&#8217;s no spatial indexing; you have to search the whole scene for everything</h2>
<p>Unity <em>supports</em> finding objects in the scene based on their name, their tag, and the components attached to them, but it’s slow. The physics engine – Nvidia’s PhysX – doubtless does <em>some</em> scene management to accelerate collision tests, and the fruits of this are exposed to you as raycasts/linecasts/spherecasts in the Physics class, but these only apply to objects that the physics engine knows about; you can’t use it to retrieve non-physics objects like particle effects, for example.</p>
<p>There’s nothing stopping you from maintaining your own lists and caches, though. If you’re going to need to run through a list of all objects that have a component of type X, then type X can, quite happily, keep a private static list of all its instances, and provide a couple of functions for searching that list much more quickly than the general scenegraph functions would be. You need to be careful about exactly when you add and remove objects from this list – using OnEnable()/OnDisable() is likely to be a better plan than using Awake()/OnDestroy(), for example, because that way you won’t have any problems with things like prefabs or inactive objects accidentally ending up on the list – but it’s still fairly straightforward to do.</p>
<p>Another approach I&#8217;ve found quite fruitful is to use tracking volumes. A tracking volume is a collision volume, marked as a trigger (i.e. look for collisions, but don&#8217;t block any movement), with a simple script attached. When an object enters the volume, the script adds it to a list; when it leaves, it removes it; and when any other code wants to find out which objects are presently in the volume, the script can simply provide the list. This is exploiting the fact that the physics engine is doing highly optimized spatial checks <em>anyway</em>, so we might as well cache the results, instead of putting the physics engine on the spot right when we want the data. The tracking volume script can be completely generic for use with any object and volume setup, and you can augment it with useful features like filtering based on object type (&#8216;only track objects that have a Flammable component&#8217;) or layers (&#8216;only track objects in the Pickup layer&#8217;).</p>
<h2>Misconception 6: Coroutines Are Useless</h2>
<p>There&#8217;s a lot of work that a game needs to perform every frame &#8211; obviously there&#8217;s all the core things like rendering, physics, animation, etc. There&#8217;s also quite a lot of gameplay logic &#8211; responding to player input to initiate actions, for example. We want to minimize latency so we do it as quickly as possible, and that means keeping an eye out for it every frame.</p>
<p>There are some bits of logic that <em>don&#8217;t</em> really need to be checked for every frame, though. Take the win conditions: you want to check that the player&#8217;s in a particular area, that he&#8217;s killed particular enemies, collected certain items, and so on. Would it really kill the experience if there was a half-second delay between killing the final enemy and the win sequence kicking off? Frequently, no. But we tend to check every frame anyway, because adding the code into the per-frame update is easier than trying to schedule checks less frequently.</p>
<p>Unity&#8217;s coroutine support makes for a really nice alternative to this. Coroutines are used to implement <a href="http://en.wikipedia.org/wiki/Computer_multitasking#Cooperative_multitasking.2Ftime-sharing">cooperative multitasking</a>. You can set up a long-running process &#8211; an infinite loop, for example &#8211; but stop it from blocking other code by having it &#8216;yield&#8217; at suitable points. The scheduler is then very simple: it just cycles through all the coroutines, running each one in turn until it yields, then moving on to the next. </p>
<p>Unity&#8217;s scheduler is a bit smarter than this, though, because when a coroutine yields, it can emit a value. In Unity, this value is treated as a note to the scheduler about when the coroutine should next be run. You can yield a null to indicate that you want to continue on the next frame; you can yield a WaitForSeconds(0.5f) object to indicate that you don&#8217;t need to run again until half a second has passed; <del>you can yield an animation object to indicate that you don&#8217;t need to run again until the animation has finished playing</del>; and so on. (<i>Edit: I misremembered; yielding AnimationStates isn&#8217;t actually supported. See the comments for some ways to get around this</i>).</p>
<p>This makes it very easy to turn an every-frame check into an every-three-seconds check: write the check in an infinite loop, with a yield-wait-for-three-seconds. Start the coroutine when the component is enabled, and stop it when it&#8217;s disabled (or if you&#8217;re mad like me, hook it up to a property setter, so that setting a CheckX flag property automatically starts and stops DoCheckXCoroutine). The value that you yield is evaluated at runtime, so you can do whatever you like to decide how long to yield for &#8211; maybe check more frequently when the player is getting close, or make the check interval a designer-configurable variable.</p>
<h2>Conclusion</h2>
<p>There&#8217;s probably more I could say, but I&#8217;ll leave it there for now, because I should really be working. I encourage you to give Unity a try, if you haven&#8217;t already.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/05/23/the-top-6-misconceptions-i-had-about-unity/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
		<item>
		<title>Hosting a Game Server on AWS</title>
		<link>http://www.altdevblogaday.com/2011/05/08/hosting-a-game-server-on-aws/</link>
		<comments>http://www.altdevblogaday.com/2011/05/08/hosting-a-game-server-on-aws/#comments</comments>
		<pubDate>Sun, 08 May 2011 09:00:21 +0000</pubDate>
		<dc:creator>Richard Fine</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://altdevblogaday.org/?p=5059</guid>
		<description><![CDATA[<p>I participated in my first <a href="http://www.ludumdare.com/">Ludum Dare</a> last weekend. While I didn&#8217;t get close to producing a <a href="http://www.ludumdare.com/compo/ludum-dare-20/?action=rate&#38;uid=4309">complete, engaging game</a>, I played around with some cool ideas, learnt some asset creation techniques, bolstered my Unity knowledge, and had quite a lot of fun.</p>
<p><a href="http://www.altdevblogaday.com/2011/05/08/hosting-a-game-server-on-aws/" class="more-link">Read more on Hosting a Game Server on AWS&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I participated in my first <a href="http://www.ludumdare.com/">Ludum Dare</a> last weekend. While I didn&#8217;t get close to producing a <a href="http://www.ludumdare.com/compo/ludum-dare-20/?action=rate&amp;uid=4309">complete, engaging game</a>, I played around with some cool ideas, learnt some asset creation techniques, bolstered my Unity knowledge, and had quite a lot of fun.</p>
<p>The theme of the contest this time was &#8220;<strong>It&#8217;s dangerous to go alone! Take this.</strong>&#8221; I considered that if it was dangerous to go alone, then it must be safer to go together. I didn&#8217;t think it would be feasible to develop a full multiplayer game in 48 hours, but I <em>could</em> develop a game that was ostensible singleplayer while having some kind of interaction between all the people who are playing it. Kinda like an MMO, but where all the other players are invisible. (I considered trying to explain this within the game in terms of quantum interference and Everett MWI or something, but I didn&#8217;t get time).</p>
<p>Naturally, in order to have players interact, I&#8217;d need some kind of server that could co-ordinate that interaction. Initially I thought I might be able to use IRC, but, alas, I wanted the game to run in the browser, and there are <a href="http://unity3d.com/support/documentation/Manual/Security%20Sandbox.html">security implications</a> that make connecting to IRC from inside the browser window impossible, at least without some kind of proxy server. If I wanted to run in the browser and also have multiplayer elements, I&#8217;d need to run my own server somewhere.</p>
<p>What&#8217;s the simplest way to get custom code running somewhere Internet-accessible for the duration of the competition? I couldn&#8217;t run it from home &#8211; my home connection&#8217;s upload bandwidth is terrible. I didn&#8217;t really want to sign up for a full shell host; most of them have a minimum term of one month, and I only really need half of that.</p>
<h3>Choosing EC2</h3>
<p>I decided to try running my server on Amazon&#8217;s Web Services infrastructure, specifically their Elastic Compute Cloud (EC2). The multiplayer server is super-simple &#8211; a single instance that stores everything in memory &#8211; so I didn&#8217;t need most of the things that EC2 have on offer, but the ability to spin up a fresh Linux install in seconds, put my server software on it, and just turn it off when I&#8217;m done was very attractive.</p>
<p>I&#8217;m also within my first year of AWS usage, which means I qualify for their <a href="http://aws.amazon.com/free/">free usage tier:</a> a 1.0 GHz Xeon processor, 613MB of memory, 10GB of storage, and 15GB of bandwidth each way in and out, free for a year. Hard to beat free.</p>
<p>I can&#8217;t write this post without mentioning the recent outage they had. Simply put, it doesn&#8217;t worry me. Amazon&#8217;s analysis of what went wrong is very thorough and shows their competency; shit happens, and when it does, the important thing is how it&#8217;s handled. That this kind of chain reaction can happen is interesting, but I&#8217;m confident that they&#8217;ll make the changes they need to make in order to interrupt the chain should anything like it happen again. Remember, Amazon dogfood this stuff; problems in their cloud don&#8217;t just affect their cloud customers, they affect Amazon.com itself.</p>
<h3>Creating the Instance</h3>
<p>The first step was to create the instance (the &#8216;virtual machine&#8217; that I was going to use). From Amazon&#8217;s EC2 management page, I hit the &#8216;Launch Instance&#8217; button, then picked the &#8220;Basic 32-bit Amazon Linux AMI&#8221; from the list. The page walked me through configuring how big I wanted the instance to be (&#8220;Micro&#8221; size to get the free tier) and which &#8216;zone&#8217; I wanted it to be hosted in (I didn&#8217;t care). There were a couple more pages of config where I could turn on things like advanced monitoring and enter custom metadata, but I didn&#8217;t need any of that.</p>
<p>Next it had me create a key pair for the machine. All the Amazon machine images are preconfigured with key-pair based SSH, and it&#8217;s really a much nicer method than using passwords anyway, so I had it create a key pair for me and downloaded the private key to my own machine.</p>
<p>Then it asked me to pick a Security Group. Security Groups are used for configuring the firewall; you might have one security group that allows SSH and HTTP, another that allows FTP, a third that allows only SSH, and so on. I put the machine in a &#8216;default&#8217; group. Finally the wizard had me quickly review my settings; after a quick check, I hit OK, and the machine began booting. </p>
<p>It was ready within about 10 seconds.</p>
<h3>Getting Connected</h3>
<p>Now my machine was running, I wanted to connect to it and start working.</p>
<p>The first thing was to enable SSH on the firewall. The default security group settings are such that machines within a security group can connect to each other on any port, but there&#8217;s no access from the outside world by default. I went to the security group page, picked my &#8216;default&#8217; group, and went to the &#8216;inbound&#8217; tab. I picked &#8216;SSH&#8217; from the &#8216;create a new rule&#8217; dropdown, and left the source at 0.0.0.0/0 (i.e. any source). Hit Add Rule, hit Apply Changes, and the firewall was instantly reconfigured.</p>
<p>Now I just needed to get key-pair authentication working. My SSH client of choice on Windows is, of course, PuTTY; the private key file Amazon had given me isn&#8217;t something that PuTTY can read by default, but PuTTY companion program PuTTYgen can import it and convert it to PuTTY&#8217;s native format. Then I just had to set up the connection: the required settings in PuTTY are a bit hidden, but I set the auto-login username on the Connection-&gt;Data page to &#8216;ec2-user&#8217;, and pointed the private key file on Connection-&gt;SSH-&gt;Auth at my private key file. Then I just had to punch in the IP address of my instance &#8211; visible on EC2&#8242;s management page &#8211; and I was connected.</p>
<p>This entire process, from deciding to use EC2 to being connected to my very own instance over SSH, took about 15 minutes.</p>
<h3>Installing Mono</h3>
<p>I wanted to write my server in C#, just like the rest of my game, so I&#8217;d need the ability to compile and execute C# code on my server. I also wanted to use some of the server-side tools that Unity provides for dealing with things like cross-domain security policy, and they too are written in C#. Even if I compiled everything on my own machine, I&#8217;d still need the Mono runtime to be present in order to run them.</p>
<p>I was all set for a difficult and time-consuming process of building things from source or something, but then I found <a href="http://stackoverflow.com/questions/3510320/install-mono-on-centos5-5-using-yum">a perfect set of instructions on StackOverflow.</a> Other than needing to prefix a few of the commands with &#8216;sudo,&#8217; they worked flawlessly.</p>
<h3>&#8230;and we&#8217;re away</h3>
<p>That&#8217;s basically all I needed to do, beyond writing the server itself. I needed to open a couple more ports in my Security Group &#8211; one for the game server itself, and port 843 for the crossdomain.xml policy server &#8211; but that was basically it.</p>
<p>I&#8217;ve not set up any persistent storage for my instance. Other than the mono install and the server software, I&#8217;ve not changed the base AMI, and it&#8217;d take me 10 minutes to reapply those changes. In any case, I&#8217;m not planning on shutting down the instance until after the contest, at which point it won&#8217;t matter.</p>
<p>If you want to run a custom game server and you don&#8217;t want to do it from home, AWS may well be worth checking out: it&#8217;s extremely easy to get going, free for most of the things that a small game would need (for a year, at least) and scales nicely if you&#8217;re looking at doing something bigger.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/05/08/hosting-a-game-server-on-aws/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Play, Don&#8217;t Show</title>
		<link>http://www.altdevblogaday.com/2011/04/08/play-dont-show/</link>
		<comments>http://www.altdevblogaday.com/2011/04/08/play-dont-show/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 11:14:45 +0000</pubDate>
		<dc:creator>Richard Fine</dc:creator>
				<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://altdevblogaday.org/?p=3320</guid>
		<description><![CDATA[There's a well-worn mantra in fiction writing: <a href="http://en.wikipedia.org/wiki/Show,_don't_tell">"Show, don't tell."</a> The Janet Evanovich quotation sums it up nicely: 

<blockquote>It is the difference between actors acting out an event, and the lone playwright standing on a bare stage recounting the event to the audience.</blockquote>

From an information-content point of view, the latter still gets everything across - but the former would be more fun to watch. Why is that, though? What's so fun about seeing the events acted out?]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a well-worn mantra in fiction writing: <a href="http://en.wikipedia.org/wiki/Show,_don't_tell">&#8220;Show, don&#8217;t tell.&#8221;</a> The Janet Evanovich quotation sums it up nicely: </p>
<blockquote><p>It is the difference between actors acting out an event, and the lone playwright standing on a bare stage recounting the event to the audience.</p></blockquote>
<p>From an information-content point of view, the latter still gets everything across &#8211; but the former would be more fun to watch. Why is that, though? What&#8217;s so fun about seeing the events acted out? </p>
<p>In many cases, if not all, fun comes from learning (as Raph Koster <a href="http://www.amazon.com/Theory-Fun-Game-Design/dp/1932111972/">writes</a>). The process of understanding &#8211; of gaining a new explanation of how something is done or why something works, and thereby solving a problem that was posed by the lack of that explanation &#8211; is enjoyable.</p>
<p>So maybe that&#8217;s why Showing is more fun than Telling &#8211; because it offers more to learn, more opportunities to undergo that process of understanding.</p>
<h3>Telling</h3>
<blockquote><p>The Colonel was very brave.</p></blockquote>
<p>When somebody Tells you something, you&#8217;ve got two options: take it or leave it. You can&#8217;t do more than that, because you&#8217;ve got nothing else to go on. It&#8217;s their way &#8211; their summary of events &#8211; or the highway. If you&#8217;ve got questions or objections, there is no further evidence available with which to reason. (We&#8217;re talking about a broadcast medium here, like a book or a film or a game. Obviously in a normal conversation, you can ask questions &#8211; but increasingly it stops being what I&#8217;d call Telling). </p>
<p>So the amount you can learn is very limited &#8211; the speaker, usually the narrator, has done most of the inference for you, so what you see is what you get. It&#8217;s efficient for the author to write, and efficient for the audience to consume, but if there are any doubts or questions lingering in the audience&#8217;s mind afterwards, it does nothing about that. Telling tells you <em>the way things are</em>, and hopes that that&#8217;s good enough for you.</p>
<p>Older games made heavy use of Telling for things like the game&#8217;s storyline; for example, the &#8216;mission briefing&#8217; format, where the game&#8217;s story would be presented as screens of text between each level, is textbook Telling. At the same time, many games also explained their game mechanics by Telling the player how they worked in the manual.</p>
<h3>Showing</h3>
<blockquote><p>Lying prone in the mud amidst the gunfire, the Colonel gritted his teeth, and reached down past his blood-stained uniform to pull a cigarette out from the packet. The wound hurt like hell, but he was damned if he was going back now &#8211; not while his boys were still in the enemy camp.</p></blockquote>
<p>On the other hand, when somebody Shows you something, you are being given more detailed information, and being allowed to decide for yourself which things are significant, and which things feature into your ongoing understanding of how the characters think and how the world of the story works. You have (or make) guesses about how things work and why people do things, and as the Show gives you more and more information, you reject and refine those guesses. A Show can take a told idea &#8211; such as &#8216;the Colonel is brave&#8217; &#8211; and let you see exactly <em>why</em> the narrator considers him brave, and <em>when</em> he is brave, and what his motivation is for being brave, and so on. If you doubted that he was brave &#8211; perhaps because he did other things that it didn&#8217;t seem so brave &#8211; then the more detailed account given by the Show might help you see him in a new light, and understand how his brave nature could fit with those other things. Showing allows you to not only discover the way things are, but also <em>how and why they are that way,</em> which is a much more substantial piece of knowledge that can be used to answer many questions and assuage many doubts.</p>
<p>The original <em>Half-Life</em> was greatly praised for its innovative approach to storytelling: it was almost all Show and no Tell. There was no opening screen full of backstory text, no long cutscenes with characters &#8216;filling you in&#8217; on what had happened with no regard for what a person would <em>actually</em> say in their situation. The shocking moment when you first meet the U.S. Marines and discover that they&#8217;re out to get you too isn&#8217;t highlighted with someone saying &#8220;The Marines are under orders to kill you too&#8221; (at least not immediately). So there&#8217;s a wonderful few moments of confusion &#8211; was it an accident? Did they mistake you for an alien? No, these guys are too good for that. If they were shooting at you it must have been on purpose. Why would they do that? And then the truth dawns on you&#8230;</p>
<p>Many games, now, follow in <em>Half-Life</em>&#8216;s footsteps. Cutscenes and flashbacks replace the briefing screens of old. The location design includes information that story elements can be deduced from, like computer terminals and newspaper clippings &#8211; or, more subtly, big welcoming banners displayed on the outskirts of ruined cities, human skeletons on the tables of alien medical labs, and so on.</p>
<p>Showing is still limited. While the author isn&#8217;t overtly giving you their own interpretation of events as with Telling, it still influences things, as they&#8217;re still in control of which details you are Shown and which you are not. So, they&#8217;ll anticipate the questions you&#8217;ll have, and try to include enough details that you can work out the answers; but they&#8217;ll often fail to anticipate your questions entirely correctly, and they might not provide satisfactory answers &#8211; or you might miss the answer that they intended you to find. If that happens, then there&#8217;s no recourse, as with Telling. </p>
<p>It&#8217;s the learning equivalent of a rail shooter: the author sets up the questions and answers for you in the hope that it will satisfy you, and they usually cover most of what you wanted to know, but there&#8217;s often a few unresolved issues &#8211; plot holes, actions that seemed out of character, and so on. Not to mention the &#8220;Don&#8217;t go in there!&#8221; effect: Who hasn&#8217;t watched a movie, seen the characters do something stupid, and thought, &#8220;Aargh! Why they hell didn&#8217;t they just do it this other way?&#8221;</p>
<p>That&#8217;s where games come in. Because games, as an interactive medium, add a third mode of exposition: Play.</p>
<h3>Playing</h3>
<p>When a creator lets you play with something, lets you engage with a mechanic and poke it and turn it over in your hands, then you gain the freedom to explore counterfactuals (&#8220;what if&#8221; questions). In a film or a book, if the characters take the left-hand door, then they will <em>always</em> take the left-hand door, no matter how many times you rewatch or reread; but in a game, you can take the right-hand door instead. Instead of being nice, you can be nasty; instead of being cautious, you can be gung-ho; and you can see what happens. For the most part, you can decide which questions you want to explore, and which ideas you want to put to the test, at your convenience. Through Play, you can discover not just the way it is, and how it comes to be that way, but also <em>why it couldn&#8217;t be any other way</em> &#8211; why the ideas that are simply Shown in other media are <em>hard to vary</em> without making them worse. (Or, sometimes, that it <em>should</em> be another way, as characters in films and books often don&#8217;t make the best decisions).</p>
<p>Every game, as you&#8217;d expect, contains some element of Play. The most remarkable ones are the ones that let you Play with unusual things: <a href="http://www.interactivestory.net/"><em>Façade,</em></a> for example, lets you Play with human relationships; <em>Dead Rising</em> includes a mechanic that lets you Play a little with photography and capturing interesting and well-framed shots; and a game like <em>Mirror&#8217;s Edge</em> lets you Play with free-running and acrobatics. There are, of course, a huge number of games that let you Play with things like combat tactics, too.</p>
<p>Just as with Showing, however, there are limits. Often in a game you <em>can&#8217;t</em> take the right-hand door instead of the left-hand door, because the developers set the game world up that way, barring the door and not modelling the room behind it &#8211; but really, this just means that &#8220;which door gets taken&#8221; isn&#8217;t something that the developers are letting you Play with in that game. They let you Play with other things instead, like whether you throw a flashbang grenade into a room before you enter it, or whether you use a health pack now or later, or what happens if you press the button marked &#8216;self destruct,&#8217; and so on. </p>
<p>Play is also subject to the developer&#8217;s interpretation, just as much as Showing or Telling, because the developer is free to distort and focus their modelling of real-world phenomena. A game about running for a political office, for example, might base its mechanics around making rousing speeches and designing good policy &#8211; or it might base it around bribing electoral officials and leaking scandalous stories about one&#8217;s rivals&#8230;</p>
<h3>Who cares?</h3>
<p>OK, so Play can be used for exposition purposes. So what? Isn&#8217;t Showing and Telling good enough already?</p>
<p>Firstly, consider that none of these exposition methods apply solely to game stories: exposition can be used to convey <em>any</em> idea, from &#8220;he is Luke&#8217;s father&#8221; to &#8220;you should hit the weak point for massive damage.&#8221; Any idea that you want the player to acquire will appear somewhere in this framework.</p>
<p>Secondly, while Showing and Telling are often perfectly fine and easy, Play allows you to convey deeper ideas, and it allows you to convey them more persuasively. For a person to be persuaded that an idea is true (or at least, the closest thing to true they&#8217;ve seen so far), any alternative ideas they can think of need to be dismissed as inferior &#8211; inferior because they don&#8217;t work, or they don&#8217;t fit the evidence, or they&#8217;re pointlessly complicated, or they&#8217;re too arbitrary, or whatever. </p>
<p>While Showing and Telling can convey an idea, they&#8217;re one-shot weapons: the creator decides what idea he wants to get across, and commits it to a medium that is ultimately read-only, in that, once published, his work will only <em>ever</em> communicate that idea. The audience consume that idea, perhaps with varying interpretations (i.e. my idea of &#8216;brave&#8217; might be a bit different to your idea of &#8216;brave&#8217;), but if they don&#8217;t buy it, the author has no second chance to convince them.</p>
<p>When the audience is invited to Play with an idea, however, the author&#8217;s got a much more populated arsenal: the game can accept an entire range of approaches that the player might try out, developing them to their conclusions. If the player is Playing the game a particular way, and they think of a better way, then they can try that out. They can put all of their alternative ideas to the test, and discover what the actual problems are with each one in turn. When they&#8217;ve finished Playing the game, not only do they understand the idea that you set out to communicate, but they&#8217;ve also dismissed many of the alternative ideas they thought of, too.</p>
<p>Sid Meier famously said that &#8220;a good game is a series of interesting choices.&#8221; For something to be a choice, there must be multiple alternatives available to the player &#8211; and for the choice to be interesting, the alternatives must all be viable, to the extent that it could be worth trying any one of them. Considered against the role of Play in allowing the player to learn what not to do, it&#8217;s clear to see why Meier is right: if you don&#8217;t offer choices then you are merely Telling or Showing, rather than Playing; and if the choices are not interesting, it is because the player doesn&#8217;t consider the presented alternatives to be superior and in need of testing.</p>
<h3>Practical ramifications</h3>
<p>I invite you to consider which forms of exposition you&#8217;re using for which aspects of your game, and to think: are there things that you&#8217;re presently Showing or Telling that you could be Playing instead?</p>
<p>If you&#8217;re making a game about a demon hunter, for example, how are you communicating that the player has killed a demon? I&#8217;m going to guess that you&#8217;re Showing it &#8211; the player swings their sword, the demon spits up blood, and so on. The player will probably buy that &#8211; after all, it&#8217;s a simple Show that conforms to all the standard gaming imagery &#8211; but, if you wanted to let them Play with it, why not let them choose exactly where to stab and slice the defeated demon, based on a simple model of demon anatomy? </p>
<p>The best strategy is the one that leaves no living demons, while still taking the least time (because other demons are around and the player shouldn&#8217;t linger). As the player works their way towards that strategy, they&#8217;ll make mistakes, sometimes wasting time chopping up a thoroughly dead demon, other times finding themselves face to face with a pissed-off and wounded <em>but not dead</em> demon&#8230; but when they succeed, they won&#8217;t just be taking your word for it that they killed the demon, they&#8217;ll feel like they did it with their own two hands, because they&#8217;ve developed expert knowledge on the subject. It helps transform the player from a generic fighter into a real <em>butcher.</em></p>
<p>Let the writing of your game &#8211; not just the story, but the themes and concepts you want to convey &#8211; inform not just the art and sound direction of your game, but the core gameplay design itself. Let it inform your balancing and tuning. Bake the message you want to send into the player&#8217;s own actions.</p>
<p>Play with Play. You&#8217;ll have more fun that way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/04/08/play-dont-show/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Polish a Turd</title>
		<link>http://www.altdevblogaday.com/2011/03/24/how-to-polish-a-turd/</link>
		<comments>http://www.altdevblogaday.com/2011/03/24/how-to-polish-a-turd/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 09:00:46 +0000</pubDate>
		<dc:creator>Richard Fine</dc:creator>
				<category><![CDATA[Bizdev]]></category>
		<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://altdevblogaday.org/?p=2245</guid>
		<description><![CDATA[There are an awful lot of games out there that are... not very good, to put it politely.

Why is that?

I propose that you can start a game project with pretty much <em>any</em> concept, and end up with a hit. It's not the concept that matters. It's the process.]]></description>
			<content:encoded><![CDATA[<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/03/turdpolishSM.png"><img src="http://altdevblogaday.com/wp-content/uploads/2011/03/turdpolishSM.png" alt="" width="114" height="112" class="alignright size-full wp-image-2288" /></a>There are an awful lot of games out there that are&#8230; not very good, to put it politely.</p>
<p>Why is that?</p>
<h2>The Concept of Concept</h2>
<p>Perhaps it&#8217;s just that the games were really strange ideas? I mean, there are some pretty <em>wacked</em> ideas out there. There&#8217;s this one game where you&#8217;re rolling a ball around the place, and stuff sticks to it, and you have to make the ball bigger and bigger&#8230; <em>oh, you own that one? OK, then how about&#8230;</em> this game where you&#8217;re a plumber, and little mushrooms and turtles are attacking you so you have to jump on them, and you explore castles and deserts and stuff, and rescue a princess or something&#8230; <em>what? One of the biggest and most successful game franchises in history? Hmm.</em></p>
<div id="attachment_2276" class="wp-caption aligncenter" style="width: 310px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/03/katamaridamacy1.jpg"><img class="size-medium wp-image-2276" src="http://altdevblogaday.com/wp-content/uploads/2011/03/katamaridamacy1-300x225.jpg" alt="Katamari Damacy" width="300" height="225" /></a><p class="wp-caption-text">People actually bought this.</p></div>
<p>Maybe weird isn&#8217;t a bad thing then. Maybe it&#8217;s the less weird, more realistic games that fail? Oh yeah, I heard about this one game that is basically just a &#8216;ordinary guy&#8217; simulator. You have a little guy, and you make him go to work, and fall in love, and do housework, and all that stuff. Who&#8217;d want to play that? I mean, I do enough of those things in real life. <em>What do you mean, it&#8217;s <strong>the highest-grossing game franchise in PC gaming history?</strong></em> Man, these consumers. They&#8217;ll buy <em>anything</em>, huh?</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/03/IMG_0472.jpg"><img class="aligncenter size-medium wp-image-2277" src="http://altdevblogaday.com/wp-content/uploads/2011/03/IMG_0472-300x225.jpg" alt="Oh god why can't I unsee this" width="300" height="225" /></a></p>
<p>Game ideas can be as strange as you like, or can be so based in real life as to be a simulation of it. They can succeed either way. So, if it&#8217;s not because of the concept, why do some game projects fail?</p>
<h2>Delta Concept Over Delta Time</h2>
<p>I propose that you can start a game project with pretty much <em>any</em> concept, and end up with a hit. It&#8217;s not the concept that matters. It&#8217;s the process. The right process will take any concept and turn it into a hit &#8211; will &#8216;polish a turd,&#8217; as they put it. It takes time &#8211; perhaps more than you have &#8211; and the worse your initial concept is then the more time it will take, but you can always get there in the end. The wrong process will founder and flail and flounder and fail.</p>
<p>Everybody knows what bits of this process looks like:</p>
<ul>
<li>
<p>It&#8217;s agile, and it&#8217;s agile for <em>everybody</em>, not just for programmers.</p>
<p>Agile is usually pitched as a programming methodology, and it&#8217;s buzzwordy, but all it really means is &#8220;able to respond quickly to changes that are needed.&#8221; That&#8217;s an approach that applies to all parts of development.</p>
<div id="attachment_2296" class="wp-caption aligncenter" style="width: 303px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/03/Black_white_cat_on_fence.jpg"><img src="http://altdevblogaday.com/wp-content/uploads/2011/03/Black_white_cat_on_fence-293x300.jpg" alt="" width="293" height="300" class="size-medium wp-image-2296" /></a><p class="wp-caption-text">This cat is prepared to respond quickly to changes that are needed. Are you?</p></div>
<p>I&#8217;m not sure that there&#8217;s much discussion of agile development outside of programming, at least by that name, but I imagine that many people have discovered things about it. For example: on my project, each character animation file has a full copy of the character&#8217;s geometry and rig. This makes changes to the geometry and rig quite painful, because each animation file needs to be updated so that things don&#8217;t get out of sync. I&#8217;ve been considering trying to find an alternative approach that uses Containers or XRef Objects to share one copy of the geometry and rig across all the animation files, making it much easier to respond to changes in the base skin &#8211; much more agile. Have any readers tried this?</p>
<p>(If you&#8217;ve got other tips on how to be agile in parts of the project beyond programming, share them in the comments!)</p>
</li>
<li>
<p>It doesn&#8217;t get attached to anything.</p>
<p>This is almost a repetition of the previous point &#8211; being attached to things makes you less agile &#8211; but it bears repeating because people often underestimate the emotional impact of change. The artists spent two months working on a particular setting, and now you&#8217;re starting to think that the game would be better off without it &#8211; how do you think they will take that?</p>
<p>You have to be prepared to &#8220;shoot your baby in the crib,&#8221; as they say.</p>
<div id="attachment_2299" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.vgcats.com/comics/?strip_id=247"><img src="http://altdevblogaday.com/wp-content/uploads/2011/03/071114-300x236.jpg" alt="" width="300" height="236" class="size-medium wp-image-2299" /></a><p class="wp-caption-text">Sometimes, it's for the best.</p></div>
<p>Don&#8217;t fall for the <a href="http://en.wikipedia.org/wiki/Sunk_costs#Loss_aversion_and_the_sunk_cost_fallacy">Sunk Cost Fallacy</a>. Whether it&#8217;s a particularly clever bit of code, a particularly beautiful piece of artwork, or a particularly intriguing design concept &#8211; it may not be appropriate for the game. And that, at the end of the day, is what matters: that the game is a coherent, balanced, self-consistent whole, not just a collection of beautiful but ill-fitting parts.</p>
<p>If you have to tell someone that their work is going to be cut, it can help if you can help them understand why it&#8217;s being cut. The better you can communicate your vision for how things <em>should</em> be, the easier it will be for them to agree with you. Of course, they might disagree that your vision is better, in which case you should listen carefully &#8211; they might be right, after all.</p>
<p>Also, never throw beautiful things away entirely: maybe they&#8217;re not right for <em>this</em> project, but if you shelve them for now, they might come in very handy on the <em>next</em> project. It&#8217;s senseless to simply destroy work that you&#8217;ve already paid to develop, and the idea that the work is being shelved for future use, instead of being wasted time, can make it a lot easier on the creator.</p>
</li>
<li>
<p>It actively looks for criticism.</p>
<p>Everyone agrees that QA and play-testing is useful, but most projects do it infrequently, or put it off until really late in the project. Why? I think there are two major factors:</p>
<p>The first is that the importance of criticism isn&#8217;t well understood. &#8220;The focus is on getting the game <em>right</em> &#8211; we can worry about the ways in which it&#8217;s <em>wrong</em> after we&#8217;ve done that.&#8221; But is it really always that easy to identify what&#8217;s right? A lot of the time, it&#8217;s fine, but every project I&#8217;ve worked on has seen disagreements within the development team about how particular things should be done &#8211; from the use of set-pieces, to the way a particular explosion effect should look. Lots of predictions are made, and lots of options are presented, with a fair case being made for each one. If, rather than worrying about <em>getting things right</em>, you focus on <em>not getting them wrong</em>, then you will still approach the best option, but you won&#8217;t spend any time agonizing over what that option is when really <em>any</em> of them would be perfectly valid.</p>
<p>The second is that people are afraid of criticism because it means they might need to change the way things are going. And this, of course, is extremely silly. If you don&#8217;t test to find problems, the problems won&#8217;t just go away! It&#8217;s better to know about a problem &#8211; to know about a criticism &#8211; and then choose not to act on it, than it is to not know.</p>
<p><div id="attachment_2302" class="wp-caption aligncenter" style="width: 310px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/03/lalala.jpg"><img src="http://altdevblogaday.com/wp-content/uploads/2011/03/lalala-300x195.jpg" alt="" width="300" height="195" class="size-medium wp-image-2302" /></a><p class="wp-caption-text">Not an effective problem-solving strategy.</p></div>
</li>
<li>
<p>Embrace what makes your idea different &#8211; and, conversely, don&#8217;t put too much effort into trying to beat other games at things that aren&#8217;t central to what you&#8217;re doing. If what makes your game different is its innovative approach to strategy and macro-management, then it&#8217;s <em>fine</em> that you display health bars in the same way as every other game. Nobody buys an RTS game because &#8220;it has a particularly innovative approach to displaying health bars.&#8221;</p>
<p>Games, while young, have developed a lot of traditions, and those traditions represent the progressive refinement of an idea over multiple iterations. There are ideas that are cliche, sure, but they&#8217;re cliche because they were used in many games, and they were used in many games because they <em>worked</em> in many games. If you want to change one of these traditions, you should make sure you understand how your change will improve it; being different just for the sake of being different, without understanding how you&#8217;re making things better, makes it very likely that you&#8217;ll be making things <em>worse</em>.</p>
</li>
</ul>
<h2>The End Boss</h2>
<p>There&#8217;s one colossal obstacle to all this. The experienced developers amongst you probably already know what it is. It&#8217;s the commercial realities of a project &#8211; the iron grip of the publisher.</p>
<p>The publisher, all too often, will not understand if you want to spend time and money making your project more agile. They <em>do</em> fall for the sunk cost fallacy, and will actively thwart your attempts to change the direction of the project if it&#8217;s too far away from their original plan, regardless of whether the direction is better. They will not fund testing and QA at the times that you need it, because the idea that testing and QA should be a continuous process is a comparatively new &#8211; and expensive &#8211; idea.</p>
<p>And worst of all, they will put all the emphasis on the original concept. Regardless of your team&#8217;s ability to take a concept and make it great, they won&#8217;t trust that. They&#8217;ll insist that you get the concept right on day one, so they can hold you to it. And when you don&#8217;t quite get the concept right on day one &#8211; because nobody&#8217;s perfect &#8211; they&#8217;ll keep insisting that you polish it and polish it, regardless how much of a turd it is.</p>
<p>The first publisher to understand that the success of a project depends not on where you start, but on how quickly and effectively you can improve, will produce a lot of hit games, and make a lot of money.</p>
<p>The real key to polishing a turd is being given the freedom to throw the turd away if you need to.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/03/24/how-to-polish-a-turd/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>It&#8217;s time to stop using Subversion</title>
		<link>http://www.altdevblogaday.com/2011/03/09/its-time-to-stop-using-subversion/</link>
		<comments>http://www.altdevblogaday.com/2011/03/09/its-time-to-stop-using-subversion/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 18:29:36 +0000</pubDate>
		<dc:creator>Richard Fine</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://altdevblogaday.org/?p=1579</guid>
		<description><![CDATA[Since learning about the existence of source control systems about 8 years ago, I've always chosen Subversion for projects: it's free, better than CVS, widely used and documented, and doesn't completely suck. I've used other systems at times, as required by particular projects - notably SourceSafe (which is worse than Subversion) and Perforce (which is better, but costly) - but none of them were quite sufficient to pull me away from Subversion as a simple, obvious choice for all of my own projects.

Until now, that is. Subversion is no longer my first choice, and I reckon that six months from now, I'll never need to use it on a project again. If you're using Subversion, I think you should probably stop using it too...]]></description>
			<content:encoded><![CDATA[<p>Since learning about the existence of source control systems about 8 years ago, I&#8217;ve always chosen Subversion for projects: it&#8217;s free, better than CVS, widely used and documented, and doesn&#8217;t completely suck. I&#8217;ve used other systems at times, as required by particular projects &#8211; notably SourceSafe (which is worse than Subversion) and Perforce (which is better, but costly) &#8211; but none of them were quite sufficient to pull me away from Subversion as a simple, obvious choice for all of my own projects.</p>
<p>Until now, that is. Subversion is no longer my first choice, and I reckon that six months from now, I&#8217;ll never need to use it on a project again. If you&#8217;re using Subversion, I think you should probably stop using it too.</p>
<h2>So, what&#8217;s wrong with Subversion?</h2>
<h3>Committing = publishing</h3>
<p>The first big problem with Subversion is that there&#8217;s no separation between creating and publishing a commit. If your changes aren&#8217;t present in the centrally accessible repository, then they&#8217;ve not been committed. This makes commits pretty slow.</p>
<p>It also makes them susceptible to network errors. If I&#8217;m committing some large assets to a repository<a href="http://beanstalkapp.com/"> hosted in the cloud</a>, it&#8217;s quite often been the case that the connection has just died somewhere along the line. If, while the upload&#8217;s been happening, I&#8217;ve made more changes to my working copy, then it&#8217;s difficult to repeat the commit: I have to carefully pick out my new changes and keep them separate from my old changes.</p>
<p>Furthermore, because my commits are published as soon as they&#8217;re made, I have to be very careful about what I commit. As soon as the commit is published, other people may check it out and try building it. So, I mustn&#8217;t make any commits that would break the build and stop other people from working. (Private branches remedy this somewhat, but this opens up a whole separate set of problems around merging, that I&#8217;ll come to later).</p>
<p>Lastly, if I can&#8217;t access the repository server &#8211; if it&#8217;s gone down, or if I&#8217;m working remotely &#8211; then I can&#8217;t commit at all.</p>
<h3>Merges</h3>
<p>While Subversion evangelists always emphasized the ease with which you could create branches, they were a bit more quiet when it came to merging those branches. If you&#8217;re going to make serious use of branches on a project, there are certain things that the SCM needs to be able to support:</p>
<ul>
<li>Files post-merge must correctly track history back along all merged branches.</li>
<li>Tracking which commits have been merged from one codeline to another, so that you don&#8217;t risk double-merging.</li>
<li>It should be easy to pull commits from one codeline to another.</li>
</ul>
<p>Since version 1.5, Subversion <em>technically</em> supports all of these&#8230; but it&#8217;s all a bit fragile. For any folder that you&#8217;ve merged things into, an svn:mergeinfo property is set, recording the revision numbers of all the things that have been merged in (so that they&#8217;re not accidentally remerged, and so that future merges know where to pick up from). This <em>more or less</em> works &#8211; provided you don&#8217;t want to do things like merge code from other repositories &#8211; but there are many reports of it being somewhat prone to error. If your mergeinfo property has been corrupted somehow, it might be extremely hard to spot &#8211; and it&#8217;s not possible to regenerate the property by simply examining the codelines and commits.</p>
<h3>File system operations</h3>
<p>If you want to reorganize files in your working copy, you have to make sure that you involve Subversion in the appropriate manner, otherwise you risk doing things like interrupting your file history or committing changes to the wrong places. Filesystem operations like move and copy need to be &#8216;svn move&#8217; and &#8216;svn copy.&#8217;</p>
<p>It&#8217;s not too hard to remember to do this if you&#8217;re working by hand, but increasingly we expect our tools to manage our files for us, and they don&#8217;t invite Subversion into the conversation.</p>
<h2>What I&#8217;m using instead</h2>
<p>I&#8217;ve switched to <a title="Git" href="http://git-scm.com/">Git</a>. It solves all of the above problems, as well as just generally being much, much faster.</p>
<p>When I first heard about Git, I didn&#8217;t really get it. Distributed version control seemed like a really bad idea &#8211; who&#8217;s copy of the code is authoritative? If bugs are logged against a particular build, how do you track down the code that was used to create it? The loss of things like sequential revision numbers seemed like a big drawback. And branching wasn&#8217;t something I ever used in Subversion, so I didn&#8217;t see why I should care about it in Git.</p>
<h3>Why it&#8217;s better</h3>
<p>It took me a while to realize the best way to think about this, which is: Screw the distributed bit. If you want to have a central, authoritative repository, you can still do that. It&#8217;s just that it becomes a matter of project policy, rather than a technical requirement; a particular repository is central and authoritative <em>because you put it in the center and treat it as authoritative</em> (or &#8216;bless&#8217; it, in the terminology). You can mimic your present Subversion setup completely. What&#8217;s disconcerting is that you have to <em>choose</em> to do so, rather than simply having it fostered upon you.</p>
<p>Futhermore, Git explicitly models something that Subversion seems to miss: Every developer&#8217;s working copy <em>is</em> a private branch. The branch is created when they check out the code, it diverges from the central repository as others publish their changes, and it&#8217;s merged when the developer updates or commits their working copy. By giving the developer an entire repository instance on their own machine to store that branch in, it can actually be treated as a real branch, with multiple commits, a proper name, and so on. Some people ask: what if developers don&#8217;t push their changes to anyone else? To which I respond: What if they don&#8217;t check in their changes at all?</p>
<p>A common criticism leveled against Git was its usability &#8211; Git originated as a set of command-line tools for the Linux kernel hackers, and that heritage persisted. This argument was reasonable a year ago, I think, but the tools have matured now. I&#8217;m using <a title="Git Extensions" href="http://code.google.com/p/gitextensions/">Git Extensions</a> as a general Windows-based Git UI, and it includes Visual Studio integration; on Mac, <a href="http://www.git-tower.com/">Tower</a> is an excellent choice <em>(Edit: And XCode 4, out today, includes Git integration out of the box)</em>. It&#8217;s true that I have dropped down to the CLI for some more involved tinkering &#8211; mostly relating to importing my Subversion repositories &#8211; but I don&#8217;t use it at all on a day-to-day basis.</p>
<p>There&#8217;s a little bit more terminology to learn &#8211; staging, pushing and pulling, rebasing, and so on &#8211; but it&#8217;s not that big a deal, and most of it&#8217;s intuitive. This isn&#8217;t a severe disadvantage over Subversion, anyway; I&#8217;ve seen claims that non-technical people can&#8217;t handle the jargon, but if you think that non-technical people aren&#8217;t already learning what terms like &#8216;checkout&#8217; and &#8216;commit&#8217; mean then you&#8217;re kidding yourself. Git&#8217;s UNIX-philosophy layers-of-hundreds-of-tiny-programs approach makes it very easy to build your own scripts and automation over the top to make things as easy as possible for your team, without forcing them to adapt a particular workflow.</p>
<h3>Why it&#8217;s not</h3>
<p>There are a couple of things I can see might be problems for people. I think they&#8217;re problems that can be circumvented or solved, either through project policy, or by waiting for Git to mature a little more, and they&#8217;re not deal breakers for me &#8211; but they&#8217;re worth considering.</p>
<p>Firstly, every developer will have a complete copy of the entire repository on their machine. Given how cheap storage is these days, I don&#8217;t think this is a very common issue, but I can imagine that some extremely large projects might find it prohibitive, especially if they&#8217;re storing lots of large assets. To give you some idea, one of my projects takes up about 900MB on disk (code + assets), and the git database adds another 430MB on top of that, for a total checkout size of 1.3GB; before I moved it to Git, its Subversion checkout was 1.85GB. As far as speed is concerned, though, <a href="http://ldn.linuxfoundation.org/article/dvcs-roundup-one-system-rule-them-all-part-2#git">Git&#8217;s speed barely drops for very large projects.</a></p>
<p>Secondly, because Git tracks files by their content rather than by their path information, it&#8217;s sometimes difficult to keep tracking history for binary files. Many changes that seem small &#8211; even changing a single pixel in a texture &#8211; can result in the content of the file changing dramatically when compressed, and if too much of the file has changed, Git might not recognize it as being the same file any more. It tries to be smart about this, and if you don&#8217;t change the filename then you&#8217;re usually OK, but occasionally it can break.</p>
<p>Thirdly, on the topic of binary files: while diffing and merging binary files is impossible just as much as under any SCM system, people tend to go for the next best thing, which is locking files to make sure that at least people won&#8217;t waste time making changes that can&#8217;t be merged &#8211; but that&#8217;s not something you can do out of the box with Git, as there&#8217;s no authority that can tell you which files are and are not locked. Most of the time this won&#8217;t be a big deal &#8211; most of the teams I know have fairly strong ownership of art assets, so you wouldn&#8217;t really expect two different people to be changing the same model &#8211; but it&#8217;s likely to bite you at the worst possible times, such as when everybody is rushing to get something done for a milestone.</p>
<p>Per-file locking is part of a more general issue, which is that Git doesn&#8217;t have much in the way of an access control mechanism: you can grant read access without granting write access, but either the entire repository is accessible, or none of it is. Sometimes it&#8217;s convenient to be able to set up a project in which certain directories (for example, &#8216;shared&#8217; or &#8216;engine&#8217;) can only be committed to by particular people.</p>
<p>Personally, I&#8217;ve found it&#8217;s not too hard to work around this by simply using multiple repositories: I&#8217;ve got multiple &#8216;section&#8217; repositories that have particular permissions set up, and then a central &#8216;master&#8217; repository that pulls branches from each section. Git&#8217;s got no problem that the branches I&#8217;m pulling in came from entirely separate repositories &#8211; another result of the distributed approach &#8211; and they merge fine, provided I make sure that the directory structures in each section are set up appropriately. It&#8217;s a little less neat than approaches I&#8217;ve previously used that employed svn:externals, but it&#8217;s not been a big problem. Git does also have support for &#8216;submodules,&#8217; but after reading the documentation several times I&#8217;ve still not been able to figure out whether they&#8217;re quite suitable for my setup &#8211; if anyone has insight into how they can be used for this stuff, I&#8217;d be interested to hear it.</p>
<p>These features are presently lacking, but Git <em>does</em> have support for <a href="http://www.kernel.org/pub/software/scm/git/docs/githooks.html">hooks</a> at various points in the SCM workflow; the &#8216;update&#8217; and &#8216;pre-commit&#8217; hooks look quite promising to me as a way of rejecting commits that break either locking policies or ACL requirements. Hopefully we&#8217;ll soon see somebody provide readily usable scripts that use these hooks to add in the missing features.</p>
<p>(Given these problems, there&#8217;s a fair case to be made that while Git is fine for source code, it&#8217;s not the right place to keep assets &#8211; you might be better off using a system more deliberately designed for assets, like Alienbrain. Personally it&#8217;s not affecting me &#8211; our art is contractor-developed, so it tends to change infrequently. Using text-based asset formats where possible does help, though).</p>
<h2>Conclusion</h2>
<p>In the month or so that I&#8217;ve been using Git, I&#8217;ve generally been impressed. My workflow has improved; I&#8217;m now making lots of tiny, logically self-contained commits, instead of the huge blob changes I was committing before, and I&#8217;m staging, committing, and then continuing to work without waiting for any tedious uploading. It&#8217;s encouraged me to experiment with trying out new features and new ways of doing things, using local branches to quickly spin off a safe environment in which to play &#8211; as well as ensuring that I&#8217;ve got somewhere I can keep the results of the failed attempts if I want to refer back to them later.</p>
<p>I can imagine that I might find myself using Subversion occasionally in the next few months, as third-party application vendors work to bring their Git integration support up to the same standard as their Subversion integration. I don&#8217;t think it&#8217;ll be long now, though. Subversion as a <em>concept</em>, as an architecture for an approach to source control, has been refuted.</p>
<p>Subversion is dead. It&#8217;s just taking a bit of time for the rigor mortis to kick in.</p>
<p><i>(<b>Update:</b> Very interesting feedback from lots of people on this. Read the comments below; you may also want to check out the discussions over at <a href="http://news.ycombinator.com/item?id=2309122">Hacker News</a> and <a href="http://www.reddit.com/r/programming/comments/g13h1/its_time_to_stop_using_subversion/">reddit</a> for more).</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/03/09/its-time-to-stop-using-subversion/feed/</wfw:commentRss>
		<slash:comments>95</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.072 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-05-17 03:36:07 -->
<!-- Compression = gzip -->
