Working w/ ModuleScripts?

I’m curious as to if there’s a way to utilise Module Scripts to register an overall instance rather than passing it individually each time?

A brief example of this being my Starship system - It’s loaded using a Main Script that sets:

local Exterior = script.Parent:WaitForChild("Exterior")

But to pass it to a module that, for example, controls flight:

require("Flight").Enable(Exterior, argument2, argument3)

System.Flight = function(Exterior, argument2, argument3)
end

Ofc there’s more arguments to pass along - Such as BodyMovers, Values, Interior etc. That are all set up in the Main Script prior to the handling of Modules.

I’ve tried working with Dictionaries to transfer arguments, but even that gets tedious.

Transfer = {Exterior = Exterior, Interior = Interior, BodyMovers = BodyMovers}

Is there any other method I can utilise?

Idea

I’ve had the idea of setting up a single Module that will handle whatever’s called - Instead of transferring Arguments constantly, it’ll do it the once and place the information into a ModuleScript.
I’ll then require said ModuleScript when needed and use the arguments to locate what is needed; i.e. a Value, BodyMover, Exterior or Interior and then return what is needed.
What are your thoughts on this?

Note

  • Unsure _G would work in this instance as there will be multiple ships in-game using the same system.
  • Same with ‘Shared’ - Due to multiple ships being present in-game, there is the possibility of the systems colliding?

Correct me if i’m wrong but by saying “register an overall instance” are you insinuating that you want to be able to just store that “registered instance” and just call functions and events from there kinda like you do with parts and other things how they have properties and functions themselves.

If i’m not mistaken what you want to do from what I understand would be quite easily to use object oriented programming which allows you to create your own instances! You can add functions and parameters / properties to your object just like with any lua built in object.

1 Like

I believe I follow?

Basically, what I’m currently doing is everytime a new function is called it’s a matter of:

example1 = function(arg1, arg2, arg3)
-- Code.
end

example1(arg1, arg2, arg3)

I want to minimise the arguments sent so that I can keep some for of order rather than constantly having:

example1(arg1, arg2, arg3)
example2(arg1, arg2, arg3)
example3(arg1, arg2, arg3)

What I would suggest is changing this so it follows an object-oriented structure.

Have a module that takes care of this. Obviously you need a constructor. Think about which arguments will always remain the same each time you call a function (I assume they are the Exterior and Interior, or the ship model as a whole), those will be passed to the constructor. Add the appropriate methods, such as Enable in your example.

The idea is, instead of having to call a function and pass what ship the function will affect, you create an object that keeps a reference to the ship, and calling methods on that object, that way the method knows by itself what ship the function will affect: it’s the ship referenced by the object the method we called is bound (this object is simply self).

Some hypothetical piece of code:

local shipWrapper = require(game.ServerStorage.shipWrapper)

local ship1 = shipWrapper.new(Exterior, Interior) --Exterior and Interior belonging to the ship in question
local ship2 = shipWrapper.bew(Exterior2, Interior2)

ship1:Enable(SomeBodyMovers) --you don't need to pass exterior and interior
ship2:Enable(someBodyMovers2) --the magic happens inside

If you’re not used to or never heard of object oriented style of programming, I suggest this topic.

1 Like

Using logic like this - If I were to store this on the Server upon initial setup of the ship - When gathering the required stuff, suches as body overs or values on the server side for piloting, how could I go about that as the table will be empty?

I’m not sure I follow. What table are you mentioning? If you mean how would I call the method when I don’t have the necessary information needed, well simply wait until the information is aqcuired, and call them.