#include functionality for Lua

Started by
2 comments, last by Vorpy 17 years, 1 month ago
I'm trying to adapt our current scripting system to allow for "includes" of common scripts so they may be reused. Currently, we load all scripts on a per level basis, and each level has its individual scripts plus one common file. What we want to do is have more then one common file, but as opposed to loading them all for all levels, we want the ability to pick and choose which common scripts load. We've had two ideas so far. One is to have a table inside our main level script that identifies all the resources we need... however, the problem here lies in first that not all levels have a level script, and additionally the load order is currently alphabetical, meaning our level script would load long after anything before an L. Another idea was to have a particular script just for declaring scripts to include. This would require we make sure the include script is ALWAYS loaded first, but this method would also add a new script file to every level. Does anyone know of any other methods, or if one of the two above would be the best way to do things?
Advertisement
I may be missing something but wouldn't 'dofile' (or better 'loadfile') work?
loadfile doesn't address our concerns very well as it doesn't automatically check if a script has already been loaded or not. I've had a friend suggest using Verify , but I haven't had much time to investigate modules. As of now, we only have stand alone scripts.
Just use Lua's modules system.

In your module named modulename.lua you start with a line like:
module(...)

and to load the module you use:
require(modulename)

modulename.somefunction(blahblahblah)

The modules work by doing stuff with tables. You could also write your own module system.

This topic is closed to new replies.

Advertisement