2016-06-08 85 views
2

我想将所有颜色值存储在一个名为“colors-rgb.lua”的单独文件中,然后在需要时按名称抓住它们。该文件的基本结构是:在Lua中使用另一个模块时遇到问题

colorsRGB = { 
    aliceblue = {240, 248, 255}, 
    antiquewhite = {250, 235, 215}, 
    aqua = { 0, 255, 255}, 
    aquamarine = {127, 255, 212}, 
    azure = {240, 255, 255}, 
    beige = {245, 245, 220}, 
    bisque = {255, 228, 196}, 
    black = { 0, 0, 0}, 
    ... 
} 

在我main.lua,我有

local colors = require("colors-rgb") 
local blue = colors.colorsRGB.aliceblue 

这使我的错误“试图指数当地‘颜色’(一个布尔值)”

我在做什么错了?

回答

1

您在colors-rgb.lua文件中丢失了return {colorsRGB = colorsRGB}。由于你没有返回任何东西,Lua保存了你模块的执行状态(作为布尔值),并作为require调用的结果返回。这就是为什么你得到试图索引布尔值的错误。

请参阅从编程Modules and Packages章在Lua 2

+0

在这种情况下,表格仍然可以被访问,但它被称为colorsRGB,因为它被声明为全局的。他应该将其声明为本地,然后从模块中返回。 – user6245072

+0

正确;尽管意图显然是通过'require'的结果来访问它的。 –

0

颜色,rgb.lua需要返回一个值。

local colorsRGB = { 
    aliceblue = {240, 248, 255}, 
    antiquewhite = {250, 235, 215}, 
    aqua = { 0, 255, 255}, 
    aquamarine = {127, 255, 212}, 
    azure = {240, 255, 255}, 
    beige = {245, 245, 220}, 
    bisque = {255, 228, 196}, 
    black = { 0, 0, 0}, 
} 
return colorsRGB