Sir Maxwell Sir Maxwell - 3 months ago 9
C# Question

Calling an array Item from another class

I got this bit of code here:

IEnemy[] Enemy = new IEnemy[2];
Enemy Goblin = new Enemy("Goblins", "Looks dirty and not so friendly", 100);
Enemy[0] = Goblin;


I can use it easily with this:
Goblin.EnemyEncounter();


First I like One-Liner when possible, so is it possible to make it smaller?

Second, I created my enemy in the Main, but I always try to keep my main small and tidy, so I wanna use another class where i create these enemies and I can call them from my Main or any other class... I was thinking about Singleton, I'm just not so sure if it's the right thing to use or a static array... Any pointers?

Answer

Sounds like you need to use a parent class that has a list of other entities.

Something like:

var world = new World();
world.Enemies.Add(new Enemy{type="Goblin"});
world.Heros.Add(new Hero{name="Bart Simpson"});
Etc...

World would need to have collections defined for each type, such as: Enemies, Heros, Vehicles, Treasure, etc...