Singletons and XNA GSE Refresh



Refresh

A new version of XNA Game Studio Express was released, which require Visual C Express Service Pack 1. The refresh provides Vista support, XACT 3D audio, performance boosts, bitmap fonts, and a distribution packer. You no longer have to give up your source code.

At the same time, Creators.Xna.com received new content. This included the long awaited XNA Racer starter kit, but also many tutorials, samples, articles, and utilities. They cover glow effects, drawing points and lines, menu transitions, Doppler and pitch sound effects, particle effects, vertex lighting, data structures, pipeline programming, the SpaceWar structure, a font maker, and Xbox 360 controller graphics.

Singletons

Scott: “In the Farseer singleton, what’s Farseer(){} for?”

Ah, I missed that? Here we go:

There are no instance fields or properties, only the static field and static property.

There are two constructors, one static constructor, one instance constructor, both private. Private is the default.

The static constructor gets automatically called right before (see Lazy below) some code accesses the Physics property. When called it initializes the static physics field.

The instance constructor, the one you are asking about, gets called when someone tries to instantiate the class. The instance constructor doesn’t do anything, which is good, because there is nothing that should be done.

I probably missed mentioning it, because it doesn’t do anything.

Hold on. Come to think of it, it does do something. It stops people from instantiating the Farseer class, while forcing the creation of the static version of the class.

The C specification (ECMA 334) states in section 17.11:

“The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

* An instance of the class is created.

* Any of the static members of the class are referenced.”

In other words: When starting the game, the static Farseer is not created. If a piece of code tries to access the Physics property, then the static part of Farseer is created. Or, if someone tries to instantiate Farseer, it is not instantiated, but the static part is activated.

Did that make sense?

Lazy

A class which isn’t instantiated/initialized until it is really needed is called a lazy class. This version of a singleton isn’t 100% lazy, but almost. If someone adds a second static member, the physics field would be initialized when that member was accessed, even if it had nothing to do with physics.

If you for some reason want a fully lazy version, you can go to the same excellent source as I did, Jon Skeet, and read more about singletons in C.

Also from Jon Skeet: If you make a second singleton which calls the Farseer singleton in its constructor, and the Farseer singleton references the second one again, then you might make the whole thing crash. That’s not a common programming situation, though.

Tiny Singleton

On another note, you could shorten the whole class to this:

public sealed class Farseer

{

    public static readonly PhysicsSimulator physics =

        new PhysicsSimulator(new Vector2(0f, 300f));

    private static Farseer() {}

    Farseer() {}

}

That’s a public field, which is a bad idea, because you can’t add code to it when it gets accessed, as you can with a property. If you don’t need code, then this would look tiny and neat. It doesn’t affect performance to use a property, though. The JIT compiler is too smart for that.

Scott: “I’ve never seen such a good mix of theory and practice, and practically everything’s perfectly clear.”

Thank you!

Cheers!

  • XBLIG stands for Xbox Live Indie Games.

  • The binary file submitted to Xbox live must not be bigger than 150MB

  • The game developer can choose the price at 80, 240 or 400 Microsoft Points

  • Fortresscraft is the bestselling inde game for the Xbox live in 2011

  • Indie titles that are released on Xbox Live needs to be previously accepted by there peers

  • In 2009 the best selling indie title for the Xbox was I MAED A GAM3 W1TH Z0MBIES 1N IT!!!

© 2006-2011 XNAtutorial.com All Rights Reserved.