Latest News
spacer View All spacer
 
February 10, 2010
 
Analysts: EA On The Right Track At Last
 
GamesBeat@GDC Confirms OnLive, GameStop, PlayStation Home Speakers
 
Ubisoft Q3 Sales Edge Down, As It Ramps Up Big Franchises
spacer
Latest Features
spacer View All spacer
 
February 10, 2010
 
arrow Television, Meet Games
 
arrow Two Halves, Together: Patrick Gilmore On Double Helix [1]
 
arrow The Road To Hell: The Creative Direction of Dante's Inferno [20]
spacer
Latest Jobs
spacer View All     Post a Job     RSS spacer
 
February 10, 2010
 
Konami Digital Entertainment Co., Ltd.
Programmer
 
THQ
Animator - Motion Builder (contract)
 
LucasArts
Senior Systems Designer
 
Trion Redwood City
<b>Sr. Brand Manager</b>
 
Telltale Games
Game Designer
 
Telltale Games
Senior Software Engineer - Core Technology
 
Airtight Games
IT System Administrator
 
Roblox
Apple Game Engineer - Kids' Virtual World
spacer
Blogs

  Using C# For a Commercial Game
by Alistair Doulin on 11/17/09 03:38:00 am   Expert Blogs   Featured Blogs
27 comments
Share RSS
 
 
  Posted 11/17/09 03:38:00 am
 

[This is a repost from my blog, doolwind.com]

Does the title of this article make you cringe? People have mixed feelings when it comes to C# and the .NET framework. 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. This article outlines my reasoning for using C# for a commercial game on PC.

Rapid Application Development

The primary reason for using C# is because coding in it is more productive than any other language I've used. Language features like interfaces, generics and delegates make it a cleaner language than C++. Development practices such as unit testing and dependency injection are also easier to use in C#. This clarity translates to more readable code and less bugs, particularly when working with a larger or younger team of developers.

The second productivity gain comes from having access to the .NET framework. From its XML handling for configuration, to built-in math libraries, many of the core functions you can think of are taken care of. This saves you writing the code yourself and lets a reliable, tested framework do the heavy lifting in areas you don't want to spend your time.

C# lets you focus on making a game rather than writing an engine, or learning someone else's engine.

Unified Code - Client, Server, Tools, Scripting

  • Server - I'm writing the server architecture (matchmaking, leaderboard, etc) in ASP.NET and C#. This lets me share libraries between the client and server and makes calling web services from the client a breeze.
  • Tools - All our tools are also written in C#. This means that our tools can have rendering within them, can reuse libraries and simplifies the creation of in-game tools.
  • Scripting - As I've mentioned in a previous article, I'm using C# for the scripting language in our game.

Better Software Development

C# supports better software development practices in a number of key ways:

  • Refactoring Tools - Visual Studio has great refactoring support such as renaming identifiers and extracting methods & interfaces.
  • Separation of Concerns - Splitting a project into separate .dll's is trivial in C#. This helps with maintainability and encapsulation of code and encourages good separation of concerns.
  • Unit Testing - C# has great support for unit testing through NUnit or MSTest. As I've previously discussed, unit testing fits perfectly in certain areas of game development. The easier unit testing is to achieve, the more people are likely to adopt it.

In-Game Web Browser

As part of the game engine we are developing we decided all interaction with the server for matchmaking would be through http/html. This reduces the need for creating such a complex in-game UI system and simplifies communication with the server. The .NET web browser control is extremely easy to use and communication between the browser and host application (the game) is a simple.

Run-time isn't too large

I'm currently targeting the .NET 2 framework. This put's the footprint at around 20MB. The latest .NET frameworks (3.5 and 4) both have a "client profile". This is a subset of the .NET framework that comes in at about 30MB. 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). MS is currently planning to make .NET 4 client profile a windows update meaning the .NET framework will finally become (almost) ubiquitous among PC's running windows.

Negatives

