Japanese podcast about games: “Game chomp chomp”(ゲームモゴモゴ)

Recently I discovered an awesome podcast that discusses a variety of games, and one of the hosts actually works the business of developing them.

While you’ll need to know at least some Japanese to appreciate this, if you do it’s a great way to study up on Japanese as well as the art of game making.

You can more details about this podcast in my post here (on my blog about Japanese study).

Advertisements

Programming Trick: Speeding up your recursive loops with a creative technique

Lately, I’ve been working on a new game project which is very different than all the other ones I’ve done so far. I’m planning on waiting until it gets a little further before I write about it in detail on this blog, but I ran into an interesting technical challenge and I’d like to discuss how I solved it.

To implement the game mechanics I need a graph of nodes, with various connections between each node. I have a need to find the shortest path between two nodes, where the path is calculated by the sum of the length of all connections as part of the path.

I started out with a simple recursive algorithm that started from each node and worked backwards to find all possible paths to get to it, and used a map to store the best connection to move, given a certain intermediate node and a certain destination node. This worked really well, although after around 13-14 iterations on a graph with many connections it started taking over a second to compute and them getting exponentially worse.

One option was for me to search for a more proper algorithm to find the shortest path, and I am sure this has been perfected by  programmers over the last few decades. But I had a secondary requirement to also store all non-optional paths as well, so some of those algorithms probably wouldn’t suffice.

Looking at my recursive function, I realized that there really wasn’t much going on in terms of sub-functions it called that do any significant calculation. If there was, I could try to optimize those or cache up some result. But the question remained – what was taking up all the time?

Looking at the function again I saw that I was passing four different arguments into it. If you have ever studied assembly, you probably know that there are several instructions just to push and pull each of these to and from the stack, and since the function is called so frequently I thought that maybe this added up to a big chunk of the total time.

I decided to test my hunch by making all the parameters into global variables. I purposefully didn’t use class variables since I knew there would be extra lookups required, translating to extra work for the CPU.

It was a bit of work to make this transformation, since for non-pointer variables I had to manually save the state before calling the recursive function, and then re-set it. But this was only for two of the variables since the other two were pointers anyway which were supposed to change (one was the map which stored the nodes that had already been traversed so far, and I already had code to adjust that when returning from the recursive function).

The resultant code was a bit harder to follow, but the performance was significantly better. For the test case I was using I was able to get several more iterations before it got significantly slow (>1 second), if I remember correctly to at least 19-20 iterations.

If I keep adding nodes and connections eventually it still gets slow again, but I will probably put limits on these anyway so I may be safe with this solution for the time being.

Although I enjoy designing games from a high level, after many years of coding I still enjoy these types of algorithmic challenges, and the satisfaction of a well-tuned function.

Game development: To Engine Or Not To Engine

Since I started scanning the posts tagged with “Game Development” on WordPress, I’ve noticed a great deal of talk about game engines, in particular Unity and UE4. Although I knew such engines existed for some time now, I was a bit surprised how frequent they are used, and how even games created by a single developer were using these.

So I’ve been thinking a bit more about game engines and decided to do a post on them. To start with, what are the reasons and pros/cons to using one?

Though I mentioned two 3D Engines above, there is really a continuum of choices for game developers, with increasing reliance on other’s code and less on their own. I’ll list some options out, very roughly:

  • Assembly-language code  [all code]
  • Low-level programming language (C, C++, etc.)
  • Higher level programming language (Basic, etc.)
  • Some programming language along with an engine (Unity, EU4, etc.)
  • Game-development tool with simple scripting or visual-based logic blocks (RPG Maker, Game Maker, etc.)  [no or very little code]

Generally speaking, the farther down on this list you go, the less time you have to spend on coding as many things are already done for you, or can be done with a few clicks or a simple script. In exchange, you have more limitations on the types of games you can create, including graphics, gameplay, and other elements. If ten people create a game with RPG Maker, there are likely to be many similarities between these games, whereas if these games were made “from scratch” in a low-level language, they would probably have much greater diversity.

There was a time when I looked at those using RPG Maker and the like with disdain, thinking “those people aren’t really developing games!”. But after some thought, I now realize that their choice to use such a tool frees up their time to focus more on game elements, like story, gameplay, and balance. While it’s true that someone making games is less likely to “make it big” by selling their game to a large audience, I’m still very far from that goal myself and surely, the joy of the act of game making itself, plus the pleasure of having others play your game are too important aspects to game creation. [As a side point, as a career building skill I still highly recommend learning some commonly used programming language, such as C++ or Objective-C]

