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.
To call the singleton, all the things you have to do are:
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
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: