#OOP question

1 messages · Page 1 of 1 (latest)

pastel niche
#

in oop, is there a way for like 2 or more scripts to call methods on the same object (both scripts access the same object, all variables inside the object/table are shared between scripts that called the constructor in a way)

grim basin
#

No.

blissful coral
#

Oops

charred grotto
rancid verge
#

gee i wonder

safe geode
#

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

pastel niche
#

you can do method calls just like normal functions in modulescripts without passing down arguments?

pastel niche
#

when im using something like car:Stop() for example

safe geode
#

idk how advanced you are but module scripts are basically just tables

safe geode
#

are you just talking about accessing the same object from different scripts?

pastel niche
#

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

pastel niche
#

like calling Car:Forward() in different scripts, the car is same for other scripts

safe geode
pastel niche
#

really?

austere fulcrumBOT
#

studio** You are now Level 1! **studio

safe geode
#

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()
pastel niche
#

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

pastel niche
#

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

safe geode
#

when you set module.car = car.new()

#

that stores a reference to the object created by car.new() inside module.car

pastel niche
#

oh you're using modulescript to store the object

safe geode
#

yeah

#

that was the entire point of the code snipet 😭

pastel niche
#

though it would be problematic for multiple objects unless i do some object id stuff inside that module table

safe geode
pastel niche
#

like a few functions inside modulescript like getObjectIdByModel() or other properties

#

alr thanks for the help

safe geode
pastel niche
#

yeah like having an object for a specific model if im using it for that

#

like enemy or player

safe geode
#

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)

pastel niche
#

yeah that seems like a good solution

austere fulcrumBOT
#

studio** You are now Level 2! **studio