What's new
  • Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • Visit Resources
  • Visit Downloads
  • Visit Portal

[Snippet C#] Singleton - or - how to save session variables

jim87

New Member
Joined
Aug 26, 2011
Messages
445
Reaction score
7
Have you ever wanted to save variables out of your methods, like stopwatches, and retrieve them whenever you want, for example to know how much time passed since the bot started?

Singleton is the thing for you then!

Actually a singleton is a class which is designed to be created only once, while usually you may have several instances of the same class (think about the classes you use in your behaviors, all the times you call them, a new instance is being created).

Creating a singleton is very easy, as much as calling it back into your other classes and methods.

PHP:
namespace Jim87
{
    class SharedData
    {
        private static SharedData instance;

        public static SharedData Instance
        {
            get
            {
                if (instance == null)
                    instance = new SharedData();
                return instance;
            }
        }

        public SharedData()
        {
            // initialize your variables here
        }

        public WoWPoint myPoint { get; set; }
        public WoWObject myObject { get; set; }
        public ulong myGuid { get; set; }
        // ...
    }
}

To call the singleton, all the things you have to do are:
PHP:
SharedData data = SharedData.Instance;

That's all! Then you can access your variables inside data, i.e. data.myPoint.

Actually you can Singleton-ize your bot or your plugin, even if I'd discourage this use as I tend to separate the variables from the bot itself, but it's just up to you!

I hope you find this usefull :)
 
Last edited:
ok so say i did want to save out "Total Combat Time" from a CC, why wouldn't i just use the normal settings wrapper save it as a String, and just re-parse it back in when i wanna load it? what advantage does this provide over the normal settings wrapper?
 
He's trying to show what a singleton-pattern is. (However, in the C#/.NET world, that's exactly what "static" is.) Singleton's are more often used for things where you may be running multiple "versions" of the class from a single process. (Say for example, you have an application managing 5 different databases. Each manager would create a singleton connection to the database, so you're not running around with 500+ connections)

In terms of HB though, "static" usually suffices for 99% of things, unless you're doing some more advanced things with app domains, etc. (Even then; static classes don't cross the domain border, so its still safe to use as a true singleton)
 
yea, i still dont get it, but ill just pretend i do.
 
ok so say i did want to save out "Total Combat Time" from a CC, why wouldn't i just use the normal settings wrapper save it as a String, and just re-parse it back in when i wanna load it? what advantage does this provide over the normal settings wrapper?

Because if you save it as a "setting" your spirit won't be pure :P - seriously, if you want to use settings is up to you, but they're not designed for that purpose. A singleton doesn't save data on the hard disk, but only for a session. Another example would be for behaviors that need to calculate a WoWPoint: normally you have to calculate it all the pulses, using a singleton you need to calculate it only once (or again, using settings and saving XYZ coords).
 
I like the idea...
Could help with figuring average damage of a spell or a heal.
 
Again, static does the same thing.

Code:
public static bool MyBoolean { get; set; }

"MyBoolean" is only instantiated *once* for the entire lifetime of the application (excluding app-domain quirks). You simply access it via "MyStaticClass.MyBoolean" instead of from a field.
 
Back
Top