For me, I learned to program around the same time I was introduced to computer games, and programming itself is a very enjoyable activity. Just writing code, tweaking it, and seeing the results can be fun, though it depends somewhat on what the program is doing and how tedious the code writing is. Because of this, sometimes I can end up spending a great deal of time on tweaking certain parts of code, and not necessarily focusing that much on the “game-making” aspect. In any case, I consider myself much more familiar with programming than game-making per se, and have much more to learn with the latter.

Having said that, regardless of your experience level you can choose any of the above options, given you have enough time. So which do you pick and why?

First off, for 3D I think you pretty much are forced to use an engine, whether it’s Unity or something else. Back in the day when 3D rendering was a much less mature technology, it might be possible to calculate your own 3D-to-2D mapping and maybe even apply simple textures (think of the classic film Tron to get an idea of what I mean). But now with advanced lighting, textures, animation, and so many other things to worry about it’s nearly impossible to do all this yourself. I knew a guy once whose hobby used to be creating graphics engines for fun, but ironically he never really did much with them, since the engine itself was the end product. Unless you have a pretty large team you’re not going to get by with creating your own engine and consuming it.

My knowledge of tools like Game Maker is limited, but I’m pretty sure most of those are 2D only, so wouldn’t apply to making 3D games. If you want to focus more on level development and don’t mind little to no control over the gameplay, you can always use something like Minecraft or Blocksworld.

For 2D games, I think you have a little more wiggle room since it’s much more feasible to write your own code to do things like display a grid of tiles and scroll them. This is what I choose to do for my first iOS game, as well as the one I am currently working on. I considered playing around with something like Unity which has support for 2D games as well, but due to concerns about the learning curve I ended up not using it. From the little research I’ve done, it seems that one of the reasons to use something like Unity is for it’s level editor, which is something you really don’t want to make yourself. Also, you would have to worry less about performance of animations and scrolling since the engine would take care of those for you. Given the opportunity, I’d still like to play with Unity sometime, however.

In the end, I think we all want to be able to say “I wrote a great game”, as opposed to “I wrote a great program”. And as long as we can customize the end result to our liking, the player shouldn’t really care what technology or set of tools was used.

Visual styles for games

When creating a game, a key element is how you display information to the user visually, and this will have a major influence on implementation times, skills needed, and how easy it is to divide up tasks among multiple developers. In this post I’d like to talk about some of the different categories available, along with each’s pros and cons.

2D

Writing a game that operates in a flat space in two-dimensions is a very common style, and even now I feel it is the most common one, at least for games done by small teams.

With 2D, you have a few different ways to create your assets:

  • Pre-rendered from 3D
  • Hand-drawn (generally requires artistic skill)
  • Drawn in-game (vector, geometrical, etc.)
  • Photographs
  • Mathematically generated

2D games can be further broken down into a few genres depending on the gameplay and constraints of movement. Here are just a few:

  • Static single screen, cannot ‘go’ anywhere (ex: Tetris)
  • Horizontal scroller (ex: Mario)
  • Vertical scroller (conceptually same as the horizontal scroller, though gives a different feel)
  • User is only shown a small piece of a larger map, and can scroll around at will

Pros

  • Spend relatively less time on graphics, compared to 3D
  • Can potentially do all coding in-house without using a third-party engine (though you have that option as well)

Cons

  • Types of games that can be done is limited
  • More and more games are moving to 3D, so it’s easy to look dated with 2D

3D

Writing a game in 3D means that a three-dimensional space is represented, usually with light sources, textures, and a camera of some sort. This is becoming the norm for more and more games, and more and more 3D engines are becoming available for no or low cost.

Pros

  • Nearly unlimited options for game types
  • Can polish graphics to the extent they are photo realistic
  • If done well, can be very intuitive to users

Cons

  • Almost always requires a 3rd-party engine (Unity, etc.), or spending significant time on developing an in-house one
  • 3D-related development and related issues typically take a great deal of time
  • Requires special skills to make textures, models, and animations
  • If done bad, can look even worse than 2D
  • Making intuitive controls can be challenging, depending level of freedom given to the user