There are obvious drawbacks to using C# for a commercial game engine which we took into consideration when making our decisions.

  • Not Cross Platform - While engines like Unity have proven that C# can run on multiple platforms, this does not occur out of the box. As we're targeting PC's exclusively for our first release this was not a problem.
  • Xbox run-time not great - C# can run on the Xbox as part of XNA game studio, however the run-time performance isn't great. Garbage collection is a particular problem which requires refactoring code to achieve acceptable frame rates.
  • Harder to use C++ libraries - Linking to straight C++ (non-COM) libraries is not trivial like it is connecting to other .NET libraries. Extra time is required if there is a requirement to use existing libraries such as RakNet.
  • Some performance concerns - C# does have some performance concerns with garbage collection and certain double math operations. However the algorithmic gains received from working with such a great language outweigh the small performance issues in certain areas. As with any language, bad code will run slow.

Conclusion

C# is a great language. I thoroughly enjoy coding for it and I find it makes me more productive when creating games. I plan to release my MVC engine in the future as an open source project. Until then, I'm continuing to develop our engine and I'll keep you up to date with the progress.

For reference, here are a couple of commercial games using C#:

Arena Warshttp://www.arenawars.net

AI War Fleet Command - http://www.arcengames.com/aiwar_features.php

Sacraboar - http://www.sacraboar.com

Have you used C# for game development? Are you sick of chasing memory leaks and crashes in your C++ applications? Do you think game developers will move on from C++ now or in the future?

 
 
Comments

Nick Janssen
profile image
Hello,

I have had two years of C++ at my game design course, and now at my last year we are learning C#. I must say that C# is such a wonderful language because it is so productive and efficient to write compared to C++. Yet I've heard that C# programs can be decompiled, do you think that this is a threat to developing?

Nick

Alistair Doulin
profile image
Nick-

I've heard your concern voiced by a number of people over the years. There are a number of "Obfuscators" which can protect your code and stop it from being decompiled.

An example is Smart Assembly - http://www.smartassembly.com/

James Hofmann
profile image
I've always been on board with the idea of using the highest possible levels of abstraction for game code, wherever it's possible. While I've taken on programming as a major skillset, I'm more interested in game design than engine programming, and fighting C++ gets in the way of that. To give some perspective into just how much power we have now:

http://prog21.dadgum.com/52.html

The sticking points, as I see them, have been a combination of memory-scarce console hardware in the last generation, and game engines built around an emphasis on the CPU-heavy rendering/collision tasks. Most games don't require high-performance code for the game rules or AI scripting, but games with a sizable budget continue to push the limits on real-time rendering, animation, and physics. If you can drop those requirements(and if you're going for the indie/low-budget approach, you probably can), then, magically, you can get away with a hell of a lot of CPU overhead and an old-school single-threaded architecture; if you can squander hundreds of megabytes of memory as well, "stupid" approaches to data management and VMs with massive memory overhead become palatable options too. (comparisons of VM memory consumption: http://shootout.alioth.debian.org/u32q/fun.php?d=data&calc=calculate&xfullcpu=0&
xmem=1&xloc=0 )

C# is just one of many options - I consider it one of the more conservative possibilities, but still a big step forward. I've spent a very long time on the question of "what languages would help me make games, by myself or with a very small team, better/faster?" My preferences have developed towards engine code in languages/compilers with inferred type systems (for ex. ML, haXe, Scala...C# is among them, but generally, I'm looking for full H-M inference or as close as possible, which isn't the case with C#) and then hand-rolled Forth-type DSLs on top for each domain-specific problem: UI, AI, cinematic scripting, etc. That way, the basis of the engine can benefit from type-checking and static analysis, and the design-facing parts can be iterated with compact, low-boilerplate code, since Forth-type languages are easy to implement and extend.

General-purpose scripting languages become much less interesting in this architecture - while they let you implement any algorithm in a script, if you're already using a high-level language in the engine, there isn't much reason to do so. DSLs get "to the point" of iterating on each feature and they help provide the bulk of the gameplay-related data, so you can get away with fewer and lighter-weight tools and custom data formats.

Nick: People who wanted to pirate software or steal bits of code were doing that by decompiling, modifying, and copy-pasting machine language 20-30 years ago. It's never been a difficult technique, just time-consuming and unnecessary for most "honest" coding. Compilation is just one weak form of obfuscation, and if you want to do more, as Alistair suggests, there are options to do so.

Michel Carroll
profile image
I only learnt .NET languages in my Computer Programming college course, so I'm now learning C++ on my own.

I found that learning C++ gave me a much greater insight into the memory use of a program. Programming in C# is now a no-brainer compared to C++. Therefore, in my opinion, to become a great programmer, learning C++ is not optional.

