I tried to do threading to load chunk of voxels It worked in series but when implemented in parallel threading it was not working like I wanted it, I even used Chatgpt and other AI tools but It didn’t help as expected Here is my code (please don’t judge It’s been a little edited by AI XD)
import threading
import queue
from settings import *
from world_objects.chunk import Chunk
from voxel_handler import VoxelHandler
class World:
def __init__(self, app):
self.app = app
self.world_height = WORLD_H + BUILD_C_HEIGHT_LIMIT
self.chunks = {}
self.voxel_pointers = np.empty([WORLD_W * WORLD_D * self.world_height, CHUNK_VOLUME], dtype='uint8')
self.voxel_handler = VoxelHandler(self)
self.render_distance = 7
self.chunk_queue = queue.Queue()
self.main_thread_queue = queue.Queue()
self.stop_threads = False
self.update_active_chunks()
self.chunk_thread = threading.Thread(target=self.chunk_worker, daemon=True)
self.chunk_thread.start()
def update(self):
self.voxel_handler.update()
self.update_active_chunks()
self.process_main_thread_tasks()
def chunk_worker(self):
while not self.stop_threads:
try:
pos = self.chunk_queue.get(timeout=1)
print(f"Generating chunk at {pos}") # Debug print
self.generate_chunk_data(pos)
self.chunk_queue.task_done()
except queue.Empty:
continue
def process_main_thread_tasks(self):
while not self.main_thread_queue.empty():
task = self.main_thread_queue.get()
task()
self.main_thread_queue.task_done()
def update_active_chunks(self):
player_pos = self.app.player.position
print(f"Player position: {player_pos}")
player_chunk_pos = (
int(player_pos[0] // CHUNK_SIZE),
int(player_pos[1] // CHUNK_SIZE),
int(player_pos[2] // CHUNK_SIZE)
)
render_x_min = max(0, player_chunk_pos[0] - self.render_distance)
render_x_max = min(WORLD_W, player_chunk_pos[0] + self.render_distance)
render_y_min = max(0, player_chunk_pos[1] - self.render_distance)
render_y_max = min(self.world_height, player_chunk_pos[1] + self.render_distance)
render_z_min = max(0, player_chunk_pos[2] - self.render_distance)
render_z_max = min(WORLD_D, player_chunk_pos[2] + self.render_distance)
active_chunk_positions = {
(x, y, z)
for x in range(render_x_min, render_x_max)
for y in range(render_y_min, render_y_max)
for z in range(render_z_min, render_z_max)
}
print(f"Active chunk positions: {active_chunk_positions}")
self.load_and_unload_chunks(active_chunk_positions)
def load_and_unload_chunks(self, active_chunk_position):
current_chunk_positions = set(self.chunks.keys())
chunks_to_load = active_chunk_position - current_chunk_positions
chunks_to_unload = current_chunk_positions - active_chunk_position
for pos in chunks_to_load:
print(f"Queueing chunk for loading at {pos}")
self.chunk_queue.put(pos)
for pos in chunks_to_unload:
self.unload_chunk(pos)
def generate_chunk_data(self, pos):
# Generate chunk data without OpenGL operations
x, y, z = pos
chunk = Chunk(self, position=pos)
chunk_index = x + WORLD_W * z + WORLD_AREA * y
self.voxel_pointers[chunk_index] = chunk.build_voxels()
chunk.voxels = self.voxel_pointers[chunk_index]
# Add chunk to chunks dictionary (thread-safe)
with threading.Lock():
self.chunks[pos] = chunk
# Queue the chunk for mesh building in the main thread
self.app.queue_main_thread_task(lambda: self.build_chunk_mesh(chunk))
def build_chunk_mesh(self, chunk):
try:
chunk.build_mesh()
print(f"Chunk mesh built at {chunk.position}")
except Exception as e:
print(f"Error building mesh for chunk at {chunk.position}: {e}")
self.unload_chunk(chunk.position)
def unload_chunk(self, pos):
if pos in self.chunks:
print(f"Unloading chunk at {pos}")
del self.chunks[pos]
def render(self):
for chunk in self.chunks.values():
if chunk and chunk.mesh and chunk.mesh.program:
print(f"Rendering chunk at {chunk.position}") # Debug print
chunk.render()
else:
if chunk:
print(f"Chunk at {chunk.position} not rendered due to uninitialized mesh or program.")
else:
print(f"Chunk is None")
def queue_main_thread_task(self, task):
self.main_thread_queue.put(task)
def shutdown(self):
self.stop_threads = True
self.chunk_thread.join()
I use moderngl, pygame, numpy, numba There are no voxels or chunks creating in this can someone please help I can’t sleep at night thinking about what I did wrong