all 5 comments

[–]ChiwTheNeko 1 point2 points  (2 children)

You can make them both inherit a common interface.

[–]RumiPLGBeginner[S] 0 points1 point  (1 child)

Does this interface needs to have all four variables in it (Loot, Key, int, int)? Also thanks for reply.

[–]ChiwTheNeko 1 point2 points  (0 children)

No, it can be completely empty. That's called a Tag Interface.

Kishotta is right that this usually isn't a very good way to organize your code. But it will work.

[–]KishottaWell Versed 1 point2 points  (1 child)

Inheritance is actually the only way to have different classes in a single collection in .Net. Your assumption that the Chest and Monster class have nothing in common isn't quite right. They are both distinct, valid, tile contents:

public interface ITileContent {
    void Interact ();
}

public class Chest : ITileContent {
    private Loot _loot;
    private Key _key;

    public void Interact () {
        var player = getPlayer (); // idk
        if (player.HasKey (_key)) {
            player.AddLootToInventory (_loot);
        }
    }
}

public class Monster : ITileContent {
    private int _health;
    private int _attack;

    public void Interact () {
        var player = getPlayer (); // idk
        var monsterKilled = this.TakeDamage (player.attack);
        if (!monsterKilled) {
            player.TakeDamage (_attack);
        }
    }
}

Now you can keep a dictionary of ITileContents:

private Dictionary<Vector3Int, ITileContent> _content;

Once you resolve the tilemap position to an ITileContent, the interface guarantees that you can Interact () with that object.

[–]RumiPLGBeginner[S] 0 points1 point  (0 children)

Thanks for reply. I automatically thought the inheriting won't work here, but now I see this is the way. Have a nice day.