0

I'm writing a simple game in unity and would like to construct a combat function

public CombatVsMonster (Character character, Monster monster)

this is what I have (takes a character and monster and executes a combat)

How can I make the same code work with (merging those two, so the second constructor can be either Monster or Character class)

public CombatVsMonster (Character character, Character character)

in other words: Can I have a constructor that would be able to take in different class objects? If yes: how?

9
  • Does Monster derive from Character? Commented Jan 4, 2017 at 16:00
  • No, but has similar characteristics. Would deriving help? Commented Jan 4, 2017 at 16:02
  • There are many possible approaches to what you're describing. When you make a complete attempt, is there a specific error or problem that you encounter? Commented Jan 4, 2017 at 16:02
  • @David my attempt was to use void, but that got an error messege Commented Jan 4, 2017 at 16:03
  • 1
    Yes. Use interfaces. Commented Jan 4, 2017 at 16:04

2 Answers 2

1

There are many ways to approach this, but a common thread shared by most games which include any sort of "Monster" is that the player, monsters, etc all have some things in common, such as HitPoints.

If that's true of yours as well, then using inheritance is probably the easiest way to accomplish what you want. "Character" should have things common to all Characters, be them NPCs or the player. From there, if Player and Monster both inherit from Character, then you can pass them both to any function that accepts Character as a parameter. This should save you a lot of code, since it would allow one function to handle interactions between Characters. They will both have all of the features of a Character, but will also have their own attributes and properties.

Sign up to request clarification or add additional context in comments.

2 Comments

This makes it clear to me: I'll make a new Class and derive both Character and Monster from it. Your solution was clear and through.Thank you very much.
Glad I could help! Don't forget to set your question as Answered!
1

You need to create an interface that will be inherited to its childs.

class Character : MonoBehavior, IActor
{
     public float Hp {get;set;}

     private float _damage;
     public float Damage
     {
          get {return _damage;}
          set {_damage = value;}
     }
     //implemetation of IActor
}

class Monster : MonoBehavior, IActor
{
    //implemetation of IActor
}

interface IActor
{
    float Hp {get;set;}
    float Damage {get;set;}
}

public Combat (IActor character, IActor character2)

1 Comment

I already figured it out thanks to user1895086, but your solution works too. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.