Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

I've decided to try something new in the "architecture" of this game. In the past I've always used a lot of static fields to globally access some unique things. Everyone who knows the pitfalls of Android will understand that it can lead to problems and is overall not a really clean way to do that.

A more clean way of getting the "dependencies" I need, anywhere I want them, is via Dagger 2. This still uses statics to store the "Access Points" to the dependency producers, but in a more advanced way via Scopes (for example "Singleton"- or "Game"-Scope). Dagger 2 also does not make use of reflection, but generates code at compile-time, so it is really fast.

Getting the globally unique World object for the current game (a new game starts every time you die), looks like this:

DI.getGameComponent().physicsSubSystem().world

The same is used for SpriteBatch, the Viewport, AssetManager, UI Stage etcetc. In total a really clean, powerful and comfortable way to access what you need, from anywhere you want. I can definetely advice other devs to try it.