Isometric

Isometric is a unique perspective where objects farther away from the camera don’t get smaller. Q*bert was one of the first games with this perspective, and XCom 3 is one of my favorites. You can see a few screenshots of isometric games here.

Isometric can implemented either simple projections so that it is similar to 2D, or can be used by configuring a 3D Engine to appear isometric.

Pros

  • Has a certain ‘nostalgic’ feel to it for those who have been gamers for some time
  • Can look more realistic than 2D, while taking much less effort than 3D

Cons

  • Freedom of camera movement and world construction is limited.

Text only

This is a style for the most part extinct, but was headed by classic games like Zork        in the day.

Pros

  • Spent no time on graphics
  • Prototype very quickly

Cons

  • Severely limits the type of game which can be made
  • Small, niche audience

Sound only

This genre is pretty rare, even more so than text only, but I’ve seen one or two apps on the iOS app store recently that use a combination of high quality sound effects and stereo sound to create a immersive experience.

Pros

  • Spend no time on graphics
  • Can be seen as “creative”

Cons

  • Very limited
  • Requires special skills to properly record sound effects
  • Small audience

Combination styles

The sky is the limit for which styles you mix and match. For example, classic Sierra games used a good mix of 2D graphics with a text-based interface. The Super Smash Brothers series uses 3D but in a mostly 2D way, emulating true 2D fighters like Street Fighter. Some games also use a combination of 2D and 3D, either simultaneously (like showing a 2D map at the same time as traveling through a 3D landscape), or switching between styles in different areas.

Though it’s probably rare, you can even switch styles in the middle of development – starting with a 2D prototype to prove the concept then switching to 3D for final implementation, or falling back to 2D when you realize 3D is not worth the effort for the given gameplay.

Lessons learned doing sound on iOS – the real story

A few days ago I wrote an article about using sound effects in iOS games. Just today I was doing some more testing and realized I had made a major mistake, and some things I said in that post were wrong. In this post I’ll let you know what I had did wrong.

I alluded to a single line change that would allow playback of MP3 files. Here is the specific line:

NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:sampleName ofType:@”caf”];

I had simply changed the “caf” to “mp3” and since I was getting sounds I assumed things were working. The weird staticky sounds I got I explained by saying that several of the same sound were colliding and causing such an effect. Though sound collision can be a problem and sometimes it might make sense to have several variations of a sound effect, what appears to have been actually happening is that OpenAL didn’t know how to process MP3 and so it was just generating random noise.

After a tried a few different .MP3 files and they all sounded pretty much the same, I finally realized what was going on.

The solution was to return the code to use “caf” files, and use a line like the below to convert MP3 files to CAF files.

> afconvert -f caff -d LEI16@44100 chop.mp3 chop.caf

After doing this, my sounds finally started playing properly. I did find some strange effects that occur when sounds overlap quickly, but they were more subtle.

One additional step that is good to know is you will need to manually tell the .caf files you added to your project that they need to be included with the bundle that gets copied to the device. You can do this by dragging the file under XCode’s Build Phases => Copy Bundle Resources. I didn’t notice this problem for MP3 files, as it seems smart enough to add the to this list automatically.

The strange thing is I would expect OpenAL to throw an error somewhere instead of just spitting out noise. So either this set of APIs is pretty badly designed, or there is some code in AudioSamplePlayer that isn’t handling things properly. Either way, I got things working so not going to pry any deeper at this point.

Game development as an act of creation, and learning to step outside oneself

Despite the fact that game development can involve many roles (story designer, art designer, gameplay designer, etc.) which can be filed by many people, ultimately the game produced is a creative work influenced from the individuality of each of the people involved. This means things like biases, world views, and what makes a fun or interesting game to each person all factor into the end result.

I believe we all have blind spots when thinking creatively, whether that is game design, a visual art, novel writing, or even musical composition. For example, something which we feel is obvious or logical may not be the same to another person, or even to most people. That’s why in all these creative arts, it’s good to have someone to bounce ideas off of and get feedback: “Does this make sense?”, “How does this sound?”, or “How does this look?”.

Herein lies a problem, because all these things are inherently subjective. We could ask 100 people to review a creative work, and they might all give different opinions and suggestions. This is why it’s best to be very picky about our reviewers, and in the domain of game development this means being picky about who tests our games, and what weight we give their opinion. We also can use basic statistics, like putting more weight to any feedback that was given by a certain percentage of people. If 80% of people say the UI is hard to use, then it would be pretty unwise to ignore that feedback.

