I'm having a problem. I'm making a utility to do procedural generated maps. I have a room pool and each rooms are disposed in a table of room. I have a method to connect all the room together which walk in the table and connect adjacent rooms.
I have an enum
which contains the type of rooms :
public enum RoomType
{
Default = 0,
Building,
Boss,
Item,
Standard,
Start,
}
In the connection method I check the neighborhood to see what kind of room it is :
if (neighbourhood[2, 1] != null)
{
if (firstLevel.isOn)
{
if (neighbourhood[2,1].TypeOfRoom == RoomType.Start)
{
roomGrid[x, y].AddConnection(neighbourhood[2, 1], Location.RIGHT)
}
}
else if (neighbourhood[2,1].TypeOfRoom != RoomType.Boss)
roomGrid[x, y].AddConnection(neighbourhood[2, 1], Location.RIGHT);
}
But when i check if the type of room is Start
, it's always true
and the connection is added.
I don't know why this happens.
where i set the TypeOfRoom : img3
The problem is most likely due to a race condition. You can easily check if this is the case as follows:
if (neighbourhood[2, 1] != null)
{
if (firstLevel.isOn)
{
var typeOfRoom = neighbourhood[2,1].TypeOfRoom; //store type in a local variable
if (typeOfRoom == RoomType.Start) //check against local copy
{
roomGrid[x, y].AddConnection(neighbourhood[2, 1], Location.RIGHT)
}
}
...
}
You will see now that the if
condition works perfectly fine but neighbourhood[2,1].TypeOfRoom
will not equal typeOfRoom
which means it is being modified in another thread.
If you are not aware of how or where your objects are bieng modified by other threads then you have bigger issues to resolve as you don't seem to understand the code or the framework you are using.
If or when you do understand why and when this is happening you will need to implement some synchronization mechanism or steer (preferable) towards immutable implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With