Roblox Studio - Writing Your Own Module Loader

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to another video today we're going to be doing a medium to advanced level topic we're going to be discussing how to create your own module loader this is big for creating your own framework and helping to define a scripting style that you'll use repeatedly and this is helpful when you start new projects and as you want to rapidly create new ideas you can use the same fundamental uh underbelly to rapidly create new ideas so if we look over in studio I have a little uh structure already set up I have the server script and uh this tutorial assumes that you already have a basic understanding of module scripts so here in the server script we have a quick require for the module loader and then it edits the children of the script and it starts it uh pretty simple and the client is the same exact thing and then under this we have a test module with the start method and we also have a client module with a start method very simple the idea is that with our loader what we're going to want to uh the goal is that as we add more modules they're going to automatically be loaded and this start method is going to be ran so we can quickly expand and add more to our game and put it all under this shell to all work together so we're going to go to our modu load here and I've already wrote a couple lines of code but this is just an empty shell and we're going to fill it in together module loader is the name of our module and uh cache modules is what we're going to use to store all the loaded modules that our server and client will load with the init method get is going to return a module that's been cached init will add all the modules from our scripts here uh to the cache modules and then start is going to call that start method on all of the cache modules an additional line I have down here is adding the get function to the shared so we don't have to require the replicated storage every time we want to access this module Lo so we're going to start simply by returning cache module's name that's a simple one uh that just returns a module that we already have loaded and then now we're going to start by uh initializing a list of loaded modules so we're going to say okay it's that simple all we do is iterate over our module scripts and we add the name of the module script as the key in our cach modules and the loaded module as the value so this will create a table of loaded module scripts which we can access from other module scripts and our start method there we go and we've created our module letter it's that simple and with the code I've already outlined what we should see is that test module. start will print started test module if we just run this game as a server as we can see down here in the corner started test module so our module loader is already up and running and it's that simple and let's say we had a second module here our first module has some method that we want to use our print method so from our second module we can Define get a share. getet and we can say test module and this should get renamed to Second module so what I've quickly set up here is that the second module will print started test module 2 and then it will call the print method of the test module that we just defined using the get method that we've defined under share and as we can see down in the output and I'll move this down here so we can see it better we can see that we've started test module started test module 2 and from test module we're saying hello from test from the second module this is a very simple and very basic way to create an interconnected system that you can quickly add and rapidly expand and you won't have to write too much additional code for how to integrate your new modules into your pre-existing system and so as we create many many more modules they're automatically ran and they're automatically loaded one caveat though from this design is that you can only call other modules after they've all been initialized because as we Loop through the scripts that we pass into our module loader test module second module say that test module has a call for second module uh just out here you know get second module if we have a call for second module in test module but second module is not loaded we're not going to get second module and second module we can't perform any operations with that so the point of this is that it comes in two stages we have an init stage which uh loads all our modules into a cached module then our start method which initi runs this start function on all the modules and once we've run that start function we also know that all the modules have been loaded and they're safe to use something else that I like to do additionally is maybe add a folder for utilities that's a way that we could expand this and we could add a module script under here for you know uh let's say it's a model utility or other utilities like that that we might use frequently in other projects we can put it all under our loader as a utility and we could add a method in our loader to get that utility further expansion as well perhaps we want to make sure that we can always have our loader in all our projects something we can do is turn it into a package if we click this convert to package button we can call it module loader and I'll set the ownership to myself this module loader will now have this package icon it's very small to see but it's under there and this means that it is an asset that is stored on the cloud and as you add things to it let's say we add see now here it's saying that we need to publish our changes if we want to edit this package so let's say we add a number utility of some sort uh this little uh exclamation Point indicates that we made a change to our package so we're going to publish our changes to our package now if we open up a new place we can go into our toolbox go to my packages and we can insert our module loader here and this module loader will be up to date with all of the other packages this is important because as you maintain an update your module loader you're going to be adding more things to it so we want to make sure that we always have the most recent version and that we're always working with the same concept we don't want to rewrite the same thing every time so beyond that what else could we add to our module loader well perhaps we could add a component system which takes tagged items and instantiates them as classes we could add um we could add a testing utility we could add a test subfolder we could add logging for printing messages to the console we could add utilities for uh you know uh maybe you have an admin tool set that you've created you could put that under your module loader as a utility and package it all together so that you have access to it in all your other games and that's how you create your own module loader uh if you enjoyed this tutorial leave a thumbs up leave any comments down below if you want me to talk about anything else or if you had any comments or questions on this video thanks guys have a good day
Info
Channel: qweekertom
Views: 8,736
Rating: undefined out of 5
Keywords: roblox studio, module loader, roblox, studio, developer, scripting, coding, module script, write code, design, discuss, solutions
Id: uJUhrCO11rk
Channel Id: undefined
Length: 8min 49sec (529 seconds)
Published: Sat Feb 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.