Reputation: 1143
public int LevelReward()
{
int oldlevel = Client.Player.Level;
if (Client.Player.Level == oldlevel + 1)
{
for (int i = 0; i < Client.Player.Inventory.Length; i++)
if (Client.Player.Inventory[i] == null)
{
Client.Player.Inventory[i] = Client.Player.Manager.GameData.Items[0x7016];
Client.Player.UpdateCount++;
Client.Player.SaveToCharacter();
Client.Player.SendInfo("You've been given 10 gold for leveling up!");
}
}
return 0;
}
What I want is for this code to, whenever a player levels up, to give them the item "10 Gold" (that is what the 0x7016 is) But look at the top of the code, I am having trouble how to make it check/realize when a player has leveled up.. the code below is just the rest of it, spawning the item in inventory spot, etc., but I am only needing help on the top part
int oldlevel = Client.Player.Level;
if (Client.Player.Level == oldlevel + 1)
Upvotes: 1
Views: 91
Reputation: 3328
I would use INotifyPropertyChanged on my Player class. Then you can be notified when different properties change and put the event logic to what is supposed to happen when event x happens.
Here is an example. You can copy and paste this into the app LinqPad and play with it before applying concept to your project.
Example code of property change notification:
public static void Main()
{
var player = new Player { Level = 1 };
player.PropertyChanged += (sender, pcEventArgs) =>
{
var self = (Player)sender;
self.Gold += 10;
Console.WriteLine($"Player has leveled up to level {self.Level} and given 10 gold, gold is now at {self.Gold}.");
};
for(var i = 0; i < 10; i++)
{
player.Level++;
}
}
public class Player : INotifyPropertyChanged
{
public int Gold { get; set; }
private int level;
public int Level
{
get
{
return level;
}
set
{
level = value;
OnPropertyChanged(nameof(Level));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Result:
Each time the level changes the event is triggered.
Console output would be:
Player has leveled up to level 2 and given 10 gold, gold is now at 10.
Player has leveled up to level 3 and given 10 gold, gold is now at 20.
Player has leveled up to level 4 and given 10 gold, gold is now at 30.
Player has leveled up to level 5 and given 10 gold, gold is now at 40.
Player has leveled up to level 6 and given 10 gold, gold is now at 50.
Player has leveled up to level 7 and given 10 gold, gold is now at 60.
Player has leveled up to level 8 and given 10 gold, gold is now at 70.
Player has leveled up to level 9 and given 10 gold, gold is now at 80.
Player has leveled up to level 10 and given 10 gold, gold is now at 90.
Player has leveled up to level 11 and given 10 gold, gold is now at 100.
In your case after adding INotifyPropertyChange to your Player class you will put the changed event on player somewhere in your code, right after you create the player the first time..
Client.Player.PropertyChanged += (sender, pcEventArgs) =>
{
var player= (Player)sender;
if(pcEventArgs.PropertyName == nameof(player.Level))
{
for (int i = 0; i < player.Inventory.Length; i++)
{
if (player.Inventory[i] == null)
{
player.Inventory[i] = player.Manager.GameData.Items[0x7016];
player.UpdateCount++;
player.SaveToCharacter();
player.SendInfo("You've been given 10 gold for leveling up!");
}
}
}
}
Now anytime you have a Client.Player.Level++ or changed in that value it will trigger you logic.
NOTE: Although your loop looks like it will probably give 10 gold per item they have in the inventory. Not sure if that is what you want BUT the concept you asked for of knowing when their level changed is answered.
Upvotes: 2
Reputation: 16199
The best approach is to launch the give 10 gold on an event of when it actually happens. When do you update the players level count? At that point give the gold.
Upvotes: 4