【发布时间】:2015-02-06 04:26:17
【问题描述】:
我在名为 folder1/config.lua 的文件中定义了一个全局变量:max_channel = 40。
现在我想在另一个文件夹中的 anthors 脚本中使用这个值说: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 是一个数字,当我输入40 时,if 语句可以正常工作
这是folder1.config.lua文件的请求:
module(..., package.seeall)
max_channels = 40
UPADTE
在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]: ?
【问题讨论】:
-
when I try to use numberOfchannel- 你是如何使用它的? -
你的
folder1/config.lua是什么样的?如果您不使用module,则max_channel = 40设置全局max_channels。 -
我已经更新了问题
-
您遇到错误了吗?
print(type(channel.max_channels))说什么? -
@Engine
for k, v in pairs(channel) do print(k, v) end
标签: lua global-variables scope require