6
votes

I'm using a game engine that allows you to program in Lua. The game engine commands are in a DLL created from C. There is a exe created in C that calls a Lua file. This Lua file is where you put all your game code including the main loop. There is no going back and forth with the exe, but you can call functions from the DLL.

So in here before the main loop I create a function which I'm going to create a coroutine from. This function iterates over a pretty big table so every n iterations I yield. This function has an infinite while loop around that because I need this stuff to run every single cycle of the main game loop, but it's OK if it's split between multiple cycles.

I then create a coroutine with this function as the parameter. In the main game loop I then resume this coroutine.

When I run my code I get the error: tempt to yield across metamethod/C-call boundary

I was reading some stuff online but not really understanding what the issue is here. Once the exe calls the Lua file it doesn't go back to the exe at all until the Lua file is finished, and since I have my main loop in the Lua file it never finishes in my test case.

What are my options with this then?

2

2 Answers

11
votes

The error is telling you that you are attempting to yield from within Lua code where there is some C function between the Lua code doing the yielding and the Lua code that resumed the coroutine. To hit this error, what you have to have done is call some C function from Lua, which calls back into Lua code, which then calls coroutine.yield().

You can't do that. You must instead restructure your code to avoid this. Since you didn't provide any code, there's not much that can be suggested.

6
votes

There are several things you can do if you cannot change your code to avoid the C/metamethod boundary:

  • If you are using standard Lua, and are compiling it yourself, try patching it with Coco — True C Coroutines for Lua.

    True C coroutine semantics mean you can yield from a coroutine across a C call boundary and resume back to it.

  • Try using LuaJIT instead of the standard Lua interpreter. It uses a fully resumable VM meaning the boundary is not an issue.

  • Try using Lua 5.2. It features yieldable pcall and metamethods which means that it can handle your problem. However, there are some changes and incompatibilities between Lua 5.1 and Lua 5.2.