Lately i’ve been trying to make more structured code but i’ve a problem with searching trough all of a List<> here’s my code:
public Unit(int maxHealth, AiData.jobs job) //Unit constructor
{
this.maxHealth = maxHealth;
this.health = maxHealth;
this.Job = job;
MyHouse = GetHouse();
}
bool tried = true;
public House GetHouse() //Search method of "Getting" a house
{
foreach(House house in GameManager.Houses)
{
if(house.Inhabitants != null)
{
if(house.Inhabitants.Count < house.MaxInhabitants)
{
house.Inhabitants.Add(this);
tried = true;
return house;
}
else
{
if(tried)
{
tried = false;
GetHouse();
}
else
return null;
}
}
else
{
house.Inhabitants.Add(this);
tried = true;
return house;
}
}
return null;
}
Basicly what i’m trying to do, is when the unit is created via the constructer. Find the unit a house (and give the house a inhabitant). I’ve checked that the GameManager.Houses is correctly filled. The whole thing works about 4 times, and then it just fails.
Maybe its a small error i made or the code is just completely wrong? All input will be appreciated!
Brum.