【问题标题】:using a lua file to require other lua files?使用 lua 文件需要其他 lua 文件?
【发布时间】:2018-10-11 16:20:27
【问题描述】:

有人可以告诉我在 lua 中是否可以做这样的事情? 它在哪里使用模块文件来包含使用单个 lua 标头的其他模块文件?

--main.lua
require "std"
local test = WIDGETS[0]

--std.lua
require "std.constants" -- this is the problem its local to this file only
require "std.functions" -- this is the problem its local to this file only

-std.constants.lua
WIDGETS = 
{
   NONE,
   PANEL,
   BUTTON
}

我需要做这样的事情,所以我不必输入 std.constants.WIDGET[whatever]

【问题讨论】:

    标签: lua require


    【解决方案1】:

    您可以在 require "std" 之后添加行 local WIDGETS = std.constants.WIDGETS。然后,该文件中的所有函数都可以引用WIDGETS,而不会污染全局命名空间:

    -- main.lua
    require "std"
    local WIDGETS = std.constants.WIDGETS
    
    local test = WIDGETS[0]
    ...
    

    您只需对每个文件执行一次此操作。

    【讨论】:

    • 就是这样,尽管我希望它把它放在全局命名空间中,这就是我这样做的原因。我不想要求它并将所有函数和常量分配给每个文件的变量,可能有数百个函数和常量
    猜你喜欢
    • 1970-01-01
    • 2019-10-12
    • 2013-03-26
    • 2021-10-01
    • 1970-01-01
    • 2013-06-09
    • 2017-05-10
    • 2015-02-19
    • 1970-01-01
    相关资源
    最近更新 更多