Mitchell Griest
Mitchell Griest

Reputation: 507

Trouble using another module in Lua

I would like to store all my color values in a separate file, called "colors-rgb.lua", then just grab them by name when I need them. The basic structure of that file is:

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},
    ...
}

In my main.lua, I have

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

Which gives me the error "Attempt to index local 'colors' (a boolean value)"

What am I doing wrong?

Upvotes: 2

Views: 66

Answers (2)

Maicon Feldhaus
Maicon Feldhaus

Reputation: 226

The colors-rgb.lua needs to return a value.

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

Upvotes: 0

Paul Kulchenko
Paul Kulchenko

Reputation: 26794

You are missing return {colorsRGB = colorsRGB} in your colors-rgb.lua file. Since you didn't return anything, Lua saved the execution status of your module (as a boolean value) and returned it as the result of require call. That's why you get the error about attempting to index a boolean value.

See Modules and Packages chapter from Programming in Lua 2.

Upvotes: 1

Related Questions