PDC 2008: Cross-platform .NET surprisingly makes a fast game scripting engine
An open source project to make a common language runtime for Linux, Mac, and iPhone that's .NET-compatible, has ended up succeeding in an area no one may have expected at first: as an artificial intelligence provider for gaming engines.
LOS ANGELES - The annual Microsoft PDC conference is perhaps the least likely place you'd expect to find a demonstration of an open source programming and scripting environment that runs on Linux and Mac. But the scripting language in this case is C#, created by Microsoft. And what's most impressive about Mono, the open source implementation of the .NET Framework CLR (which also, by the way, has a version for Windows) is that it's being implemented as host of a replacement for the scripting language in one of the most popular cross-platform MMO gaming engines: Unity3D.
"A game is an engine that has to render between 30 and 60 frames per second; it has to take user input, it has to take network input, it has to run the simulation, figure out where objects are...It needs to invoke all of the enemies' AI, and the enemy needs to make a decision on a given position, all of this stuff. And eventually it can push this to the rendering pipeline and the audio pipeline, and update this on the screen. And you have a very short amount of time to do it," explained Miguel de Icaza during PDC on Wednesday. De Icaza is best known in the development community for having spearheaded the GNOME project, one of the leading GNU-licensed desktops for Linux.
But more recently, he's been leading Mono, which is enabling .NET to compete against Java on platforms that Sun thought it might have all to itself.
"One of the problems that people were talking to us about was, it turns out that a lot of games have limitations on how much AI they can put on a game, because it's the slowest piece of everything," de Icaza continued. "So it turns out that the display, the simulation, and the supporting infrastructure tend to be built on C, C++, or assembly language. There are people tuning shaders, tuning physics, using templates and that sort of thing. But when it came to the game logic, this is not something that you want to render in C++. This is something that you wanted to keep high-level, you want a different team of people to actually build that, so you have people from MIT doing the graphics and the simulations and the physics. But you want an artist to be tweaking the behavior of light, and you don't want those guys forgetting to free a blob of memory, so you give them a safe language.
"And what has happened in the game industry for the last eight years or so, people have adopted LUA in droves." LUA is a so-called lightweight scripting language. It's easy to embed in other systems, it's comfortable to developers who already understand C, but it's slow. And with game programming being sequential in nature -- update the screen, check for user control, perform cleanup, do some AI, rinse, repeat -- the time slice for the AI scripting language to operate is becoming narrower and narrower by virtue of quicker graphics.
But development houses don't want the AI team to have to deal with the graphics team, lest too much communication end up slowing down the process somehow. So the guys doing what used to be called "gaming theory" end up using a safe scripting language like LUA, or maybe UnrealScript. "The safer the language is, the slower it is," said de Icaza. "It's easy to program, but you lose some performance."
The Unity3D engine and development environment were originally built in Objective-C for Mac OS. But after having started out with its own scripting language, de Icaza demonstrated, Unity switched to Mono, which enabled all of the "living" objects inside of Unity universes to be scripted using the Mono implementation of C#.
As a demonstration, de Icaza ran some of Unity3D's test universes, including a kind of virtual jungle where a species of wild stork is simulated using instances of bird objects. All of the birds as a class, or each bird as an instance, is scriptable directly in the IDE using C# code. In fact, adding the code triggers Unity3D to add class members and their running state to the IDE itself, so they can be changed graphically as well.
Just to show how versatile C# has become in this environment, he created a class of spotlight which blinked on and off within this universe at a random interval. Doing this next to one of the birds caused it to be scared -- a behavior which was also controllable using a C# script.
The move to Mono has sped up the performance of games created with Unity3D by unbelievable double-digit factors, and has also improved the compile time of live objects -- as de Icaza showed attendees first-hand -- by an astounding 3000%.
"What we've done with Mono so far is, we've managed to get the game AI to be faster...But we were talking to another game company," related de Icaza, "and they were telling us, 'You know what, we have been using C# for the entire game on the client and the server, and it actually has improved our productivity tremendously. We're actually way ahead of schedule than what we would have done with C++."' And the amazing thing about that, he continued, was that this expediting of processes was on account of implementing managed code -- something which on paper, you'd think, would be slower.