As with all things, budget comes into play here since if we are not willing to spend money, our best bet is to be lucky and make friends with someone very experienced in playing and/or designing games, who also possesses the communication skills to clearly communicate what works and what doesn’t. Another option is to just get as many people as we can to help out, gather their feedback, and then worry about how to value it later. If your budget for your indie game is 0$, then you’re options are pretty limited here.

Fortunately, there is one more thing we can do to help – become our own best beta testers. That is the art of stepping out of our everyday thoughts and trying to act like a third party. This can involve playing devil’s advocate (“is this feature really easy to understand?”), or trying to think in detail about what the “average” person would think (“so-and-so game is popular, so many people would quickly grasp this element”).

In the field of music, we can get a good idea of what a third party would hear by recording ourself and listening to that with all our attention, since during the actual performance we are worrying about so many other things it’s hard to really listen to what sound we are putting out and the overall feelings it invokes. For novel writing, you can try to read through a chapter you wrote after waiting a few weeks, since then you’ll be closer to what a new person would experience. At least I found this to be a good practice.

For game development, I think getting into a neutral state where we turn off our biases is very difficult, but actively trying to get into that mode can help us train little by little.

[Image credit: http://www.gratisography.com ]

Lessons learned doing game sound in iOS

[Note: there are some problem with the conclusions drawn in this post. See this follow up post for corrections]

For my current mobile game project, I have a need to do both background music, and sound effects.

In a previous project, I used AVAudioPlayer to loop a MP3 file continuously in the background, and you can see an example of code to do that here (just make sure you call setNumberOfLoops to “-1” if you want infinite looping). For the sound effects, originally I planned to use the same API, however there are some problems with this. The biggest of these is there can be a large delay (a second or more) between when you kick off playback and the sound actually plays. This is fine for background music, but for a series of quick sound effects (imagine the sound of bricks falling, like in Tetris) it just doesn’t fly.

One of the other ways to play sound files with a bit more control is OpenAL. The documentation by apple (here) is pretty good, but it has a pretty steep learning curve, especially if you are like me and didn’t want to spend hours on experimenting. One of the reasons for this is if you want to do multiple sounds you have to do extra work to manage the simultaneous sounds, since unfortunately OpenAL doesn’t do this out of the box.

Fortunately, someone put together a class called AudioSamplePlayer (get it here) which covers much of the basics needed for playing simultaneous sounds, plus the added bonus of easily modifying the pitch and volume of them. The class is pretty easy to use, with the only minor annoyance that you need to pre-load sounds. However, this is understandable since it is designed to improve performance by avoiding loading each sample every time it is played. The licensing of the code is also such that you can use it pretty freely (but be sure to read it in full before using it yourself).

With AudioSamplePlayer you can save some development time, but you may need to do extra work depending on your requirements. One minor tweak I made is to allow .mp3 files (instead of just .caf files) which was a simple one line change.

A bigger problem is that if you game has the possibility to play the same sound quickly in repetition, the sounds can overlap and the resultant sound can sound strange (tinny or mechanical). For example, if you were playing the sound of each rocks hitting the ground in a simulation with 1000 rocks, some would likely occur near the same time.

In this case, one option is to limit the number of simultaneous sounds (and the above class allows this with a simple constant), but that can lead to unexpected behavior as well, especially if your sound file is relatively long (this includes and echo or space at the end of the file). You can also write logic to use a few different sounds to avoid exact-file collisions, or modulate the sounds in some way. You could even delay sounds a bit to reduce collisions, but again that would often be undesirable since the visuals would be out of sync with the sound.

In my case, I am considering just allowing the sound collisions since the resultant effect sounds sort of cool and seems to fit the atmosphere of the game.

Your next question is probably where to get the sound effects, since you may not want to record them yourself. There is a bunch of free sound effects sites on there, but I’ve found this site to have many interesting effects which can be used given the condition that you attribute the site.

References

http://www.raywenderlich.com/69369/audio-tutorial-ios-playing-audio-programatically-2014-edition

https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html

http://ohno789.blogspot.com/2013/08/openal-on-ios.html

http://www.freesfx.co.uk