Fabians Game Programming Tips: Comments Are Not As Great As People Say
Fabians Game Programming Tips: Comments Are Not As Great As People Say
Fabians Game Programming Tips: Comments Are Not As Great As People Say
During the years I've learned to optimize for readability and having straight forward
code that is easy to debug.
Good:
const int HealthPickup = 5;
myHealth += HealthPickup;
Don't be afraid to use constants for one use only.
{//Hud Update
title = "Player health " + health.ToString();
}
}
In loops, I suggest you reverse the order of xyz, this will go through the array in the
same order you read a book.
for (int y = 0; y < Height; ++y)
{
for (int x = 0; x < Width; ++x)
{
doSomething(x, y);
}
}
Bad:
bool onHorse;
void update()
{
switch(state)
{
case GameState.InMenu:
updateMenu();
break;
case GameState.Play:
if (onHorse)
{
updateHorseRide();
}
else
{
updateGamer();
}
break;
case GameState.Death:
updateDeathCinematic();
break;
}
}
Good:
void update()
{
if (menu != null)
{
updateMenu();
}
else if (player.health > 0)
{
if (horse != null && horse.health > 0)
{ //Checking if the horse exist instead of trusting a boolean
updateHorseRide();
}
else
{
updateGamer();
}
}
else
{
updateDeathCinematic();
}
}
Connected values
Don't write code where the same change has to be done in two or more places.
Connect all values that rely on eachother, example:
int totalHeight = 10;
int topRow = 4;
int bottomRow = totalHeight - topRow;
Good:
if (alive)
{ DoOption2(); }
else
{ DoOption1(); }
Look at the example below, it is very straight forward but it is easy to miss the "not":
if (!player.alive)
{ DeathAnimation(); }
if (player.alive)
{ } //Left empty for now
else
{ DeathAnimation(); }
Complexity kills
Big projects often hits a brick wall when they get too complicated. For example, a
game i worked on for a long time, this is what I do to add a new monster:
You can not completely avoid this issue, but here is a few tips on how to make it a bit
easier to manage:
-Remove old code whenever possible, people keep things and think "it will be useful
later".
-Add one feature at a time, make everything bug free before you add more.
-Make debug tools that makes it easy to go anywhere in the game and get any
equipment. Make them available for people who are testing your game.
-Straight forward and 'dumb' solutions often lasts longer. Optimizations, multi
threading, complicated math formulas and recursive methods - will often give you
issues later down the line.
-When planning ahead, keep in mind that each new feature is going to take longer
and longer to add. Do not be fooled by how fast the initial progress was.