Dev Log #4: Fixing A Nasty Bug

It’s been a little bit since the last update. I have had some life events going on, such as moving back onto campus for my final semester of classes. I also created a really nasty bug in my game, that was very simple, but left me frustrated for a good bit as I could not figure it out. This proved to be a good learning moment for me when it comes to coding in C++ and more importantly, debugging. 

So, I started building out the classes for a Player Character Class and a Non-Player Character Class. Both inherit from the Base Character Class I built earlier on(Dev Log #2: Making a Cube Move and Dev Log #3: Adding Health and Death). I started out by fleshing out the Player Character Class, adding functions and variables that I will use to build out the Player’s experience. 

All is going well, I compile the code. Change some things, compile again. No issues as I explore how I want to make the Player Character work. After doing this for a bit, I compile, and the compile fails while the Unreal Header Tool is doing its thing. It spits out an error code and never reaches the point of compiling my code. Weird. 

I start to try a couple things. I try rebuilding all the code. That fails. I try googling the error code. I don’t find a clear answer no matter how hard I look. I go back and try compiling the code again. Still is failing. Also, now I am starting to get Intellisense Errors (at this point I am using Visual Studio Code) and now none of my header files seem to be found. I try tracing this error back to its source. Still nothing and now I am getting into unfamiliar territory as I go into the guts of the Unreal Engine. This is not looking good and there must be a simpler, cleaner solution. 

In my frustration, I do what seemed the most reasonable, and go for rebuilding all the code from the ground up in a new project, in XCode instead of VS Code. I copy each file of code I wrote from one project to the other, starting at the highest level in my inheritance diagram, working down. Each time I copy a class, I compile the code, seeing if it fails. I do this until I get to the Player Character Class. After copying the code for this class, the compile fails. 

AHA! I found the culprit class. XCode pops out a message saying the Unreal Header Tool has crashed. Ok, so now I know that something is causing the Header Tool to fail. So now I start commenting out code and compiling the code, until it starts working. I do this all the way to the point that I have commented out all the code in the Player Character Class. 

Yay! I am getting closer to finding the bug! Now I start to uncomment the code, line by line, till the compile fails again. I now quickly find the error. Turns out, I had declared a variable in the Player Character Class that it had also inherited from the Base Character Class. I quickly squash the bug by commenting out this bit of code as it was not necessary. I am able to get back to coding the rest of the class. 

So, what did I learn from this experience? First, I learned you cannot redeclare an inherited variable. The compiler will not like that. The second thing I learned was in hindsight from this experience. I probably did not need to build an entirely new project to find this bug. It seemed the way to go in the moment, but it was unnecessary, and probably costly if this was a larger project. The better option would have been to jump straight to commenting code till it started working again. I would have located the bug quicker and not dealt with the same frustration. 

For me, this was a humbling moment as I still have a lot to learn. I learned quite a bit from this, and I am probably going to learn a lot more as this project continues. 

Next post I hope to be talking about the game design and how the player character will work with some screenshots. Until next time!