Multiple variables in module file

Hi,

I am working on a project with the composer and am trying to make globally accessible color tables. My game has multiple themes, and I would like to keep all of the themes organized in one external lua file. This way, every scene has access to the different color tables (which are changed depending on which theme the player has picked).

I have the module loading properly into each scene, but my only problem is in creating multiple tables/functions within each module.

Here’s what I have:

All scene files import scene.functions.themes:

local color\_palettes = require( "scene.function.themes" ) package.loaded["scene.function.themes"]

My scene.functions.themes (there will be more themes, but I cut it down to two for sake of this thread)

local palette\_1 = { [1] = "0", [2] = "0", [3] = "1", } return palette\_1 local palette\_2 = { [1] = "1", [2] = "1", [3] = "1", } return palette\_2

Once loading in the module, I receive the following error:

error loading module 'scene.functions.themes' from file '/Users/admin/Documents/theme-test/scene/functions/themes.lua': /Users/admin/Documents/theme-test/scene/functions/themes:3: '\<eof\>' expected near 'local' stack traceback: [C]: ? [C]: in function 'require' ?: in function 'require'

Is this a normal error in the sense that I should just create a new module file for each theme? I’d like to avoid this though, as I will have about 15 themes and would not like to load that many files into each level scene.

Thanks!

local color\_palettes = require( "scene.function.themes" ) package.loaded["scene.function.themes"]

I don’t think you need to do the package.loaded() statement.

Modules have a defined format. First you create a table and you add your module objects to the table and return the table. Something like this:

local M = {} M.palette\_1 = { [1] = "0", [2] = "0", [3] = "1", } M.palette\_2 = { [1] = "1", [2] = "1", [3] = "1", } return M

Then in your code you would reference:

color\_palette.palette\_1

to use that palette. I don’t know how you intend to use this, but most Corona SDK color settings takes an array (table indexed numerically) of numbers. You’re sort of defining key-value pairs, though it probably will work, but you’re making the values strings not numbers. Perhaps you will find that:

M.palette\_1 = { 0, 0, 1 }

will work a little better for you.

Rob

Thank you for the help, it works perfectly!

local color\_palettes = require( "scene.function.themes" ) package.loaded["scene.function.themes"]

I don’t think you need to do the package.loaded() statement.

Modules have a defined format. First you create a table and you add your module objects to the table and return the table. Something like this:

local M = {} M.palette\_1 = { [1] = "0", [2] = "0", [3] = "1", } M.palette\_2 = { [1] = "1", [2] = "1", [3] = "1", } return M

Then in your code you would reference:

color\_palette.palette\_1

to use that palette. I don’t know how you intend to use this, but most Corona SDK color settings takes an array (table indexed numerically) of numbers. You’re sort of defining key-value pairs, though it probably will work, but you’re making the values strings not numbers. Perhaps you will find that:

M.palette\_1 = { 0, 0, 1 }

will work a little better for you.

Rob

Thank you for the help, it works perfectly!