gpt4 book ai didi

c - 从 lua C 函数返回几个参数

转载 作者:太空狗 更新时间:2023-10-29 16:07:47 26 4
gpt4 key购买 nike

我想从 C 函数中获取 Lua 中的几个参数。我试图在 lua 堆栈上推送几个参数:

static int myFunc(lua_State *state)
{
lua_pushnumber(state, 1);
lua_pushnumber(state, 2);
lua_pushnumber(state, 3);

return 1;
}

然后像这样在 Lua 中调用它:

local a,b,c = myFunc()

不幸的是,b 和 c 值为 nil。我不想为我需要的每个值都编写一个函数,而是利用 Luas 功能从一个函数中检索多个参数。

最佳答案

C函数的返回值是返回值的个数。

将其更改为 return 3; 就可以了。

这里,引用Programming in Lua:

static int l_sin (lua_State *L) {
double d = lua_tonumber(L, 1); /* get argument */
lua_pushnumber(L, sin(d)); /* push result */
return 1; /* number of results */
}

关于c - 从 lua C 函数返回几个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19544611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com