Shadowblitz16
Shadowblitz16

Reputation: 145

using a lua file to require other lua files?

can someone tell me if its possible in lua to do something like this? where it uses a module file to include other module files using a single lua header?

--main.lua
require "std"
local test = WIDGETS[0]

--std.lua
require "std.constants" -- this is the problem its local to this file only
require "std.functions" -- this is the problem its local to this file only

-std.constants.lua
WIDGETS = 
{
   NONE,
   PANEL,
   BUTTON
}

I need to do something like this so I don't have to type std.constants.WIDGET[whatever]

Upvotes: 2

Views: 518

Answers (1)

Paul Belanger
Paul Belanger

Reputation: 2434

You could add the line local WIDGETS = std.constants.WIDGETS after your require "std". Then, all functions in that file can reference WIDGETS without polluting the global namespace:

-- main.lua
require "std"
local WIDGETS = std.constants.WIDGETS

local test = WIDGETS[0]
...

You would only have to do this once per file.

Upvotes: 1

Related Questions