0

I have a method that is supposed to check a player's HP and then perform some logic based on if the number is greater than 0, or less than or equal to 0.

The method works but if I type it in the code and then change the hp value of a player it won't do anything until I type in the method name again. Then it will display the correct information.

At first I thought of using some kind of loop instead of a method. If I am correct, that means I'd have to have put curly braces around all my code that needs to get checked (which means basically around the whole code and I don't want that). Same with IF statement - even if I put it at the beginning of the code I'd still have to put curly braces around all the code.

Then I thought of a method which I already mentioned. And as I said - it does what it's supposed to do but only if I paste it multiple times into the main code.

Now, my question - is there ANY way to make the method "repeat itself" constantly or, I don't know, start at some place in the code and remain active?

Here is the method:

static void Status()
{
    if (Player.playerHealth <= 0)
    {
        Player.isDead = true;
        Console.WriteLine("You are dead!");
    }
    else if(Player.playerHealth > 0)
    {
        Player.isDead = false;
        Console.WriteLine("You are not dead!");
    }
}

I would be really grateful for any help.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
TinyPaws
  • 3
  • 3

2 Answers2

1

You can define playerHealth as a property. That way, any time anyone changes it, you can make some code fire, including the check that you want.

class Player
{
    protected int _playerHealth = 0;

    public int PlayerHealth
    {
        set 
        {
            _playerHealth = value;
            if (_playerHealth == 0)
            {
                isDead = true;
                Console.WriteLine("You are dead!");
            }
        }
        get
        {
            return _playerHealth;
        }
    }

Now you don't even need to call Status... the logic will occur automatically whenever the player's health is modified.

var player = new Player();
player.PlayerHealth = 0;   //Automatically triggers message
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Thank you very much. I chose to go with your answer as it was easier to follow and also I may use a similiar thing later, for example for a situation when player's mana points reach 0. It was really helpful but also showed me I just scratched the surface. Can't really ask for anything else but... if you could post few line explaining your code that would be absolutely awesome. I understand the overall concept after that but I plan on not simply copy pasting from others without trying to actually understand it. Yet, it works great! Thank you! – TinyPaws Oct 11 '20 at 22:44
0

Correct me if I'm wrong but you want to call a method everytime the player's life changes? It looks like you should use an event.

public class HealthEventArgs : EventArgs
{
    public int Health { get; set; }

    public HealthEventArgs(int health)
        : base()
    {
        this.Health = health;
    }
}

public class Player
{
    public event EventHandler<HealthEventArgs> LifeChanged;

    int _Health;
    public int Health
    {
        get => _Health;
        set
        {
            _Health = value;
            LifeChanged?.Invoke(this, new HealthEventArgs(_Health));
        }
    }
}

Then in your code it would look something like that

Player player = new Player();
player.LifeChanged += (o, e) => 
{
    Player p = o as Player;
    int health = e.Health;

    // Whatever logic here, with access to the player and its health
};

With that mechanism in place, everytime the health of a player changes the event will fire and you will have access to the player and its health and can act accordingly.

If you're not confortable with lambda expressions, the code above can also be written as such

Player player = new Player();
player.LifeChanged += PlayerLifeChanged;

public void PlayerLifeChanged(object o, HealthEventArgs e) 
{
    Player p = o as Player;
    int health = e.Health;

    // Whatever logic here, with access to the player and its health
};
Arthur Rey
  • 2,990
  • 3
  • 19
  • 42
  • Thank you for helping! I decided to go with the other solution as it seemed slightly easier for a newbie like me but I appreciate your help and I'll look more at your code and try to understand it better. Those events sound really promising, I feel like they could really be of use in my console game, possibly later on. – TinyPaws Oct 11 '20 at 22:46