#OOP question
1 messages · Page 1 of 1 (latest)
No.
Oops
ye u put it inside of a table that has all objects and return it so you can access it
Hey
Ur a scripter?
gee i wonder
Yeah it's called a module script
they're very useful
essentially you can require the module script from different scripts and the information in the module script will stay consistent across all the other scripts
you can do method calls just like normal functions in modulescripts without passing down arguments?
wdym
when im using something like car:Stop() for example
idk how advanced you are but module scripts are basically just tables
well a module script would represent a class not an object
are you just talking about accessing the same object from different scripts?
if im using a modulescript i need to do something like module.StopCar(the arguments here) which i need to pass the arguments and stuff
yes
like calling Car:Forward() in different scripts, the car is same for other scripts
there's a crap ton of ways to do that
really?
** You are now Level 1! **
the laziest way is to use _G or shared but that's considered a bad practice. Both a just tables that are shared across all scripts
the recommended way is to just create a module script to store your shared variables
like
--module script
local module = {}
module.car = nil
return module
--random script
local module = require(path.to.module)
module.car = car.new()
--other script
module.car:Drive()
i was thinking i could just put like a pointer variable (like object_id) inside whatever object im working on, and when i do method calls on it i can just work with whatever table im working with (in this example Car) like Cars[object_id] instead of doing self but that seems a bit complicated for a simple thing
yeah but
when you make car.new(), all the variables (like speed, carModel) are stored inside the car variable, now if i do the same on other scripts in the same game variables inside car like speed aren't shared with my other scripts
i need a way to access 1 object, but on different scripts
well yeah obv that's why you only call car.new() on one script
when you set module.car = car.new()
that stores a reference to the object created by car.new() inside module.car
oh you're using modulescript to store the object
though it would be problematic for multiple objects unless i do some object id stuff inside that module table
just store them in a list or a dictionary then
like a few functions inside modulescript like getObjectIdByModel() or other properties
alr thanks for the help
you're trying to link models and code objects together right?
yeah like having an object for a specific model if im using it for that
like enemy or player
in that case I'd have a dictionary where the keys are the models and the objects are the values
so you can do
module.cars[model] -> car object
and if you need to reverse it (get the model from the car object) just add a field in the car object where you store the model (like car.model or smth)
yeah that seems like a good solution
** You are now Level 2! **