Dependency Service - Easy dependency referencing

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
image

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

image

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
image

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
image

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


Warning

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.
image


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

5 Likes

At this point We should just rather decide which one really need a module itself.
Instead of for the sake of supposedly “clean code” that you mess up your code like that.

Example why would anyone want to make it so bureaucratic to the point where checking nil must have a module of it own? :thinking:


Programming is just language, so codes are sentence, why would you overcomplicate your function by calling a piece of code with different name? Like calling the sentence “Hit the ball then run to the toilet” to just “action 1()” :man_shrugging:

It’s shorter of course but is it really clearer by calling ball and toilet to just action 1, clean code should change to clear code because it’s entire priciple is more oriented to be clear rather than clean

4 Likes

Lets say I had two ways for checking if something is nil

local isNil = isNil(character)

local isNil = character == nil and character.Parent == nil

Pretend one day it turns out this isn’t enough to check for an instance being nil

If my check for nil code is wrapped by a module
All I need to do is update the module rather than update every line of code that checks for nil

I believe code should be designed with reusability in mind.

Clean code makes it readable which makes it clear >:)

1 Like

Will you every update your check nil module? :man_shrugging: :thinking:
Really, like ever?

Readable doesn’t mean it’s a short sentence
Let say you have a sentence with bunch of professional word that you don’t even know what it’s mean like just read some Wikipedia article there’s plenty
Module is like professional word which is indeed short but not readable :hugs:

Yes indeed upgrading your dictionary is easier than scanning your books and edit every sentences but have you ever consider that the action it represent will ever change? Like at all? :exploding_head:

4 Likes