Lua协程错误:试图跨元方法/C调用边界屈服 [英] Lua coroutine error: tempt to yield across metamethod/C-call boundary

查看:97
本文介绍了Lua协程错误:试图跨元方法/C调用边界屈服的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个游戏引擎,允许您在Lua中编程.游戏引擎命令位于从C创建的DLL中.在C中创建了一个调用Lua文件的exe.这个Lua文件是放置所有游戏代码(包括主循环)的地方. exe没有来回关系,但是您可以从DLL中调用函数.

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.

因此,在主循环之前的这里,我创建了一个函数,将从中创建一个协程.该函数在一个很大的表上进行迭代,因此每产生n次迭代.这个函数有一个无限的while循环,因为我需要这个东西来运行主游戏循环的每个循环,但是如果在多个循环之间进行分配就可以了.

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.

运行代码时,我收到错误消息:试图跨元方法/C调用边界屈服

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

我在网上阅读一些东西,但并没有真正理解这里的问题. exe调用Lua文件后,它直到Lua文件完成后才返回到exe文件.由于我的主循环位于Lua文件中,因此在我的测试用例中它永远不会完成.

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.

那我有什么选择呢?

推荐答案

该错误告诉您,您正在尝试从Lua代码内部进行屈服,而在执行屈服的Lua代码与执行该操作的Lua代码之间存在一些C函数恢复了协程.要解决此错误,您需要做的是从Lua调用一些C函数,该函数会回调Lua代码,然后再调用coroutine.yield().

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.

这篇关于Lua协程错误:试图跨元方法/C调用边界屈服的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