0
votes

In the game I'm making, I'm developing it mostly in C#, but I'd like to handle some of the game logic using Lua coroutines, because they can represent actions taken over multiple frames nicely. I've never actually used Lua before though so I'm not sure if this will work. Ideally I'd like the C# game update function to be able to add one or more Lua coroutines to be executed to a queue, and then each frame, iterate over the queue, executing each coroutine until it yields or finishes. I just want to know if this is possible, and if so, how to implement it, because it doesn't seem to fit with the "lua_dofile" paradigm that Lua uses.

Here's some C# pseudocode to show what I mean:

GameUpdate()
{
  // Update game logic
  // (which may add one or more Lua coroutines to the queue)

  // Update input, physics, sound, etc

  // In Lua:
  //  For each coroutine in queue,
  //  execute it until it reaches a yield,
  //  or it finishes, whichever comes first
}

GameRender()
{
  // Render a frame
}

UPDATE: This article (http://www.gamedev.net/topic/585128-luainterface-and-coroutines/) gives the answer I was looking for. In short, it is possible.

1
Lua doesn't have a "lua_dofile paradigm". - Nicol Bolas

1 Answers

3
votes

Yes it's possible. It fits fine with the "dofile paradigm": the host app exposes a means for the script to register a callback for the host to call; e.g. RegisterEventHandler(foo), hostTypeInstance.OnFoo = foo, etc.

When the host runs a user script, the script can pass the host callback functions/coroutines, and the host saves handles to them.

The host can call/resume the functions/coroutines every frame, whenever the mouse is clicked, or whatever else you've arraigned for them to be called.