卢阿&隐式全局状态
我目前正在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在函数内部使用的任何链接都不会更改其外部的 var。您应该像这样使用 smthg:
不一定有“本地”。
作为替代方案,我可以建议您使用非安全方法:
Any link you use inside the function won't change the var outside of it. You should use smthg like this:
There is 'local' isn't necessarily.
As alternative I can suggest you to use non-safety method:
我发现表作为引用传递,我可以从函数内部修改它们,如下所示:
在 Lua 内部:
将导致:
I found out that tables are passed as references and I'm able to modify them from inside the function like this:
And inside Lua:
Will result in: