Hello! I am making a Minecraft like game, and I have a chunk loading system. However, whenever I go into a chunk that has already been loaded, all chunks that are loaded randomly either disappear or appear. I can still collide with them, but they disappear! Here is the code that might be causing the problem:
if (chunksToLoad.Count > 0)
{
if (!chunks.ContainsKey(new Vector2Int(chunksToLoad[0].x, chunksToLoad[0].z)) || chunks[new Vector2Int(chunksToLoad[0].x, chunksToLoad[0].z)] == null)
{
chunks.Add(new Vector2Int(chunksToLoad[0].x, chunksToLoad[0].z), new Chunk(chunksToLoad[0]));
chunks[new Vector2Int(chunksToLoad[0].x, chunksToLoad[0].z)].isActive = true;
activeChunks.Add(chunksToLoad[0]);
chunksToLoad.RemoveAt(0);
}
}
playerChunkCoord = GetChunkCoordFromVector3(player.position);
// Check view distance
if (!playerChunkCoord.Equals(playerLastChunkCoord))
CheckViewDistance();
if (chunksToDraw.Count > 0)
chunksToDraw.Dequeue().CreateMesh();
if (!settings.enableThreading)
{
if (!applyingModifications)
ApplyModifications();
if (chunksToUpdate.Count > 0)
UpdateChunks();
}
}
}
public void AddChunkToUpdate(Chunk chunk)
{
AddChunkToUpdate(chunk, false);
}
public void AddChunkToUpdate(Chunk chunk, bool insert)
{
lock (ChunkUpdateThreadLock)
{
if (!chunksToUpdate.Contains(chunk))
{
if (insert)
chunksToUpdate.Insert(0, chunk);
else
chunksToUpdate.Add(chunk);
}
}
}
void UpdateChunks()
{
lock (ChunkUpdateThreadLock)
{
chunksToUpdate[0].UpdateChunk();
if (!activeChunks.Contains(chunksToUpdate[0].coord))
activeChunks.Add(chunksToUpdate[0].coord);
chunksToUpdate.RemoveAt(0);
}
}
void CheckViewDistance()
{
ChunkCoord coord = GetChunkCoordFromVector3(player.position);
playerLastChunkCoord = playerChunkCoord;
List<ChunkCoord> previouslyActiveChunks = new List<ChunkCoord>(activeChunks);
//List<ChunkCoord> keepChunks = new List<ChunkCoord>();
//activeChunks.Clear();
for (int x = coord.x - settings.viewDistance; x < coord.x + settings.viewDistance; x++)
{
for (int z = coord.z - settings.viewDistance; z < coord.z + settings.viewDistance; z++)
{
ChunkCoord thisChunkCoord = new ChunkCoord(x, z);
if (!chunks.ContainsKey(new Vector2Int(x, z)) || chunks[new Vector2Int(x, z)] == null)
{
//chunks.Add(new Vector2Int(x, z), new Chunk(thisChunkCoord));
if (!chunksToLoad.Contains(new ChunkCoord(x, z)))
chunksToLoad.Add(new ChunkCoord(x, z));
}
else
{
chunks[new Vector2Int(x, z)].isActive = true;
activeChunks.Add(thisChunkCoord);
}
}
}
for (int i = 0; i < previouslyActiveChunks.Count; i++)
{
if ((previouslyActiveChunks_.x > coord.x - settings.viewDistance && previouslyActiveChunks*.x < coord.x + settings.viewDistance) &&*_
(previouslyActiveChunks_.z > coord.z - settings.viewDistance && previouslyActiveChunks*.z < coord.z + settings.viewDistance))
previouslyActiveChunks.RemoveAt(i);
}
foreach (ChunkCoord c in previouslyActiveChunks)
{
// chunks[c.x, c.z].ClearMeshData();
chunks[new Vector2Int(c.x, c.z)].isActive = false;
activeChunks.Remove(c);
}
}
Please help! I have no idea what could be causing it, but I did notice one thing. Before, that for loop going through the previouslyActiveChunks list was in the double for loop, checking if any of the list contents contained the (x, z) chunk coord. I changed it to what you see as the second to last loop in the code to make it more efficient. That made it worse. Thank you for your time!
[203895-screenshot-376.png*|203895]*
_*