Game Registry
This Article is for beginners to illustrate one effective solution to in getting your game objects to communicate in almost any part of your game.
This article is the second part in the article to learn to build a Space Shooter game.
Lets think about the structure of a typical game. Here is a rough map of possible objects in a Space Shooter game;

Can you imagine the types of interactions between objects? For example;
- How would the player know if it is hit by one of the enemy bullets (there could be hundreds)?
- How would an enemy know if its been hit by one of the player bullets?
- How do we set an explosion effect when an Enemy or Player gets destroyed?
The solution this article is suggesting is a Registry class, similar to the Singleton Pattern. The main thing that makes this Registry pattern possible are static variables. A static variable is different from a normal variable as it belongs to the class it is declared in, rather than each individual instance of that class.
For example if you can imagine a class for an enemy you will have some kind of variable containing the value of how fast it is moving. You may want many enemy ships created from your 'EnemyShip' class. So if the speed variable was a static variable, when you change the speed for one enemy ship, all the enemy ships would have their speed changed too.
Here is an example of a Registry Class for the suggested game.
package;
import org.flixel.FlxG;
import org.flixel.FlxSprite;
import org.flixel.plugin.photonstorm.FlxSpecialFX;
import org.flixel.plugin.photonstorm.fx.StarfieldFX;
class Registry
{
public static var player:Player;
public static var bullets:BulletManager;
public static var enemyBullets:EnemyBulletManager;
public static var enemies:EnemyManager;
public static var effects:Fx;
public static var hud:HUD;
public static var stars:FlxSprite;
public static var godmode:Bool;
public static var background:ScrollingBackground;
}
Using a Registry is straighforward and can be used almost anywhere in your game. For example if you are writing the code to show an explosion through your FX class you could do this;
Registry.fx.explodeBlock(x,y);
There is no need to create a new var of your Registry since it has static variables. All you have to do similar to what I suggested in with speed is create one instance of the variables in your Registry. You would do this before you call the above code;
Registry.fx = new fx();
As you can imagine this design pattern is useful for almost any type of game. The main danger in using a Registry is that you can overriding your static vars when you dont mean to. You have to mindful of your usage and placement of how you interact with it, escpecially how you create new instances of variables like we did with 'FX'. An 'init' and 'destroy' function is useful so keep things organised.
Here is the Registry.hx class to put in your source folder;
package ;
import org.flixel.FlxSprite;
import org.flixel.plugin.photonstorm.FlxSpecialFX;
import org.flixel.FlxG;
import org.flixel.plugin.photonstorm.fx.StarfieldFX;
class Registry
{
public static var stars:FlxSprite;
public static var player:Player;
public static var bullets:BulletManager;
//Setup the Registry Objects to create your instances
public static function init()
{
//load the special effect for a fancy stars bg
if (FlxG.getPlugin(FlxSpecialFX) == null)
{
FlxG.addPlugin(new FlxSpecialFX());
}
var starfield = FlxSpecialFX.starfield();
stars = starfield.create(0, 0, FlxG.width,FlxG.height, 300);
starfield.setStarSpeed ( 0, 1 );
//create the player ship
player = new Player();
//create the Bullet manager with a default of 40 bullets
bullets = new BulletManager();
//if you want more bullets available for the quadshot just insert your desired amount
//see the Object Pool article
//bullets = new BulletManager(100);
}
}
Since we have a Registry setup I am sure you want to see you shiny new ship shoot bullets already!
The next article will explain one of the best ways to make this happen in the Bullet Manger.