How to hand over variables c#

Asked 2 years ago, Updated 2 years ago, 42 views

Additional information

void wake(){
    monstar=Getcomponent<monstarstatus>();//External
    monstar2 = Getcomponent<monstar2status>();// From outside
    int monstarability=monstar.attack;
    int monstar2ability = monstar2.attack;

    int monstarHP=monstar.HP();
    int monstar2HP=monstar2.HP();

If the conditions monstarHP and monstar2HP are >0:
If monstar acts first,

int monstar2LEFTHP=(monstar2HP-=monstarability);
int monstarLEFTHP=(monstarHP-=monstar2ability);

Perform the above operation permanently.Also, if either LEFTHP reaches <0, it ends immediately

Is this using if and for? Or I'll look for another way.

int monsterLEFTHP=monstarHP-damage;// Monster LEFTHP= Monster HP100-damage20 

If you want to do this repeatedly,
It's 80 instead of LEFTHP100, so
Next, I want to subtract -20 from 80.

c#

2022-09-30 14:24

1 Answers

I think it's roughly as follows.

using System;

public class Monster {
    private inthp = 100;

    public int HP() {
        return hp;
    }
}
US>class Sample {
    static void Main() {
        Monster monster = new Monster();
        inthp=monster.HP(); // Remove monster's current HP
        int damage = 20;
        while(hp>0){
            Console.WriteLine("The monster's current HP is {0}",hp);
            hp-=damage;//Damage Calculation
            Console.WriteLine("Damaged HP has reached {0}.",hp);
        }
    }
}


since the value is taken out by the method (monster.HP()) I think it's running on an external method.
HP is an attribute value of monster and must be rewritten.
For example, add a method to set attribute values as follows:

using System;

public class Monster {
    private inthp = 100;

    public int HP() {
        return hp;
    }
    public void HP(int value) {
        hp = value;
    }
}
US>class Sample {
    static void Main() {
        Monster monster = new Monster();
        inthp;
        int damage = 20;
        do{
            hp=monster.HP(); // Read
            Console.WriteLine("The monster's current HP is {0}",hp);
            hp-=damage;
            monster.HP(hp); // Export
            Console.WriteLine("Damaged HP has reached {0}.",hp);
        } while(hp>0);
    }
}

This allows you to take out the monster value, calculate the damage, and write it back.

In C#, there is a syntax as a property when you don't want these attribute values to be private and you don't want them to handle them directly (so you exchange them via methods).
If you use properties to write the same way, you will see the following:

using System;

public class Monster {
    private inthp = 100;

    public int HP {
        get{
            return hp;
        }
        set {
            if(value<0){
                hp = 0;
            } else{
                hp = value;
            }
        }
    }
}
US>class Sample {
    static void Main() {
        Monster monster = new Monster();
        inthp;
        int damage = 20;
        do{
            hp = monster.HP;
            Console.WriteLine("The monster's current HP is {0}",hp);
            hp-=damage;
            monster.HP = hp;
            Console.WriteLine("Damaged HP has reached {0}.",hp);
        } while(hp>0);
    }
}

It seems to be dealing with attribute values directly from the outside, so you can simply write as follows:

Monster monster=new Monster();
int damage = 20;
do{
    Console.WriteLine("The monster's current HP is {0}", monster.HP);
    monster.HP-=damage;
    Console.WriteLine("Damaged HP to {0}", monster.HP);
} while(monster.HP>0);

Personally, rather than doing this outside,
monster.damage(damage);
I think it's better to call like this


2022-09-30 14:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.