Dependency Service: For easy dependency referencing!
Link: DependencyService - Roblox
The Aim of This
- A way to reference instances that scripts require, regardless of where they currently are.
- Allows for easier organization, I find myself reorganizing assets from time to time.
- Having a game composed of multiple modules rather than a few or a single script
Introduction
I am going to on a little tangent, but stick with me. It will all make sense.
Do you hate scrolling? Well, I do!
Why waste time scrolling through thousands upon thousands of lines? I know a few people who can’t even scroll, as they don’t even have a scroll wheel!
But anyway, because of scrolling, one function per module script is more than enough for me.
average module in my codebase
However, I’ve noticed how inconvenient it is to try and require any module scripts I need.
What if this one module required a lot of other modules?
slight exaggeration of what it could look like
What if I wanted to reorganize my modules? I can’t do a mass update to all my required modules! I would go insane from that!
I realized I needed a way for modules to reference each other easily. I needed a service that could manage what my scripts depended on!
About
Dependency Service is an easy way to reference modules and instances, allowing for easy organization! I feel like this has been done already, but I wasn’t able to find any.
example of its usage within my codebase
The whole idea behind this is to use the ObjectValue to point towards any dependency the script needs.
Setup
--You can set up the service like this
local DependencyService = require(ReplicatedStorage.DependencyService)
--I personally recommend you set it as a global variable
--DO IT AT THE START! I AIN'T RESPONSIBLE FOR YOUR SPAGHETTI CODE!
_G.DependencyService = require(ReplicatedStorage.DependencyService)
Usage
this is what it looks like when normally used
Step 1
Create an ObjectValue and place it in your script
Step 2
Set the Value of the ObjectValue to the (Module / Instance) you wish to use.
Make sure to rename your ObjectValue to the object it is pointing to
Step 3
In ModuleScript
--First get the Dependency Service
local DependencyService = _G.DependencyService
--Then pass in your script that has the ObjectValues
local dependencies = DependencyService:Get(script)
--get the dependency, it will automatically require itself
local otherModule = dependencies.OtherModule
you can pass in any Instance in the :Get() method, but I recommend using the script for easy organization
Settings
the settings are attributes on the DependencyService, I recommend keeping them on
AutoRequire: Automatically require any module dependency
ClearReferences: Automatically destroy the ObjectValues after use on the Client
CreateDirectory: Folders dependencies become tables that can act as the folder.
Can also AutoRequire modules
NameSensitive: ObjectValues require the same name as dependency
Warnings
Make sure all your assets are loaded first!
You can do this by loading the game first: DataModel - IsLoaded
--Replicated First
if not game:IsLoaded() then
game.Loaded:Wait()
end
Avoid Recursive Requiring
This happens if a module that while requiring another module loops back to requiring itself.
Additional Notes
-
The game will probably take up more memory from this, but for me, that is alright. As the benefits of organizing my codebase and assets far outweigh its downsides. But if you require your game to be heavily optimized, this might not be for you!
-
You could honestly just make your own, but I posted this to show another option when developing on Roblox.
-
Because this uses ObjectValues, you can use the Filter Workspace Search Bar to easily search for what depends on what