Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a code block to run when a variable changes?

Tags:

c#

I'm writing a code for a small game so naturally, I have a lot of variables that all do different things whenever they change. My question is, is it possible to tell my code to run certain lines of code each time the variables change, so as to avoid repeatedly typing in the same lines, over and over again?

Example:

    Hero1.Health = 10;
    Hero1.MaxHealth = 12;
    ProgressBarHeroHealth1.Max = Hero1.MaxHealth;
    ProgressBarHeroHealth1.Value = Hero1.Health;

If I change the hero's health, I want the Progress Bars value to automatically change with it, without having to write it down every time that it changes.

I don't know if it's at all possible, or if I'm just hoping for too much, so I'm coming to you for answers. Any info would be much appreciated.

like image 585
Andrew Avatar asked Apr 27 '26 06:04

Andrew


2 Answers

Beginner

You could write an extension method for the control, e.g.

static class ExtensionMethods
{
    static public void SetHealth(this ProgressBar control, Hero hero)
    {
        control.Max = hero.MaxHealth;
        control.Value = hero.Health;
    }
}

And call it like this:

ProgressBarHeroHealth1.SetHealth(Hero1);

Intermediate

Now extension methods are cool and all, but this is like the dumbest way to use them. If the control class isn't sealed, a better design is to write a proper method:

public class HealthBar : ProgressBar
{
    public void SetHealth(Hero hero)
    {
        this.Max = hero.MaxHealth;
        this.Value = hero.Health;
    }
}

And call it the same way:

HealthBarHeroHealth1.SetHealth(Hero1);

(In order for this to work, you have to use HealthBar instead of ProgressBar in whatever UI platform you're using).

Advanced

But you know what would be really cool? A progress bar that updates itself. Hmmm... maybe if it listened for some kind of event....

class HeroHealthBar : ProgressBar
{
    protected readonly Hero _hero;

    public HeroHealthBar(Hero hero) : base()
    {
        _hero = hero;
        hero.HealthChanged += this.Hero_HealthChanged;
    }

    public void Hero_HealthChanged(object sender, EventArgs e)
    {
        this.Max = _hero.MaxHealth;
        this.Value = _hero.Health;
    }
}

Of course you'd need a Hero class that raised the event....

class Hero
{
    public event EventHandler HealthChanged;

    public void SetHealth(int current, int max)
    {
        _health = current;
        _maxHealth = max;
       OnHealthChanged();
    }

    protected void OnHealthChanged()
    {
        if (HealthChanged != null) HealthChanged(this, EventArgs.Empty);
    }
}

Once all this setup is done, when you want to set the health, all you have to do is write

_hero.SetHealth(current,max);

...which will set it on the hero and automatically notify the progress bar to update itself as well.

like image 70
John Wu Avatar answered Apr 29 '26 18:04

John Wu


It is possible. Suppose You have a class called Hero and you want to change progress bar colour when healt is increase or decrease then

Class Hero
{

Public String Name{get; set;}
Public Double health;
Public Double Health
{
    get
    {
        return health;

    }
    set
    {
       //Here you can place a function which can easily check the value of health and work accordingly.

       ProgressBarColorChange(value+health);

       health = value;
    } 
 } 
 Public void ProgressBarColorChange(double health)
 {
   //Color Change Fuction Implementation
 }
}
like image 31
Tayeb Himel Avatar answered Apr 29 '26 18:04

Tayeb Himel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!