[Android Mini-Game] Form the word

This is my first mini-game for android. The objective is to form the word by touching the letters within time. Touch an invalid letter, or let the timer expire and the level fails.

As the player progress on the levels the word count will increase and the letters will disappear faster. Letter count starts from 4(very easy) to 14(hard)

Play store link: https://play.google.com/store/apps/details?id=fousteris.formtheword

Advertisements

2 tips for avoiding garbage collector in (android) gameloops

Currently implementing by first game in android i was excited to see the challenges a game (unlike any other software) introduces. One of them is the fact that because you have a loop running continiously you do not want to continiously request new memory. The reason is that smartphones have tight memory constraints and allocating objects means the garbage collector will be interrupting your game in order to reclaim unreferenced objects. However the operation of the garbage collector is not free. In case of a garbage collector interrupt your game will experience lags. In this article i present two ways i used in my game to avoid the garbage collector overhead.

1) Use an object pool
Every game needs to represent objects. So in the game loop you discard and create new objects all the time. For exaple if you have an enemy which dies or gets out of the game viewport, bviously this object doesn’t needed any more, but another enemy will appear very soon. If we create and discard objects in that fashion the overhead will be very high and the garbage collector will interrupt very often to reclaim the unusded objects.

So instead of allocating and discarding objects we maintain a list of instanciated objects ready to be used. If we need an object we remove it from the list we use it and when we are done with it we put it back to the list. That way we are not creating or destroying new objects.

ArrayList<Enemy> enemyPool = new ArrayList<Enemy>(); //This is our pool
ArrayList<Enemy> enemies = new ArrayList<Enemy(); //This is the list of enemies currently displayed

//Preallocate some enemies...
for(int i = 1; i <= 20; i++) {
   enemyPool.add(new Enemy());
}

//Game loop
while(true) {
     Enemy newEnemy = enemyPool.getEnemy();
     if (newEnemy != null) {
         enemies.add(newEnemy);
     }
     updateEnemies(); //Update the enemies list based on game logic.
     draw(); //Draw the frame
}

We are preallocating a pool of 20 enemies. The number depends on the game and the number of objects you will need.

2) Use char arrays / StirngBuilder instead of Strings
One of the traps in games are Strings. If you want to manipulate Strings in the game loop the String class is not a good idea. Strings are immutable and thus you need to create a new object when you want to change a character inside or concatenate it with another. You should use preallocated character arrays when aplicable instead of strings.

One example is the countdown timer in games. In my game i have one and i first implemented it with the naive approach. However the number of object allocations was pretty high, so i switched to a char array implementation and manually added the digits at the end.

This is the naive implementation

String timeDisplay = "TIME LEFT: " + String.valueOf(timer);

Howevever we are creating two new Strings per iteration. Not that good. Instead we should preallocate(out of the main loop of course) an array timeDisplay and manually change the digits at the end.

char[] timeDisplay = "TIME LEFT: ##".toCharArray();
char[] digits = { '0','1','2','3','4','5','6','7','8','9'};

Inside the game loop we extract the digits from the timer and set them at the array.

timeDisplay [timeDisplay.length - 2] = digits[(int)Math.floor(timer % 100 / 10)];
timeDisplay [timeDisplay.length - 1] = digits[(int)Math.floor(timer % 10)];

This code is less flexible than the naive method because you need to set the array length based on the timer number of digits. In my case the timer will always be a two digit number so it is ok. But it is fairly easy to make it support more digits. The big Plus here is that no more objects are allocated.

General rules
The object pool pattern is definitely your best friend when you are creating a game. You should preallocate every object you will need in game. Also always prefer StringBuilder/char arrays instead of strings. Immutability here is an enemy so we prefer using StringBuilder/char array. If you need to concatenate many strings then you should definitely need to use StringBuilder. If you have constant Strings which need to be mutated the char array is the best choice.