Mon, 27 Feb 2023 12:00:00 +0000

How making games can help us use Swift everywhere!

Game Development And Swift

TLDR; I will be using game development fundamentals as the foundation for using Swift outside of Apple's ecosystem.

In today's article we're going to take a high level overview of game development and explore the current role of Swift as a tool used for making games and how we're going to use it in our journey for using Swift outside of Apple's ecosystem.

Current State of Game Development

One can get a decent idea on the current state of game development without any game dev experience, by simply browsing the Steam store. For those who don't know it, as of today, Steam is the largest PC game store out there.

Whether you're an avid gamer or not, one of the first thing you'll probably notice after spending a few seconds on the front page is just how large of a catalog for games there are.

You'll probably catch a glimpse of some of the big AAA names out there which have the funds necessary to advertise on main stream media and hire thousands of workers.

But you'll also likely be met with a ton of smaller offerings, made by independent studios and smaller teams (sometimes even solo developers). And if you choose to search for similar listings like this, you'll feel even more overwhelmed than you might already.

Having grown up using Steam, I like to think I've got a decent enough grasp seeing how much its grown since the beginning. When it first launched, it was mainly used to sell the games made by the company who owns it (Valve). Since then, it's exploded as a defacto spot for PC gamers everywhere.

But Steam is one of many digital storefronts that exist today on the market. Bringing it back to our Apple roots (since this is a Swift blog ofcourse), you'll notice that the App Store on your phone has its own tab for games.

The point I'm trying to make is, the gaming market is pretty large now and feels like it only keeps growing. Both in complexity of the offerings available and the amount of developers out there flooding the market with new titles everyday.

Part of the reason for this explosion in growth was thanks to the evolution of the tools available today for game developers. Making games used to be a lot less accessible to the majority of people.

More often than not, you were formally educated on the subject, or knew someone already in the industry. You either had your own tools or code base you worked on over the years that grew over time, or you had to pay for expensive licenses from commercial game engine providers like Unreal.

The subject matter to start was also more complex. You most likely needed a background in Linear Algebra or applied physics. You had to genuinely understand what you were trying to do with your code to be able to work with it, and model it after mathematical concepts that required prior study.

Resources were also more limited, existing on the fringes of the internet in unique forums tailored for the area by industry professionals.

Now a days however, anyone can become a game developer as long as they have internet, patience, and can search for videos on youtube. The market is way more accessible thanks to the tools and license structures available now.

You can pick from a myriad of free to start game engines out there like Unity / Unreal Engine / Game Maker, etc. These tools also come with a bunch of plug and play assets other people made that you can download and use in your own projects.

That means even as a fresh developer, you don't even have to start from scratch anymore. What was once considered a massive undertaking requiring tons of labor and specialization before, has now been streamlined (as is evident by content like this)

Despite these advancements, game development is still a tough line of work don't get me wrong. Most projects stay as hobbyist endeavors or never get released. Having experience that was valuable 20 years ago, still makes it generally easier to navigate this landscape today too.

But the general consensus is, game development has become more accessible across the board and so to have the under the hood concepts that have fueled the industry at large.

Swift in Modern Game Development

Can you talk about Swift already?

Fine.

Can you even make games with Swift?

Yes.

What's the catch? Wait don't tell me, it's -

Yes, you have to use Apple's Libraries if you wan't to make games in Swift in the least painful way possible.

The most popular language for making games now a days, is generally C++ still. Without going too much into detail or the background behind it, C++ has been used for decades in game development. It's fast, runs on most platforms, and just has a lot of history built on it.

Unreal Engine uses C++ even. Other languages exist in this capacity too depending on the tooling. Unity uses C# for example. Other engines may use a lighter scripting language like LUA.

But Swift, an Apple 'first' language, is mainly used for writing games on Apple's platforms.

Much like their general app GUI libraries like UIKit, they also offer some for making games and rendering 3D software. SpriteKit and SceneKit. SpriteKit is generally used for making 2D style games like the original Mario games. SceneKit is more for 3D games.

Unfortunately, if you put in the effort to build a game (an already heavy task) using one of these libraries, you generally fall under the same trap that you do when making apps with Swift. That is, you can't take it anywhere outside of Apple's System.

Using Swift to write games with other libraries that aren't SpriteKit and SceneKit, is still relatively new territory. There are some frameworks being developed to do this. Some that run on Web Assembly (since Swift supports that).

Other libraries that use Swift on top of a lower level game dev framework also exist, like Swift for Godot. This approach involves writing Swift code that gets translated to be understood and control a more formal, pre existing game framework.

But nothing major is out there yet. Nothing you'll see 'running in production' easily. There's a lot more to making a game than just choosing the language you want to write it in. Like assessing what limitations exist with your toolset.

One of C++'s advantages for game development is it's portability. Generally speaking, its easier to reuse the same game you wrote in C++ on different platforms, with relatively minor tinkering needed to port it between platforms. A game written in Swift or other languages, might not be the same.

How does this help us?

So the idea behind this thought exercise about understanding the current state of game development and Swift, is to try to leverage the benefits of game development in aiding us to use Swift outside of Apple's platforms.

We covered that Swift game dev is pretty much locked into Apple's Ecosystem (unsurprisingly), and that game's made in C++ are generally portable across platforms. What if we were to interface with Swift in a similar manner?

Game's are pretty complex application's that offer just about anything you could wan't to do that a simpler app can. They draw stuff on a screen, they provide pleasing animations, text based data, data fed to you from a network, etc.

Maybe we can try to work with Swift under the context of game development to help us use it in a more general sense outside of Apple's ecosystem. This wouldnt be the first time something similar happened.

The Mono project was created to make C# (a once microsoft proprietary language) cross platform and usable elsewhere. This is actually the backbone of Unity's game engine infrastructure, and Unity is used to make most games on the App store!

LibGDX was designed to make cross platform Java games too. Even the hit game Slay The Spire was written in it and now ported everywhere.

So I have hope that endeavouring to use Swift in a similar capacity should not only work, but be super enjoyable.