2016-06-08 82 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