Conor wrote:This would be a lot easier to code in source as you can add a Player.bool.
Actually, you can add bool or any other type of variable or class to the Player object. I haven't mentioned it anywhere, but there is a special way to do it. Player object contains ExtraData object, which is of type Dictionary<string, object>. It was created to let you add to the Player as many variables as you need. In order to do it you have to:
1. Think up some key of type string. And, it would be good to save it as a constant value.
2. Add this key with the respective object of any type to the ExtraData.
3. Retrieve and edit the value later as needed.
Here's an example:
- Code: Select all
public const string KeySomeInt = "SomeInt";
public void IncreaseWarningsAndKick(Player p)
{
if (!p.ExtraData.ContainsKey(KeySomeInt))
{
// Saves int value of 1 to ExtraData under "SomeInt" key.
p.ExtraData.Add(KeySomeInt, 1);
}
else
{
// Casts the value that is stored under "SomeInt" key to int
// and increases it by one and stores in newWarnCount variable.
int newWarnCount = (int) p.ExtraData[KeySomeInt] + 1;
if (newWarnCount > 3)
{
p.Kick("&cKick: Too many warnings.");
return;
}
p.ExtraData[KeySomeInt] = newWarnCount;
}
}
I use this method more and more often. In many cases it seems to be a better and cleaner solution than adding another variables to the Player class.
Anyway, the example you published is very clever. It does the job without using Player's variables. In programming there are many different ways to achieve exactly the same result and it's one of the things that make programming very interesting.