My First Ludum Dare Part 3 - Trials and Tribulations

As I am sure many other developers did, we ran into more than our fair share of bugs along the way while developing Cacophony. Some of which turned out to be straight forward fixes that were right under my nose, others which were a bit more elusive.

Let’s take a look at a few of the notable ones.

Walls that aren’t walls

In my previous post I wrote about how the map generation works for our game.

Long story short: We fill a square with walls, remove walls in rectangles to make rooms, remove walls to make hallways between these rooms. This is from my understanding a tried and true methodology for procedural generation of dungeons in roguelike games.

This is where A LOT of my programming time during Ludum Dare was spent and this bug in particular cause me a large quantity of grief.

Here’s what went down:

In our game, we have side and corner tiles for the wall to make the dungeon rooms look more natural and appealing. This is a feature we wrote from scratch on top of the generation provided by more text-based tutorials.

image-7.png

This alone was just some simple math, if we know the coordinates of the 4 corners of the room its easy enough to figure out where these tiles should go.

So while this was working we’d run into issues where when a room was not a corner like seen above, but more like the layout shown below, tiles in the green boxes would still visually appear to be walls but would logically act as floor tiles.

image-8.png

Maybe some of you can already see where this bug is going, but I could not. I looked through the code and everywhere when we set the logical tile to a floor tile we updated the visual tile so what was going on?

I debugged this for hours upon hours on Saturday morning with no luck, it had gotten to the point Sunday night I was content shipping it this way and just saying there are some visual errors in the game.

Then, in a passing sentence one of my co-developers Scott said something along the lines of “It looks like it’s just placing the walls on top of the hallways” and as soon as I heard that everything in my brain clicked into place.

What was happening was a problem with the order in which operations were happening. The hallways were getting created, which updated our logical tileset but afterward the visual corners and edge walls were being added. When the visual edges and corners are added, we weren’t changing the logical tiles because in my mind they were already walls we were just updating the visual cues.

A bug that had my pulling my hair out and accepting defeat, solved in seconds by Scott’s passing thought.

Classic.

Prefabs updating out of the game

This is probably one of the weirdest bugs I have ever seen in Unity, and this will serve as an example of why you should listen when people say there is a proper way to do things.

When maps are generated we also spawn a couple of NPCs in the dungeon. Namely, two shopkeepers one for gear, and one for food. Naturally, when we spawn these NPCs because our entire map is randomly generated we will have to place them in their respective rooms.

This should be a very simple operation but it wasn’t working both NPCs were being spawned at 0,0 and never moving.

So, a few Debug.Log statements later and we can clearly see the function responsible for moving them being called, and we can even see that the debug log is printing the position where they are supposed to be as their current position. Cue the confusion.

What was actually happening was that all of this code was accessing the prefab file stored in the editor, and not the active game object we had just instantiated using that prefab. Weird right?

The issue turned out to be how we were accessing the prefab we wanted to spawn.

image-9.png

What had tipped me off that this may be a problem was actually a random message directed to somebody else in the Unity discord. What did that message say? “There is no reason to ever use Resources.Load” and from there, I knew that even if it didn’t solve my problem it couldn’t hurt to remove this line of code and access the prefab in a safer way.

And like magic this actually solved our problem. What originally had confused me more than almost any other bug I had seen previously was as accessing prefabs the same way I did in every other script.

Programming, am I right? I suppose a lack of sleep and mounting stress will make you write bad code.

Our biggest challenge

More so than any of the bugs we encountered and map generation in totality by far the biggest challenge we faced during Ludum Dare was attempting an implementation of A* path-finding.

This was a daunting task but one that would have to be overcome in order for us to have a functional roguelike game, there’s not much challenge in a bunch of enemies that stand still is there?

The last time I had thought about A* was in my last college semester Algorithms class and even then I had a very hard time wrapping my head around it.

So without much planning or research I dove right in at a naive implementation of my own making which failed miserably. After a few hours were wasted on this effort Scott did some research on A* and prepared a presentation to explain the algorithm to me.

The details are hazy on me still so please do not take this as an A* guide there are much better resources out there but my understanding is essentially A* checks the possible moves it could make from its current position, then rates them based on their distance from the objective. From here it consistently makes the best decision currently available to it then moving in that direction.

From here, Scott, Ryan our artist, and myself got in discord and began to pair program our way to success with the guidance of Scott’s research and presentation.

Ultimately, this was a process that took us until almost 5am on Saturday night (Sunday morning?) but what was important that we succeeded and the game would not die by a lack of path-finding.

screeny.PNG

Cacophony: The Ballad of the Celestial Goblin-King is a procedurally-generated roguelike dungeon crawler created in 72 hours for Ludum Dare.

The first update to Cacophony will be released May 12th.

You can try the game in your browser here.