Home > Enterprise >  How can I reuse an object in C#?
How can I reuse an object in C#?

Time:12-05

I am making a simple .NET C# Console fighting game, and have a class called player whose constructor sets these values:

public int maxHP;
public int currentHP;
public int damage;
public int potionCount;

public Player(int maxHP, int damage, int potionCount){
    this.maxHP = maxHP;
    this.currentHP = maxHP;
    this.damage = damage;
    this.potionCount = potionCount;
}

The player object is created in a separate class called battle, with functions for starting a battle and rewarding the player after each successful battle shown as so:

class Battle{   
    Player player = new Player(20, 1, 2);

    public Battle(){
        //Creates a loop prompting Player actions, then the enemy attacks, if enemy is dead reward the player.
        Reward();
    }
    
    public void Reward(){
        //Asks the player if they want  10 max health,  1 damage or  2 health potions and changes the player object's specified value.
    }
}

The main problem is that when I call for a new battle to be created in the Program class' main loop, it resets the player values to what they are at the top of the Battle class, when I instead want the player to use the values from the previous battle. Is there any way I can do this with the same player object?

Here is the main loop:

static void Main(string[] args)
{
    Battle slime = new Battle(5, 1, "Slime");
    Battle zombie = new Battle(10, 3, "Zombie");
    Battle skeleton = new Battle(15, 5, "Skeleton");
    Battle guard = new Battle(20, 7, "Royal Guard");
    Battle king = new Battle(30, 10, "King of the Dungeon", true);
            
    WriteLine("Press any key to exit.");
    ReadKey();
}

CodePudding user response:

Will be easy to show you if you show your main loop what change youd need to make. But basically, instead of instantiating a new Player inside your battle class, either create the Player outside the Battle object and pass it in with the constructor or function on the battle. If a Player exists for multiple Battle's then it makes sense that the Player shouldn't be created inside the Battle right?

Edit With your update including main:

I'm a little confused because I see you originally said your Player object is created outside the Battle object but your examples show it being created inside the Battle object. So I hope this actually helps and isn't something you've already done.

static void Main(string[] args)
{
     Player player = new Player(20, 1, 2);

    Battle slime = new Battle(player,5, 1, "Slime");
    Battle zombie = new Battle(player,10, 3, "Zombie");
    Battle skeleton = new Battle(player,15, 5, "Skeleton");
    Battle guard = new Battle(player,20, 7, "Royal Guard");
    Battle king = new Battle(player,30, 10, "King of the Dungeon", true);
            
    WriteLine("Press any key to exit.");
    ReadKey();
}

class Battle{   
    private readonly Player1 _player ;

    public Battle(Player player, int maxHp, int damage, string name){
        //Creates a loop prompting Player actions, then the enemy attacks, if enemy is dead reward the player.
        _player = player;
        //set other variables
        Reward();
    }
    
    public void Reward(){
        //Asks the player if they want  10 max health,  1 damage or  2 health potions and changes the player object's specified value.
    }
}

CodePudding user response:

You just create a constructor of Battle with an existing player

class Battle{   
    Player player;

    public Battle(Player player, int hp,...)
    {
        this.player = player;
        //Creates a loop prompting Player actions, then the enemy attacks, if the enemy is dead reward the player.
        Reward();
    }
    public void Reward(){
        //Asks the player if they want  10 max health,  1 damage or  2 health potions and changes the player object's specified value.
    }
}

and in the main program use the same player object

static void Main(string[] args)
{
    Player player = new Player(20, 1, 2);
    Battle slime = new Battle(player, 5, 1, "Slime");
    Battle zombie = new Battle(player, 10, 3, "Zombie");
    Battle skeleton = new Battle(player, 15, 5, "Skeleton");
    Battle guard = new Battle(player, 20, 7, "Royal Guard");
    Battle king = new Battle(player, 30, 10, "King of the Dungeon", true);
            
    WriteLine("Press any key to exit.");
    ReadKey();
}

I would also recommend creating a class of the enemy types and just use new Battle(player,enemy); to start a new fight.

  • Related