A way to use metatables to prevent Cylic module Dependency issues?

So what I am thinking is there is module A, B and one that is a middle man. Both modules are defined in a table inside of the middle man (a module script too) and is setting a metatable with the required module tables. Hopefully this makes sense. If there is a better way to do what I am thinking then lmk!

6 Likes

#help-and-feedback:scripting-support

What do you need this for? It’s hard to tell what you’re trying to do.

1 Like

I have a multitude of module scripts that need to send data between each other. I prefer to keep actual scripts them selfs to have a reduced amount of code and to only handle incoming data from a client etc if that make sense. It helps with long-term maintenance at least in my opinion. It also helps me with keeping things clean.

1 Like

Yes, this is what I use too. It’s a modular system! All you need is a main module which can require them all. All those modules would use the main module to get the information from other modules.

-- Module A
local ModuleB = Importer:Get("ModuleA")
print(2)
return {}
-- Module B
task.wait(2)
print(1)
return {}
-- Output:
1
2

Importer:Get() would yield until that module has finished loading. This will help stop cyclic module issues. But if two modules import each other, and that’s required to finish loading, it will wait forever. You could fix that by importing it later, or importing it in a task.spawn as long as it isn’t needed immediately.


Please move this to #help-and-feedback:scripting-support by editing your topic!

2 Likes

So what you’re saying is that as long as I do a task.spawn require function I can make it avoid such issue and have it still require just as long as it happens later on?

1 Like

I suppose you can do just that!

1 Like

Alright Ill give it a try. At the moment I am experimenting with a middle man type module. I just need it to not give this issue as I have a table of afk players and there is a PlayerService module that gets the AFK table from GameService to remove the player from it if they leave.

1 Like

A ‘middleman module’ is what I made and used for my modular system. It requires all modules in the system and indirectly gives them access to the importer. Therefore, they can all reliably import the other modules in the system. I was inspired to make that after making something similar for organizing code used to run a discord bot.

1 Like

Do you have the loader code for the modules so it just saves me time and a headache? I am just messing about a the moment to see how I can do it and having been failing a bit atm.

I don’t really want to give it away right now! There’s other modular systems out there in resources that you could use though.

All good. If you can, could you point me towards those?

This seems different, but may work:

Have not used anybody elses system, so I’m not sure what’s good or not.

1 Like

I could work with this maybe. Ill have a look at it. I did look at it in the past but it was slightly confusing to use although I will give it a try again soon if all else fails. It would cause me to do some big reworks but shouldn’t be too bad.

1 Like