{"id":260,"date":"2009-11-17T13:27:13","date_gmt":"2009-11-17T03:27:13","guid":{"rendered":"http:\/\/www.doolwind.com\/blog\/?p=260"},"modified":"2009-11-17T13:27:13","modified_gmt":"2009-11-17T03:27:13","slug":"using-csharp-for-a-commercial-game","status":"publish","type":"post","link":"https:\/\/www.doolwind.com\/blog\/using-csharp-for-a-commercial-game\/","title":{"rendered":"Using C# For a Commercial Game"},"content":{"rendered":"<p><a href=\"http:\/\/www.doolwind.com\/images\/blog\/csharpcommercial.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright\" title=\"C# Commercial\" src=\"http:\/\/www.doolwind.com\/images\/blog\/csharpcommercial.jpg\" alt=\"\" width=\"109\" height=\"82\" \/><\/a>Does the title of this article make you cringe?\u00a0 People have mixed feelings when it comes to C# and the .NET framework.\u00a0 Just like many hardcore game developers 10 years ago swore that games should only be made in straight C, many developers today say that C++ is the only way to go.\u00a0 This article outlines my reasoning for using C# for a commercial game on PC.<\/p>\n<p><strong>Rapid Application Development<\/strong><\/p>\n<p>The primary reason for using C# is because coding in it is more productive than any other language I&#8217;ve used.\u00a0 Language features like interfaces, generics and delegates make it a cleaner language than C++.\u00a0 Development practices such as unit testing and dependency injection are also easier to use in C#.\u00a0 This clarity translates to more readable code and less bugs, particularly when working with a larger or younger team of developers.<\/p>\n<p>The second productivity gain comes from having access to the .NET framework.\u00a0 From its XML handling for configuration, to built-in math libraries, many of the core functions you can think of are taken care of.\u00a0 This saves you writing the code yourself and lets a reliable, tested framework do the heavy lifting in areas you don&#8217;t want to spend your time.<\/p>\n<p>C# lets you focus on making a game rather than writing an engine, or learning someone else&#8217;s engine.<\/p>\n<p><strong>Unified Code &#8211; Client, Server, Tools, Scripting<\/strong><\/p>\n<ul>\n<li><strong>Server<\/strong> &#8211; I&#8217;m writing the server      architecture (matchmaking, leaderboard, etc) in ASP.NET and C#.\u00a0 This lets me share libraries between the      client and server and makes calling web services from the client a breeze.<\/li>\n<li><strong>Tools<\/strong> &#8211; All our tools are also      written in C#.\u00a0 This means that our      tools can have rendering within them, can reuse libraries and simplifies      the creation of in-game tools.<\/li>\n<li><strong>Scripting &#8211; <\/strong>As I&#8217;ve mentioned in a <a href=\"http:\/\/www.doolwind.com\/blog\/why-you-should-use-csharp-for-your-scripting-language\/\">previous article<\/a>, I&#8217;m using C# for the scripting      language in our game.<\/li>\n<\/ul>\n<p><strong>Better Software Development<\/strong><\/p>\n<p>C# supports better software development practices in a number of key ways:<\/p>\n<ul>\n<li><strong>Refactoring      Tools<\/strong> &#8211;      Visual Studio has great refactoring support such as renaming identifiers      and extracting methods &amp; interfaces.<\/li>\n<li><strong>Separation of      Concerns<\/strong> &#8211;      Splitting a project into separate .dll&#8217;s is trivial in C#.\u00a0 This helps with maintainability and encapsulation      of code and encourages good separation of concerns.<\/li>\n<li><strong>Unit Testing<\/strong> &#8211; C# has great support for      unit testing through NUnit or MSTest.\u00a0      As I&#8217;ve <a href=\"http:\/\/www.doolwind.com\/blog\/test-driven-game-development\/\">previously discussed<\/a>, unit      testing fits perfectly in certain areas of game development.\u00a0 The easier unit testing is to achieve,      the more people are likely to adopt it.<\/li>\n<\/ul>\n<p><strong>In-Game Web Browser<\/strong><\/p>\n<p>As part of the game engine we are developing we decided all interaction with the server for matchmaking would be through http\/html.\u00a0 This reduces the need for creating such a complex in-game UI system and simplifies communication with the server.\u00a0 The .NET web browser control is extremely easy to use and communication between the browser and host application (the game) is a simple.<\/p>\n<p><strong>Run-time isn&#8217;t too large<\/strong><\/p>\n<p>I&#8217;m currently targeting the .NET 2 framework.\u00a0 This put&#8217;s the footprint at around 20MB.\u00a0 The latest .NET frameworks (3.5 and 4) both have a &#8220;client profile&#8221;.\u00a0 This is a subset of the .NET framework that comes in at about 30MB.\u00a0 My plan is to use the .NET 4 client profile once it is released as it gives me access to all the latest language features (Linq, lambda expressions, dynamic objects, etc).\u00a0 MS is <a href=\"http:\/\/blogs.msdn.com\/jgoldb\/\">currently planning<\/a> to make .NET 4 client profile a windows update meaning the .NET framework will finally become (almost) ubiquitous among PC&#8217;s running windows.<\/p>\n<p><strong>Negatives<\/strong><\/p>\n<p>There are obvious drawbacks to using C# for a commercial game engine which we took into consideration when making our decisions.<\/p>\n<ul>\n<li><strong>Not Cross      Platform<\/strong> &#8211;      While engines like Unity have proven that C# can run on multiple      platforms, this does not occur out of the box.\u00a0 As we&#8217;re targeting PC&#8217;s exclusively for      our first release this was not a problem.<\/li>\n<li><strong>Xbox run-time      not great<\/strong> &#8211;      C# can run on the Xbox as part of XNA game studio, however the run-time      performance isn&#8217;t great.\u00a0 Garbage      collection is a particular problem which requires refactoring code to      achieve acceptable frame rates.<strong><\/strong><\/li>\n<li><strong>Harder to use      C++ libraries<\/strong> &#8211; Linking to straight C++ (non-COM) libraries is not trivial like it is      connecting to other .NET libraries.\u00a0      Extra time is required if there is a requirement to use existing      libraries such as RakNet.<strong><\/strong><\/li>\n<li><strong>Some performance      concerns<\/strong> &#8211; C#      does have some performance concerns with garbage collection and certain      double math operations.\u00a0 However the      algorithmic gains received from working with such a great language outweigh      the small performance issues in certain areas. \u00a0As with any language, bad code will run      slow.<strong><\/strong><\/li>\n<\/ul>\n<p><strong>Conclusion<\/strong><\/p>\n<p>C# is a great language.\u00a0 I thoroughly enjoy coding for it and I find it makes me more productive when creating games.\u00a0 I plan to release my MVC engine in the future as an open source project.\u00a0 Until then, I&#8217;m continuing to develop our engine and I&#8217;ll keep you up to date with the progress.<\/p>\n<p>For reference, here are a couple of commercial games using C#:<\/p>\n<p><strong>Arena Wars<\/strong> &#8211;<a href=\" http:\/\/www.arenawars.net\"> http:\/\/www.arenawars.net<\/a><strong><\/strong><\/p>\n<p><strong>AI War Fleet Command<\/strong> &#8211; <a href=\"http:\/\/www.arcengames.com\/aiwar_features.php\">http:\/\/www.arcengames.com\/aiwar_features.php<\/a><\/p>\n<p><strong>Sacraboar<\/strong> &#8211; <a href=\"http:\/\/www.sacraboar.com\">http:\/\/www.sacraboar.com<\/a><\/p>\n<p>Have you used C# for game development?\u00a0 Are you sick of chasing memory leaks and crashes in your C++ applications?\u00a0 Do you think game developers will move on from C++ now or in the future?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Does the title of this article make you cringe?\u00a0 People have mixed feelings when it comes to C# and the .NET framework.\u00a0 Just like many hardcore game developers 10 years ago swore that games should only be made in straight C, many developers today say that C++ is the only way to go.\u00a0 This article <a class=\"more-link\" href=\"https:\/\/www.doolwind.com\/blog\/using-csharp-for-a-commercial-game\/\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[33],"tags":[8,19,29,112,48,64,77],"class_list":["post-260","post","type-post","status-publish","format-standard","hentry","category-game-development","tag-net","tag-c","tag-game-coding","tag-game-development","tag-microsoft","tag-scripting-language","tag-unit-testing"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pgEc5-4c","_links":{"self":[{"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/posts\/260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/comments?post=260"}],"version-history":[{"count":0,"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/posts\/260\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/media?parent=260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/categories?post=260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/tags?post=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}