Wednesday, May 20, 2020

Writing Console Quality Javascript Games in the ES6+ Era: Part 1

When I tell other game development enthusiasts that I write video games for the web in JavaScript, I get a variety of interesting and stigmatized responses back.
  • Why wouldn't you just use a game engine like GODOT, Unity, GMS, or Unreal?
  • Don't you run into performance issues?
  • As your game grows, how does it scale?
  • Isn't coding your own game engine super hard?

These responses typically follow the same underlying theme: JavaScript isn't well suited for writing video games.  Well, please have an open mind, grab your favorite beverage, and allow me to dispel many of these myths about developing high performance, high quality games that run on the web.

JavaScript, not an inferior tool, it's a tool with a totally different set of strengths and weaknesses


While it's nice to be able to use a one stop solution like GODOT or Unity, I propose that writing a video game in JavaScript isn't just easy, it's also beneficial to you as a developer.  It's especially beneficial if your day job is writing enterprise code as a software engineer.  In addition to that, JavaScript brings a ton of great features to the table that you miss out on with other solutions.  In some ways it's certainly less convenient, but for every drawback, there's a benefit.

A great example of where JavaScript shines is the NPM ecosystem.  NPM is the world's largest software registry.  With almost a million packages, you have a massive number of resources you can put to use in your indie game project.  Installing NPM packages is as easy as running a console command, and with the standardization of the ES6 module system, making use of the features of each package is relatively straight forward.

At this point, you might be asking: "But... aren't you limited to the web as your platform?".  The short answer is No.  There are number of products out there dedicated to shipping web based games to your alternative platform(s) of choice.  The most popular among them is Apache Cordova.

But, what about Performance?  JavaScript is everywhere.  Because of this, a huge effort has been placed on performance enhancements over the years.  With optimization advances to the most popular JavaScript engines(like v8), you're looking at near native performance out of the gate.  This is without even using Web Assembly(which I do like, by the way).  In fact, in my boiler plate JavaScript game framework project, I'm getting frame draws and engine updates happening at sub-millisecond speeds, 60 frames/second in 2D on a very old machine(AMD FX 8350).  This is PLENTY fast enough for almost all 2D games on the latest hardware.


Okay, it's fast, but why choose it over other game engines that are also fast?  In many cases, choosing a turn key game engine solution(like Godot or Unity) is a good choice.  However, for those of us that know JavaScript and are interested in the control and flexibility it provides, there's a strong case to be made to use an ES6+ project as a platform for your game.  For instance, if you use JavaScript professionally, the skills you'd use to create a game using JavaScript translate well to your other projects.  Conversely, the tricks you've picked up along the way as an enterprise programmer translate directly to your game project.  Not to mention, JavaScript has recently become the most popular language choice for programmers, worldwide.  Perhaps not for game development, but I consider it a great stepping off point to introduce enterprise developers to the world of indie game development.

In addition to good performance, the popularity of JavaScript has spurred almost every database, communication and framework shop to write interface implementations for JavaScript. 

The reasons are numerous(more than I've mentioned here), and the capabilities are more than adequate.  If anything, don't rule it out as being a viable option, because in my experience it can certainly hold it's own against the giants in the industry.

In my next blog post, I'll start to go over my boiler plate ES6+ game dev framework project, and how you can use it to write high performance video games for the web!