Sir Maxwell Sir Maxwell - 5 months ago 13
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:

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?


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"});

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