Module Circular Dependency

I’m creating a game heavily inspired by minecraft, and the following scripts are direct features from minecraft.

I currently have an issue with module circular dependency. I currently have 3 module scripts, inventory, dropped, and pickup.

image

Inventory → Dropped → Pickup → Inventory again

Inventory handles the inventory system and can also drop an item (think of selecting an item then right clicking outside your inventory in minecraft)

Dropped handles registering an item and animating it

Pickup is in charge of determining the distance of the player from each dropped object, if they’re close then they’ll pick it up (by adding to inventory)

I’m not experienced at module script hierarchy, are there any recommendations for designing this system, and should I adopt a certain hierarchy style I’m not aware of?

Just make sure the definition of one class isn’t tied to the definition of the one it is defining. You need to have some sort of solid ground.

Usually, this is from types and if this is the case you can merely put your type into the start of the circular dependency.

Keep in mind that your code should work fine even with this and it isn’t a must to fix, but is recommended

1 Like

Currently, none of these scripts are classes. They consist of a initialization function (called from the local script) that activate input events. The rest of the module consists of a bunch of functions. Not sure if this is a good way to structure the code or if there is another standard practice.

Maybe this is worth trying out?

image

2 Likes

Ok, just keep in mind that you don’t have to change anything.

you should not depend on another module that depends on the same module

module a needs b and b needs a is a bad way to design it. I’m certain you probably figured this out by now, but a band aid solution is to require it on a start function where its safe to call eachother

local ModuleA = nil

module.init = function()
end

module.start = function() -- // this is a safe space for circular dependencys
ModuleA = require(pathtomodulea)
end