tags:

views:

79

answers:

1

Say I want to make a module for say a set of GUI controls, how would I create a module that would load all of the GUI scripts, and should I put those scripts as modules themselves? I was thinking of having a system like this:

module("bgui", package.seeall)

dofile("modules/bgui/control.lua")
dofile("modules/bgui/container.lua")
dofile("modules/bgui/screenmanager.lua")
dofile("modules/bgui/form.lua")
dofile("modules/bgui/button.lua")
dofile("modules/bgui/textbox.lua")
dofile("modules/bgui/label.lua")

Would all the files run then have the variables they set as part of the bgui module? Aka if in control.lua I had control = {...} would it be defined as bgui.control or should I make the control.lua a module itself, something like module("bgui.control") would that work as I intend?

Sorry if this isn't very clear had to write it in a rush, thanks :)

+4  A: 

You are actually asking two questions here.

The first one is "is this way of loading lots of files on a module ok?"

The answer is - yes. It is kind of an unspoken standard to call that file mymodule/init.lua. Most people have ?/init.lua included on their path, so you can just write require('modules/bgui') and init.lua will be loaded automatically.

This said, you might want to remove some code duplication by using a temp table and a loop:

# modules/bgui/init.lua
local files = {
  'control', 'container', 'screenmanager', 'form', 'button', 'textbox', 'label'
}
for _,file in ipairs(files) do dofile("modules/bgui/" .. file .. ".lua") end

The second question is "are objects defined on one file available on bgui?". The answer is also yes, as long as the file defining the variable is "done" (with dofile or require) before the file using the variable.

egarcia
Thanks so much, I couldn't seem to find much documentation on lua modules and this helps a tonne.
Blam