1
\$\begingroup\$

I have a voxel game similar to Minecraft. I have a funciton PushLoadedChunks(Direction) that pushes all of the loaded chunks forward, backward, left, right. This is done with sfd.BeginInvoke(PushLoadedChunks, null, null).

This works great until I fire two of these quickly, which happens if the user goes diagnol. It errors out because two threads are pulled trying to modify the same data, both writing to the same thing because they try to load the same chunks. I wanted to do something like this:

Create one standard thread and a queue to push through. Instead of invoking PushLoadedChunks, I wanted to add it to the queue and have that single thread work through each chunk one after another, proving to be slower performing but more stable code.

I am struggling because Invoke seems to wait for the chunks to return which works, but freezes my player until it is loaded. I need reference in creating a "queue" of sorts, or help setting up a single thread to run each method when they are called and wait until one is finished to do another.

Does anyone have any advice or resources I could look at to see the best way to complete my task?

\$\endgroup\$
1
\$\begingroup\$

My solution of problem.

See chunkGenerationThread for chanks generation and processBlockUpdatesPackagesThread for update chunks when user set or remove blocks.

There are lot of code, but allmost no comments. In general, how it works: set/remove block -> add changes to queue package -> wait untill Update and send package for processing to queue -> in separate thread check queue and process changes and update mesh -> use new mesh :)

If you want, for details ask in comments or in chat.

Perhaps it's not the best implementation (LOL), but it works. Most of time. I'm still working on it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.