在不同文件夹的不同Lua脚本中使用全局变量
我在名为在不同文件夹的不同Lua脚本中使用全局变量,lua,global-variables,scope,require,Lua,Global Variables,Scope,Require,我在名为folder1/config.lua的文件中定义了一个全局变量:max\u channel=40 现在我想在另一个文件夹中的anthors脚本中使用这个值,比如:folder2/script2.lua下面是我尝试过的: local channel = require "folder1/config" numberOfchannel = channel.max_channels 当我尝试使用数字频道>代码>时,编译器将其视为字符串而不是值的整数 40 。为什么会这样 更新:以下是我如何尝
folder1/config.lua的文件中定义了一个全局变量:max\u channel=40
现在我想在另一个文件夹中的anthors脚本中使用这个值,比如:folder2/script2.lua
下面是我尝试过的:
local channel = require "folder1/config"
numberOfchannel = channel.max_channels
当我尝试使用<代码>数字频道>代码>时,编译器将其视为字符串而不是值的整数<代码> 40 。为什么会这样
更新:以下是我如何尝试使用numberOfchannel
:
if num < numberOfchannel then
...........
attempt to compare number with nil
UPADTE
在greatwolf的建议之后,我试图将内容显示在频道
局部变量中,但我收到了一条错误消息,上面说:
stdin:1: bad argument #1 to 'pairs' (table expected, got nil)
stack traceback:
[C]: in function 'pairs'
stdin:1: in main chunk
[C]: ?
模块
已弃用
在config.lua
中,只需执行以下操作:
return {
max_channels = 40
}
当我尝试使用numberOfchannel时
-您打算如何使用它?您的文件夹1/config.lua
是什么样子?如果您未使用模块
,则max_channel=40
设置全局max_channels
。我已更新问题您是否收到错误?print(type(channel.max_channels))
说什么?@Engine对于k,v成对(channel)的打印(k,v)结束
我将在下个月5日尝试,但这是为了您的帮助
return {
max_channels = 40
}