Ernest Huntley
profile image
@Mike - I agree that learning C++ (and C, probably) is essential to becoming a great programmer. The insight they give you into the lower-level workings of computers and other languages is certainly invaluable.

Although, I think that, once having a good foundation built, moving on to a language like C# (which I think is a spectacular language!) for reasons of productivity, clarity and ease-of-use is a wise choice.
The biggest stumbling block that is keeping more developers from making that move (ok, aside from portability) is probably stubbornness. We are a bunch of mules aren't we? Kicking and screaming when we are faced with change to something more abstract, despite the fact that it might be a fantastic tool. We feel like our pride will be destroyed. That we can no longer brag about our ub3r l33tn3$$ and pointer-to-a-pointer-to-a-pointer arithmetic. *sigh*

Great article. I hope more developers hop on this bandwagon!

Guilherme Campos
profile image
This game was also made in C# - www.mothergaia.com.br/cityrain

Andrew Grapsas
profile image
All I have to say: MONO. It fixes your portability problem. Additionally, with the next release, there should be increased garbage collection optimizations.

It makes the feasibility of developing a game in C# more promising. That being said, it's still difficult (as mentioned in the article) to interop effectively with native code.

My personal feelings for game development are, "don't limit yourself to one language."

Each language has its strengths and weaknesses. With the right design and understanding of architecture and libraries, various languages can interact without fear.

Check out the event collaborator architecture!

Chris Howe
profile image
C# and Mono ftw! I saw that Unity is planning 360 support, which I assume means a 360 port of Mono too? Even if they keep that to themselves it shows that it is possible. Using C++ to embed a JIT-compiled platform such as Mono, .Net or JVM is the way to go IMO.

"Check out the event collaborator architecture! "

Do you have a link or something? Google is failing me today. :)

Andrew Grapsas
profile image
Sorry, it's "event collaboration", my bad :)

http://martinfowler.com/eaaDev/EventCollaboration.html

Kai Steinmann
profile image
.NET is my preferred platform. That being said, it's like everything else. It's goof for some stuff, not for others. Should you try to make the next Unreal killer in C#? Probably not. Could you make any number of other well performing games in C#? Sure you could.

Also, check out Unity3D. It gives you all the beauty of writing in C#, and it runs on Mac, Windows, your browser and the IPhone. Oh, and it's free.


Nathan Rivera
profile image
C# is a great language. I would love to see managed languages became the standard for game coding. Like you said, the ability to use Agile practices (refactoring tools, nunit, etc.) is very valuable.

A lot of the COM interop issues are being addressed in C# 4.0:

http://blogs.msdn.com/samng/archive/2009/06/16/com-interop-in-c-4-0.aspx

Joe Cooper
profile image
We use Java which is a stone's throw away from C#. I use for our flagship product as well as personal game projects I do with friends and tools.

Garbage collection has been an issue, at least with video playback. If you want to sustain a solid frame rate, it does not help to have the GC pop in every several frames. Fluid motion was especially key. I wound up having to code "around" it by subclassing a BufferedImage to make a "recyclable" image class that could be re-used while shuffling data between FFMPEG and Swing.

The memory management is a key feature of managed languages and is a large part of what makes them conducive to RAD.

If it becomes an obstacle that you have to actively work around, than these advantages don't actually apply anymore.

However in this particular case it was only a tiny piece of the program and overall it has been a huge benefit using Java.

There have been other issues but they wouldn't apply to C# or .NET.

Ian Morrison
profile image
One of the advantages of C# mentioned in this article is the refactoring tools Visual Studio provides. That's great, but it DOES bear mentioning that products like Visual Assist X add some pretty sweet C++ refactoring tools as well, which are real life savers. Not free, but at this point I can't imagine working with C++ in Visual Studio without it!

Still, between the advantages you mention and mere curiosity, I DO intend to learn C#. Who knows, maybe I'll even dig through XNA and try to make a game out of it. ;)

Alistair Doulin
profile image
James Hofmann-

I agree C# is just one of the many options of "newer" languages. I personally find it's at the right abstraction level for productivity while still have enough control over performance.

A DSL for each domain would be a very interesting. This would take the regular exposing of certain functions to script a step further to empower designers even further, and reduce the technical barrier if it's more "natural" to them.

Alistair Doulin
profile image
Michel Carroll-

