在不同的Lua脚本文件夹中使用全局变量

我在文件夹 folder1/config.lua 中定义了一个全局变量:max_channel = 40

现在我想在不同文件夹中的另一个脚本中使用此值,比如 folder2/script2.lua,这是我尝试的代码:

local channel = require "folder1/config"
numberOfchannel = channel.max_channels

当我尝试使用 numberOfchannel 时,编译器将其视为字符串而不是整数值 40。为什么会这样?

更新:这是我如何使用 numberOfchannel

if num < numberOfchannel then
...........
attempt to compare number with nil

注意,num 是一个数字,如果我放置例如 40if 语句可以正常工作。

这里是文件 folder1.config.lua 的开头:

module(..., package.seeall)
max_channels = 40

更新 在 greatwolf 的建议下,我尝试显示局部变量 channel 的内容,但收到错误消息:

    stdin:1: bad argument #1 to 'pairs' (table expected, got nil)
stack traceback:
        [C]: in function 'pairs'
        stdin:1: in main chunk
        [C]: ?

点赞
用户1517394
用户1517394

module 已被弃用。

config.lua 中,只需这样设置:

return {
    max_channels = 40
}
2014-12-27 13:32:35