The Importance of Code Cleanup.

While working on my most recent game project, I decided that I was going to make my own enemy AI detection code that would work exactly how I wanted it to. Without a concrete plan or any sort of preparation, I dived right in. I am aware that this isn’t a good way to make code that works but I tried anyway and after I finished it; the code was a sloppy, mix-mash mess of band aid fixes, convoluted commentary and a lot of ‘legacy code’ that was commented out, only existing to remind me of past mistakes when writing similar code. But the code worked right? That’s all that mattered, until it came to having to develop another enemy with similar detection code.

Looking back at the code that powers my most basic NPC, I realised that there was no way I could apply it to my newer NPC without writing lots of repetitive code that functioned a little bit differently. So I decided that it was time for a bit of Code Cleanup, and I went into the creation of my new NPC with a much more concrete plan lined up.

Here is my original NPC, obj_enemy; PATROL2

The robotic design of the character is not only to fit into the gameworld, but to also cover for its very ‘robotic’ and rudimentary coding. For example, lets look at the detection code for this guy;

Looks relatively simple I guess? Definitely messy but not the worst code I’ve written, but looking inside those Regions you find;

A Real Mess.

So my next main NPC, obj_army BREATHER will have a lot more going on in terms of what it will be doing outside of detection. I decided I’d start pushing a lot of the detection code that was repeated in each of these different regions into a new cleaner script that would shorten the code dramatically and improve readability.

So here it is;

As you can see, the code is shortened drastically and is all self contained in one script, so now instead of having all the detection code in the object itself, it will be passed through to this script, that I can now use for all future NPCs going onwards. Looking at obj_army’s code now is like a breath of fresh air compared to obj_enemy (unless you dislike how I indent my code, which would be no surprise);

All of that repitition shortened into this code (missing a lot of bells and whistles, of course but the code will be more concise either way). To me this has demonstrated how important it is to code concisely and have a plan going in. My shabby code looks 10 times better after I’ve really thought over what I want to do and ensuring that I’m not coding in a manner that is counterproductive!

As for the game prototype, its coming along well. Art has never been my strong suit so I’ve barely touched the sprites, but the games overall structure is coming along nicely. I’m trying to ensure that the basic mechanics are completed first before I start building the game world as I don’t want to go back and add new mechanics in later on unless absolutely necessary.

I expect that a basic prototype will not be far off, but until then!

-Casp

Leave a comment