I completely agree that to become the best game programmer you can, learning C++ is a good step. The point I'm making is that C# is a better use of your time when rapidly developing games. Having memory management in the forefront of your mind is unproductive, but that is no excuse for not knowing how it works. Having a solid understanding of core programming principles and how the .NET framework acts is vitally important for all but the simplest of games.

Alistair Doulin
profile image
Ernest Huntley-

I agree with your points about C++. Portability seems to be the biggest concern from most "older" game programmers I talk to. Stubbornness does come into the equation, but not as much as I saw from people making the switch from C to C++. I know of a number of senior programmers at studios that still swear that C is the only true option for game development. This kind of stubbornness will never die :)

Giuseppe Navarria
profile image
Depends how the commercial game is. If you want performance even on low end machines, forget about c# or at least about using only C# libraries. Using C++ libraries for physics and such and C# to handle the game logic could be a good solution.
I still think using a C++ graphics engine is much more profitable than using XNA, for example Ogre is actually much easier to use than XNA and has a lot of stuff XNA doesn't. (XNA is more a DirectX wrapper than a real engine)

Doug Poston
profile image
I would have to add the fact that C# is owned by Microsoft into the 'Negatives' category.

I don't have anything against Microsoft, and C# is great for tools, but there is a risk that Microsoft can take legal action to pull the use of C# on non-Microsoft platforms. How big this risk is I don't know, but it does make me hesitant to use it outside of tools (I also worry that schools are teaching C# and not C++).

Again, not Microsoft bashing here (they're great for developers). Just something to think about.

Chris Howe
profile image
@Guiseppe: XNA also has a lot of stuff that Ogre doesn't, such as input, networking, sound, etc. I tend to agree though that the way forward is not pure C#. Using C# as an embedded scripting language within a C++ framework might be the best way (which is what I was arguing for here -> http://www.gamasutra.com/blogs/ChrisHowe/20090808/2701/An_Argument_For_Mono_In_G
ames.php).

@Doug: C# is not owned by Microsoft. It was created by them, but it is now a standard. As I understand it there are potentially some patent issues in the Mono implementations of various .Net libraries , but these have been largely cleared up (see the Microsoft Community Promise).


Curt Perry
profile image
C# was invented by microsoft, but not owned by microsoft.

Microsoft relinquished property rights to ECMA (ECMA-334) and ISO (ISO/IEC 23270)

Doug Poston
profile image
Thanks for the info, that does make C# a lot more attractive.

Alistair Doulin
profile image
Andrew Grapsas-

Thanks for the extra info on Mono. I must say I haven't spent a great deal of time looking into Mono. I'm interested to hear other people's experiences with writing a game for Mono as well as any open source projects I can use as a reference.

As for not limiting yourself to one language, I completely agree. One great advantage of targeting the .NET framework is there are now countless languages you can use. From F# to Python you have a choice of which language to use to solve each problem.

Alistair Doulin
profile image
Kai Steinmann-

Completely agree with everything you said :)

I'd also like to add that Unity3D is a great platform (not just because it uses C#) and I'd recommend anyone creating a 3D game to look at it. My main reason for not using it (or other engines out there) is that I'm currently working on a 2D game which was going to be more pain than value in using a 3rd party 3D based engine.

Alistair Doulin
profile image
Chris Howe and Curt Perry-

Thanks guys, you beat me to it. C# is no longer owned by C# removing this issue of ownership.

Ayotunde Ayoko
profile image
excellent article. i find C# very attractive(cleaner),easy to use and understand. i like the XNA framework prospect too (although in my opinion, it might take a while for C# games to be 'more commercial', i foresee them being along the 'indie' path)...i am creating a simple demo with it. but i didn't know about the unity3d engine(which uses .net) until now.

execellent article again :)

jeffrey gassman
profile image
Hello,

My son is interested in creating computer games. What books and software do you recommend for him to get started?

Thank you!

Quentin Smith
profile image
While I do believe C# is beginning to vie with C++ as a realized game development language, C++ is still the bread and butter of software engineering. Knowledge of C++, I believe, against all other languages gives a developer an air of knowledge and professionalism. I could not imagine entering the field of game development without a solid C++ background. Throughout my experiences working with C# developers, those who lack confidence with C++ often fail to fully realize the responsibilities and requirements of their code, and interfacing with 3rd party APIs.


none
 
Comment:
 


Submit Comment