I am in the process of designing my own Game Engine for use in a real time CRPG - think Baulder's Gate. I know most of my specifications right now, and I am currently in a planning phase.

I chose Lua for a good number of reasons.

  • Fast and real time iteration.
  • Easy to lay out data and serialize tables.
  • Easy to learn for other programmers.
  • Can design tools to automatically write scripts for certain entities

For the first reason, I decided to have as much of my game code handled inside Lua as possible. With the exception of low level systems being handled inside of C++: Audio, physics, animation, rendering, resource managers(?).

But I came to a problem when it came to threading. I settled on using a job system. But I am not sure how to approach this with a heavy emphasis on Lua.

My Update look will look something like...

Local dt {}
While Game.isRunning() {
    dt = timer.delta();
    PreUpdate(dt);           //Delayed Raycasts for AI, Physics Triggers
    UpdateLogic(dt);         //AI, UI, Event Handling, etc.
    UpdateAnimations(dt);    //State Machines, Blending, framed scripts

How should I approach threading this?

My first thought is that I have only one VM of Lua, and set it on the main thread; then, the worker threads will work only in C++. Lua will have C++ API callbacks. When Lua calls this callback, it returns to C++. The C++ will create a job to be dispatched. The job is placed inside a qeue and processed on other threads in native code. I was thinking of following this idea. Most games seem to handle Game Logic in a serial manner -UpdateLogic(dt)-, then asynchronously updates lower level systems, multi-thread rendering, or handle events that are needed to fire immediately.

My Second thought is to use a job system with all threads using a Lua instance. I guess with this system I would be able to update entities in a threaded manner. But I can quickly see the problem. Because an entity's data is mutable, this can quickly cause logical errors. Even if I decided to defer updates to data, I can still see this becoming a problem with math errors.

Any guidance would be helpful here.


Your Answer

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

Browse other questions tagged or ask your own question.