为什么我得到一个线程而不是参数? [英] Why am I getting a thread instead of my parameters?

查看:76
本文介绍了为什么我得到一个线程而不是参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C / C ++游戏中的lua 5.3,以便对其行为的某些部分进行脚本编写。

I am using lua 5.3 from my C/C++ game to allow certain parts of its behavior to be scripted.

在C ++程序中,我调用的每一帧lua函数 main 的方式如下:

From the C++ program, every frame I call the lua function main in the following manner:

lua_getfield(VMState, LUA_GLOBALSINDEX, "main");
int result = lua_pcall(VMState, 0, 0, 0);

我希望脚本定义一个名为 main ,它可以完成很多工作。例如,我可以使用一个脚本来执行以下操作:

I expect the script to define a function called main, which does a bunch of stuff. For example, I can have a script that does something like this:

local f = function()
    draw_something({visible = true, x = 0, y = 0})
end

main = function()
    f()
end

draw_something()是C代码的回调,它做一些有趣的事情并带有传递的参数:

draw_something() is a callback to the C code, which does something interesting with the parameters passed:

lua_getfield(VMState, 1, "visible");
bool visible = (bool)lua_toboolean(VMState, 2); lua_pop(VMState, 1);
if (!visible)
    return;
// Do some other stuff

有趣的是,在此回调被执行时称为,我作为参数传递给lua端的 do_something 的匿名表现在位于堆栈位置1,因此我可以调用 lua_getfield() 从C端访问可见 字段,并对其进行处理。

Of interest, is that by the time this callback is called, the anonymous table I passed as a parameter to do_something in the lua side, is now at stack position 1, so I can call lua_getfield() from the C side, to access the "visible" field, and do something with it.

这很好用,多年来我已经做了很多类似的事情。

This works pretty well, and I've done lots of stuff like this for years.

现在,我想将lua调用转换为 f 到一个协程,所以我从lua方面做这样的事情:

Now, I want to convert the lua call to f to a coroutine, so I do something like this from the lua side:

local f = function()
    draw_something({visible = true, x = 0, y = 0})
end

local g = coroutine.create(function()
    while true do
        f()
        coroutine.yield()
    end
end

main = function()
    coroutine.resume(g)
end

结果应该是相同的,但是现在轮到通过将调用移至协程内部的 draw_something(),我传递给该函数的参数(应该是一个表)现在是线程了吗? ( lua_istable()返回0,而 lua_isthread()返回1)。

The result should be the same. However, it now turns out that by moving the call to draw_something() inside a coroutine, the parameter I had passed to the function, which should have been a table, is now a thread? (lua_istable() returns 0, while lua_isthread() returns 1).

有趣的是,向回调函数传递多少个参数无关紧要:0、1、4、50,从回调内部只能得到一个参数,它是一个线程。

Interestingly, it doesn't matter how many parameters I pass to my function: 0, 1, 4, 50, from inside the callback I'm only getting one parameter, and it is a thread.

由于某种原因,这是我导出的某些功能(并非全部)发生的。我在导出不同函数的方式上看不出任何区别。

For some reason, this is happening with some functions that I exported, but not all. I can't see any difference in the way I'm exporting the different functions though.

lua是否有理由将我的参数切换到线程上?

Is there any reason why lua would switch my parameters to a thread?

推荐答案

我找到了答案。

事实证明<$在 lua_CFunction 上传递给您的c $ c> lua_State 不能保证与您第一次在<$ c上获得的相同$ c> lua_newstate()

It turns out that the lua_State that is passed to you on a lua_CFunction is not guaranteed to be the same to the one you first got on lua_newstate()

我想每个协程可能都有自己的 lua_State 。如果您总是在 lua_State 上进行操作,而您在 lua_newstate()上操作,则协程可能会出现问题,所以您必须确保您始终使用在 lua_CFunction 上传递的 lua_State

I suppose that each coroutine might get its own separate lua_State. If you always do stuff on the lua_State you got on lua_newstate(), you might have problems with coroutines, so you have to ensure you always use the lua_State you got passed on your lua_CFunction.

这篇关于为什么我得到一个线程而不是参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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