0
\$\begingroup\$

Im following a C# rpg game development tutorial atm and what confuses me is that I cant figure out when to use what.

The author writes different classes where 1 class derives from another so the derived class has all the properties of the bass class. With other classes when all the properties are needed from another class he just makes them a datatype

Example: both the monster and player class have currentHealth and and maximumHealth as properties from the LivingCreature baseclass. But then the author writes Item class as a public data type property in the InventoryItem class. Can someone explain to me when I should make a datatype and when I should I derive a class?

public class Monster : LivingCreature {
 public int ID { get; set; }
    public string Name { get; set; }
    public int MaximumDamage { get; set; }
    public int RewardExperiencePoints { get; set; }
    public int RewardGold { get; set; }
    public List<LootItem> LootTable { get; set; }

    public Monster(int id, string name, int maximumDamage, int rewardExperiencePoints, int rewardGold, int currentHitPoints, int maximumHitPoints)
        :base  (currentHitPoints, maximumHitPoints) {

        ID = id;
        Name = name;
        MaximumDamage = maximumDamage;
        RewardExperiencePoints = rewardExperiencePoints;
        RewardGold = rewardGold;
        LootTable = new List<LootItem>();
}


}
public class InventoryItem
{
 public Item Details { get; set; }
 public int Quantity { get; set; }

    public InventoryItem(Item details,int quantity)
    {
        Details = details;
        Quantity = quantity;
    }
}
\$\endgroup\$
  • \$\begingroup\$ Would help if you posted some code. I'm assuming you're asking about inheritance vs composition? \$\endgroup\$ – mmking Mar 14 at 2:30
1
\$\begingroup\$

Your terminology is a little unusual. A "data type" is a name for a kind of value, just like a class is a name for a kind of value. They're the same thing. It sounds like what you're calling a "datatype" is actually an instance of a value.

The difference between when you'd use the two is the difference between an "is a" relationship and a "has a" relationship (also known as inheritance versus composition).

You derive from a class, generally, when you want to express functionality with an "is a" relationship: "A monster is-a kind of living creature."

You create instances of types as properties, generally, when you want to express functionality with a "has a" relationship: "A living creature has-a current health value."

When the "is a" or "has a" phrasing makes sense, use that. When they both could make sense, that's more of a judgement call. The typical suggestion today is to prefer composition ("has a") over inheritance ("is a"). This was not always the case, though. In recent history it was far more prevalent to see a lot of "is a" relationships used to express functionality.

When you want to use which is, thus, to some extent up to you, and I would encourage you to experiment with both to discover the pros and cons for yourself (as they are many and varied, depending on your use case).

In your inventory example, InventoryItem sounds like it represents an item within a player's inventory, and so it has an Item property referring to what general kind of item is in the inventory, and maybe a "stack count" property to indicate how many there are.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.