卢阿&隐式全局状态

发布于 2024-10-20 04:40:54 字数 652 浏览 6 评论 0原文

我目前正在将 Lua 集成到我的项目中,并且在此过程中我遇到了一个小设计问题。目前,如果我想将主机应用程序中的信息获取到 Lua 脚本中,我会以这种方式调用我在 C 中注册的函数:

-- Inside lua
local state = host.get_state()
-- Do something with "state"

现在的问题是:状态显然可以改变,并且“state”变量将那么就会过时并且很可能无效。到目前为止,我接受了这一点,因为并不经常需要全局状态。在以下情况下问题更大:

local user = host.get_user('id')
host.set_user_flags(user, 'abc')
-- internally "user" is now updated, but to get the accurate information in Lua, I
-- will have to explicitly redo "user = host.get_user('id')" for every operation
-- that accesses this table

我读了一些有关参考文献的内容,我认为它们可以帮助我解决这个问题,但我并没有真正理解它。

有没有办法像我在 C 中那样直接抛出指针?

I'm integrating Lua into my project at the moment, and I'm facing a small design problem on the way. Currently, if I want to get information from my host application into Lua scripts, I call a function that I registered in C, in this fashion:

-- Inside lua
local state = host.get_state()
-- Do something with "state"

Now the problem with that is: the state can obviously change, and the "state" variable will then be outdated and most likely invalid. So far I lived with this because the global state isn't needed too often. It's more problematic in the following scenario:

local user = host.get_user('id')
host.set_user_flags(user, 'abc')
-- internally "user" is now updated, but to get the accurate information in Lua, I
-- will have to explicitly redo "user = host.get_user('id')" for every operation
-- that accesses this table

I have read a bit about references and I think they can help me with this, but I didn't really understand it.

Isn't there some way to just throw around pointers like I can do it in C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玻璃人 2024-10-27 04:40:54

您在函数内部使用的任何链接都不会更改其外部的 var。您应该像这样使用 smthg:

local count
function MyFunc()
      count = 1
end

不一定有“本地”。

作为替代方案,我可以建议您使用非安全方法:

count = 0
function MyFunc(v) --we will throw varname as a string inside
     _G[v] = 1 -- _G is a global environment
end
MyFunc('count')
print(count)

Any link you use inside the function won't change the var outside of it. You should use smthg like this:

local count
function MyFunc()
      count = 1
end

There is 'local' isn't necessarily.

As alternative I can suggest you to use non-safety method:

count = 0
function MyFunc(v) --we will throw varname as a string inside
     _G[v] = 1 -- _G is a global environment
end
MyFunc('count')
print(count)
倾城泪 2024-10-27 04:40:54

我发现表作为引用传递,我可以从函数内部修改它们,如下所示:

static int dostuff(lua_State *L)
{
    lua_pushstring(L, "b");
    lua_pushnumber(L, 23);
    lua_settable(L, 1);

    return 0;
}
/* lua_register(L, "dostuff", &dostuff); */

在 Lua 内部:

t = { a = 'a', b = 2 }

print(t.a, t.b)
dostuff(t)
print(t.a, t.b)

将导致:

a   2
a   23

I found out that tables are passed as references and I'm able to modify them from inside the function like this:

static int dostuff(lua_State *L)
{
    lua_pushstring(L, "b");
    lua_pushnumber(L, 23);
    lua_settable(L, 1);

    return 0;
}
/* lua_register(L, "dostuff", &dostuff); */

And inside Lua:

t = { a = 'a', b = 2 }

print(t.a, t.b)
dostuff(t)
print(t.a, t.b)

Will result in:

a   2
a   23
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文