Question regarding "require"

@d.mach,

FYI, SSK is an example of a ‘super module’.  That is, it requires several dozen modules once and accumulates them into a single semi-deep table/module hierarchy that is then exposed as a global.

The module (when fully loaded; and you can control this to reduce it) is about 500KB of memory.

This is NOT efficient in some regards, but from an accessibility and usability viewpoint it greatly reduces the complexity of collecting the varied functionality in SSK.

I then alleviate some of the ‘lookup costs’ associated with putting all this functionality in a single deep hierarchy by localizing functions/and features I use most.

For example, if I’m using the math2d sub-module in a game file, I put this at the top:

-- -- SSK 2D Math Library local addVec = ssk.math2d.add;local subVec = ssk.math2d.sub;local diffVec = ssk.math2d.diff local lenVec = ssk.math2d.length;local len2Vec = ssk.math2d.length2; local normVec = ssk.math2d.normalize;local vector2Angle = ssk.math2d.vector2Angle local angle2Vector = ssk.math2d.angle2Vector;local scaleVec = ssk.math2d.scale

Now, instead of this:

local vec = ssk.math2d.angle2Vector( 45 )

I can type this:

local vec = angle2Vector( 45 )

Not only is this easier to type, but by localizing the function I’ve reduced the lookup time to use it repeatedly.

 

dave, your approach is nice but will load ALL random files at the same time. You just call a random method after, but all are in memory.

 

I don’t think its what d.mach wants since in his code he only loads one at a time.

hes method will work fine…but i think he misses a “return theFile” to work properly and if he wants to “unload” the require file, just use package.loaded[“random1”] = nil

I know, that was my point:  unless the OP truly has “very very huge” files, then the topic isn’t even worth worrying about - just require all the files at startup, use any one at random during runtime, and just be done with it.

Otherwise you’re going to an awful lot of trouble (and incurring run-time load penalties as well), all for a not-actually-demonstrated assumed need to reduce code size.  The OP code, if run repeatedly on each scene change (as OP stated it would be) would have eventually loaded all modules ANYWAY as written.

Thanks for all your help and feedback. I guess it is not huge enough to get through all the trouble.

I also wanted to improve performance regarding loading time of scenes… but it seems it is something else which is making this loooooong.

@davebollinger, following your thinking, i would go for 1 file rather than 3 files since they are little.

and calling just the function you need inside that file.

local random\_number=require ("random") -- one file only local number=random\_number["random"..math.random(3)]() -- substitute the number with the number of functions you have

in this example the functions inside random.lua should be something like this:

local m={} function m.random1() local random\_number=math.random(10) return random\_number end function m.random2() local random\_number=math.random(100)+30 return random\_number end function m.random3() local random\_number=math.random(30)+50 return random\_number end return m

saying this…with only 1 file and only 1 function I could accomplish the same thing i did with 3 functions. Using parameters to that function. I don’t know whats inside “randomfile” and “randomfile_2” are so i can’t  conclude that will work in d.mach case.