Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua - Question on modules

Tags:

module

lua

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 :)

like image 301
Blam Avatar asked Mar 26 '26 13:03

Blam


1 Answers

You are actually asking two questions here:

First, "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

Second, "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.

like image 97
kikito Avatar answered Mar 28 '26 10:03

kikito