Home > Software engineering >  What can I do if I need to reach a function in child class and I have its parent reference?
What can I do if I need to reach a function in child class and I have its parent reference?

Time:05-28

Sorry for the title, I could not imagine a more specific title but let me explain.

Let's say we have 2 different characters. One is Ally which is our character and the other one is Enemy. And let's say we are immortal so Ally cannot die.

public class Ally : BaseCharacter
    {
        public override void Attack() { }
    }

    public class Enemy : BaseCharacter
    {
        public void Die() { }
        public override void Attack() { }
    }

    public class BaseCharacter
    {
        public BaseCharacterData baseCharacterData;

        public virtual void Attack() { }
    } 

Here, Attack() function is common between these child classes. But Die() function is not.

And we have data classes for these classes.

    public class AllyData : BaseCharacterData
    {
    }

    public class EnemyData : BaseCharacterData
    {
        public string health;
    }

    public class BaseCharacterData
    {
        public string name;
    }

And also here, NAME variable is common and HEALTH is not (cuz Ally cannot die).

The problem is this, when I try to create BaseCharacter from another script I assign CharacterData to Ally and it's just fine.

But when I create Enemy, Enemy's health will not be set (I can set it from somewhere else somehow maybe but it's a problem when I need to reach it as well). Also, let's say we have another class called TEMP and one of its functions takes CharacterData. But it will not be able to reach HEALTH if that CharacterData belongs to an Enemy or it will not be able to call Die() function from BaseCharacter script of Enemy cuz its not common.

This whole scenario is not a real one and does not make sense but I try to simplify it and tell what my problem looks like.

My question is, Is there something I'm doing wrong or this scenario just need a better structure to achieve my goals?

I'm open to all suggestions or solutions but I'm not trying to solve my problem easily or solve it in bad ways. Just trying to find the right ways.

Thank you all in advance.

CodePudding user response:

Death is not something that is called upon the character. It is something that happens in response to something happening to the character. Things that can happen to a character:

  • drank a poison of potency P,
  • got hit with a rock of weight W, to body part B,
  • got hit with an arrow of weight W, speed V, with enchantment E, to body part B
  • fell down to an object O (poisoned spikes or plain old ground)
  • got hit with a magic spell named "just die already"

So these are your methods. Enemy can die from them, and ally can't, but even an ally can experience some other effects (dizziness, unconsciousness, etc.).

CodePudding user response:

You have defined a data class that is similar to the baseCharacter class into two types. Why not set the information within the class itself? Eliminate Alternatives and Dublications The first and most important programming lesson.

public class Ally : BaseCharacter
{
    public override void Attack() { }
}

public class Enemy : BaseCharacter
{
    public float health;
    public void Die() { }
    public override void Attack() { }
}

public class BaseCharacter
{
    public string name;
    public virtual void Attack() { }
} 

Access Class

public void Start()
{
    BaseCharacter character = new Enemy();

    if (character is Enemy myEnemy)
    {
        myEnemy.health = 52; // for e.g
    }
}
  • Related