Lua coroutine error: temptation to exit metamethod / C-call border

I use a game engine that allows you to program in Lua. The game engine commands are located in a DLL created from C. In C, an exe is created that calls the Lua file. In this Lua file, you put all of your game code, including the main loop. There is no going back and forth with exe, but you can call functions from a DLL.

So, here, before the main loop, I create a function from which I am going to create a coroutine. This function iterates over a rather large table, so every n iterations I give way. This function has an infinite while loop, because I need this stuff to run each cycle of the main game loop, but it is normal if it is split between several cycles.

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

When I run my code, I get an error: the temptation to exit through the metamethod / C-call border

I read some materials on the Internet, but did not understand what the problem was. As soon as exe names the Lua file, it does not return to exe at all until the Lua file is finished, and since I have my main loop in the Lua file, it never ends in my test case.

What are my options with this?

+4
source share
2 answers

The error tells you what you are trying to get from Lua code, where there is some C function between the Lua code doing yielding and the Lua code that resumed the coroutine. To hit this error, you need to call some C function from Lua, which returns the Lua code, which then calls coroutine.yield() .

You cannot do this. You should instead rebuild your code to avoid this. Since you did not specify any code, this cannot be much.

+8
source

There are a few things you can do if you cannot change your code to avoid the C / metamethod border:

  • If you use standard Lua and compile it yourself, try fixing it with Coco - True C Coroutines for Lua .

    The True C coroutine symbolism means that you can get from the coroutine along the border of the C call and return to it.

  • Try using LuaJIT instead of the standard Lua interpreter. It uses a fully revolving virtual machine , which means that the border is not a problem.

  • Try using Lua 5.2 . It has valid 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.

+4
source

Source: https://habr.com/ru/post/1385685/


All Articles