Sleitnick / AeroGameFramework

AeroGameFramework is a Roblox game framework that makes development easy and fun. The framework is designed to simplify the communication between modules and seamlessly bridge the gap between the server and client.
https://sleitnick.github.io/AeroGameFramework/
MIT License
216 stars 56 forks source link

[Enchantment] Lazy load addition to better organize modules #52

Closed ghost closed 6 years ago

ghost commented 6 years ago

Here is a small edit that I use.

-- Setup table to load modules on demand:
function LazyLoadSetup(tbl, folder)
    --Simplify hierarchy of modules
    local modules = {}
    for _, object in ipairs(folder:GetDescendants())do
        if object:IsA("ModuleScript")then
            modules[object.Name] = object
        end
    end

    setmetatable(tbl, {
        __index = function(t, i)
            local obj = require(modules[i])
            if (type(obj) == "table") then
                Aero:WrapModule(obj)
            end
            rawset(t, i, obj)
            return obj
        end;
    })
end

This allows me to better organize the hierarchy and still use the modules like before. image

function UI:Init()
    TimerModule = self.Modules.Timer
    TweenModule = self.Modules.Tween
end
Sleitnick commented 6 years ago

I actually have plans for a similar feature. It is designed to be somewhat similar, but preserve the dot-notation of the folder. I'm going to close this issue, but your idea is not lost!