0

I have a concept that involves a world being simulated. Lots of entities and processes acting upon them, not to mention interactions, etc...

Every SimulationTick, the simulation updates (preferably a fixed interval, I don't expect the simulation to tax the CPU and don't need a fast tick [a few/second]).

The game will allow players to see an environment that allows them to interact with the simulation.

I need the simulation to be far larger than the area visible at any given time, and I don't need any Unity functionality, so it could in theory run completely isolated, just passing back relevant state as needed.

What's the best way to architect this application?

At one extreme, I could have the simulation running in its own process, exposing a network port/RPC call for the game engine to query/invoke.

At the other, I could just build it all into one assembly and tie the simulation tick to either physics or some other time keeping mechanism.

It feels like it should have its own thread, but what spawns/manages it? I'm guessing that if Unity serialises the object holding the reference to the thread, it'll lose it? How do I work around that?

Basic
  • 1,267
  • 9
  • 25
  • 1
    This sounds like something sufficiently solved by threads. You can see How to not freeze the main thread in Unity for some starting points on that front. What specifically is the serialization case where you're worried the thread would be lost? (Or better, what is the concrete test case you've tried in which you've verified the thread is lost?) If you ask a question about that specific problem, we can help you solve it. – DMGregory Nov 21 '21 at 04:00
  • I went down that rabbit hole before... I'm more than familiar with multithreading outside Unity, and I'm happy using semaphores/mutexes to signal back and forth with a minimum of costly locks. It's more that the runtime [especially in the editor but also in builds] has a habit of recycling objects. I could just spawn a background thread and trust the exception handling is sufficient that it'll run forever, however, I'd rather keep a reference [to the thread and a signal) so I can do things like shutdown gracefully and repopulate when saving/loading a game. – Basic Nov 21 '21 at 19:55
  • @DMGregory [Sorry, forgot to @ you]. Put this another way... If it were your game, where would you put the code block to spawn the simulation thread? – Basic Nov 21 '21 at 19:55
  • Fortunately enough, there's an example of how to do that in the link I shared above. If that approach doesn't work for your needs, can you edit your question to give us a concrete example of where it falls short? – DMGregory Nov 21 '21 at 19:58

0 Answers0