I have a type defined in a projectile ModuleScript, let’s call this Module A:
export type HitInfo = {...}
Now, I need to use that type in another module, Module B, an effect that can respond to a projectile hitting something:
function GenericProjectileEffect:Hit( Info: ModuleA.HitInfo ) ... end
The only problem is, Module A (projectile) needs to require Module B (effect) so that it can spawn an instance of the class, and this function (with a Luau type) needs to be defined right away. Having Module B require Module A to gain access to the type would result in a cyclic requirement, which is a no-go. Is there any way I can do this?
This is bad not only because it can’t resolve in Roblox, but because from an organizational standpoint it doesn’t make much sense to have co-dependent modules. This isn’t exclusive to Roblox.
They should probably be refactored into a single module to resolve the dependency issue and to fix the organization.
They can’t be one module, one is a class that is created on the server and client that manages projectile physics and one is a class (extends an Effect class) that is created only on the client and handles how the projectile is rendered.
A good example is that I have one class that extends Projectile, called Bullet. This Bullet class is intended to simulate a bullet fired from a gun, which can visually have any effect really, but the physical behavior is still that of a bullet; it will ricochet off surfaces and create metal sparks if it hits metal.
You can take whatever code they both need and put it in a shared third module, or you can take one of the modules and generalize it enough to not require the other module directly. Or any functions of ModuleA can be rewritten to take ModuleB or an Instance thereof as a function parameter, thus removing the compile time dependency. This may not work with strict typing though.