Hello! Welcome back to another installation of the Blightmare Development Blog. It’s garbage day in my neighbourhood today which ties in nicely with this two part topic.
I was working on ironing out some bugs that we had found while doing some playtesting and I noticed another problem. Have a look:
As you can see, there’s a very pronounced stutter or jank every few frames. This not only looks terrible, but makes it very difficult to play so it definitely needs to be fixed. In this case, it was bothering me so much that I decided to fix it immediately before moving on to the bug. I will walk through the steps that I took to figure out what the problem is and what the solution is.
Games are typically complex software systems with lots of inter-related moving parts so it’s a bit daunting to try to figure out what’s going wrong at first. The best way to start approaching a big problem is to break it down into smaller problems and then repeat the process until you can solve the whole thing. In this case, we first need to work out what a jank even is. Jank is not an overall bad framerate, instead it is a single frame that took much longer than the ones around it. This means that on one out of every several frames, something is taking much longer than it should.
Now that we know what we’re looking for, the question is how to find it. Because this problem doesn’t happen all the time, we have to be on the lookout for some kind of exceptional case. Perhaps the simplest way to try to track down what’s going on is to just systematically look at all the code in the game and determine which part is bad. Blightmare right now has about 24,000 lines of code across 300 source files. Reading and understanding all of that is a really big task and seems highly inefficient. What other options are there?
What we would like to be able to do is to somehow “see” what the code is actually doing at runtime and how long each piece is taking. If we could do that, then we could run the game until there was a jank and then go look at a comparison between a good frame and a janky frame to get at least some indication of where to start. How would we be able to measure the runtime of the code like that? One option is to work through the code bit by bit and add timers that automatically started and stopped when a function was entered and exited and then collect all this data somewhere for later processing. We could tag each sample with a frame index to allow the comparison that we are trying to do, and if we sum up all the functions in the frame, it should be pretty easy to see which one is the janky one.
If it sounds like a lot of work to manually add timers to all your functions, that’s because it is. And in addition to the time to write all the timer code, the timers themselves would actually slow down the code and could affect the results in ways that might give false results if you’re not super careful about how the timers work. Fortunately, this problem is not something that is unique to Blightmare, and there are tools out there that can automatically collect the information that we want without any manual instrumentation. The name for such a tool is a profiler.
Unity has a profiler built-in to the editor which shows function timings along side graphics timings and a whole bunch of other statistics that are important for optimizing different parts of a game. For the purpose of this bug, we only need the cpu profiler. To run it, I just opened up the profiler window, hit record, and did the thing that I observed as being slow in the game. Returning to the profiler – after stopping collection – shows what is called a flame chart of where the cpu is spending time in a hierarchical way.
Here’s a quick video of a profiler session:
As you can see in the video, the thing taking up most of our frame time (41.69 ms out of 60.38 ms) is something called GC.Collect. GC here refers to Garbage Collector and it is invoking the Collect method. We’re finally back to garbage! I hope it was worth the trip. Anyway, next week I’ll explain a bit about what Garbage Collection is and how I fixed the jank that it was causing in Blightmare.
If you are enjoying the content on this blog, please follow us on Twitter to make sure you get the latest updates and wishlist Blightmare on Steam to get notified when it’s released! Thanks for reading and stay safe out there!