Is it OK to require modules "on the fly"

I am creating a system of super powers for the players and there are over 50 of them, each power is its own module inside a folder. The require list was getting very long at the top of the script that calls these powers so I thought about just requiring the module when the player actually uses the power.

The question I have is: Is this a problem? the player will be using these power many times during a play session, and this code will require it each time, is this a problem? A player could be doign require hundred of times each time they play, would this cause a memory leak?

function module:PlayPowerEffects(sentPlayer,sentPower,sentAbility,toggle)

	local powerModule = script.PowerEffects:FindFirstChild(sentPower)
	
	if powerModule then
		local requiredModule = require(powerModule)
		requiredModule:FireAbility(sentPlayer,sentAbility,toggle) -- fire the server side of the power
	else
		print("ERROR: assigned power doesnt exist")
	end
	
end

As a note, I tried to require all the moduels inside the fodler and require them in a for loop, putting them into a table. But for some reason after doign that I wasnt able to fire and of the methods inside each one, so this was my attempt to simplify things.

this is what my attempt looked like

local powerEffects = {}

for i,v in pairs(script.PowerEffects:GetChildren()) do
	powerEffects[v.Name] = require(v)
end

then elsewhere in the script i would try to do this:

powerEffects[sentPower]:FireAbility()

but this would tell me the method didn’t exist, when I’m sure it did

Thanks in advance!

1 Like

I do know that modules cache when required. So requiring 1 module 100 times should in theory be safe memory wise, but that’s in theory.

I would probably try to stay away from requiring a bunch of modules each frame just because you can probably require all of your modules at once perhaps on load or on run.

2 Likes

Yah modules cache, the next time you require the module it just returns the reference of the table returned from the module.

2 Likes

so does this mean its safe to require a module hundreds of times during a single play session?

1 Like

Yah it’s safe to require the same module 100 times or 10000 times, it doesn’t matter.

2 Likes

Yes, in fact the popular framework Aero Game Framework does something similar called ‘lazy loading’ which essentially only requires the module when it is first indexed anywhere. This can have numerous benefits in terms of performance for your game as you’ll only be requiring the module if it is actually being used by something, therefore until it has been indexed nothing has been loaded into memory from that module. It is a good idea to only require what you’re actually using.

As others have said, when a module is required it is cached therefore requiring it once as opposed to many times has no difference on performance.

3 Likes

Thanks for the help, each of you provided the answer. I had to pick who to mark as SOLUTION so I chose the most detailed but you are all appreciated!

THANKS!