I am trying to use the commun object orientation, I know lua it's a little different from others programming languages, but I can't find how to use diferents files to be able to sort my code, something like this:
function dog.new () dog = {} dog.name = "the name of the dog" return dog end
and from another file I can call this fuction to get new dog
dog1 = game.ServerScriptService.Dog.new() dog2 = game.ServerScriptService.Dog.new()
it's very simple, I trying to get the normal and simple way to import files to other files to inherit and keep an organized and recycled code.
Any help will be appreciated.
Well, to put it simple, there're two ways you could go about this: you could use a module script, and have the main(?) script call on it, while the module script returns the set data, or you could use _G, which I highly recommend against using, as it shares across any serverside script, and does not work w/ localscripts.
This is how a module script would be set up, and how the main(?) script would call upon it:
-- Module return { 'Data' } -- Main Script local returnTableFromModule = require(MODULE) print(returnTableFromModule[1]) -- Data
To add, and note from before, the require function will call upon the module script, but also yield the code until the module script returns the code/ exists w/in the specific parent.
Now, onto _G; however, before I begin, I recommend NOT using this, as it has loopholes & security vulnerabilities, i.e. any serverside could retrieve it, localscripts can not communicate w/ it, and any serverside script could edit/ alter the data (at will), so if an exploiter wanted to edit the data at any time, it's free for the taking to edit/ alter.
This is how it would be set up, incase you're curious:
-- Script 1 _G.Data = { 'Data' } -- Script 2 print(_G.Data[1]) -- Data _G.Data = { 'Altered Data' } -- Script 3 print(_G.Data[1]) -- Altered Data
More information about what was mentioned
_G - I recommend AGAINST using this.
Module Scripts - I highly recommend this over _G, as it has better security, and is safer for script to communicate through, + I believe localscripts can access this (however, I can not confirm this, so if you plan on doing this, proceed w/ care).
Stuff touched on, but didn't go into great detail about
Hope this helped you in any way! :D
Consider reading this lua tutorial on inheritance. They show you some good patterns and even have a class creator function which you might like to use.
Be warned, your script has two problems:
Instead of using the link above, you might want to see how Roblox does this with the chat system - simply run Roblox Studio and look in the Explorer window for the 'Chat' content. Look for a "ClassCreator" script and look at how other ModuleScripts use it to create their own classes.
Here's a sample dog class, with usage:
--Dog ModuleScript local Dog = {__index=Dog} function Dog.new(name) local dog = {} --assign variables here if desired dog.name = name return setmetatable(dog, Dog) end function Dog:Bark() print(self.name .. " barks!") --self exists because of the ':' syntax on the previous line end return Dog --Main script local Dog = require(game.ServerScriptService.Dog) local dog1 = Dog.new("Dog1") local dog2 = Dog.new("Dog2") dog1:Bark() --In lua, this is identical to saying "dog1.Bark(dog1)" dog2:Bark() --You can technically also use Dog.Bark(dog1), but you shouldn't do this unless you know for a certain that 'dog1' is a Dog and not some child class (ex a 'Terrier'). Since you generally don't know what class an object is without testing for it (which is a questionable thing to do in most cases), you should avoid doing it this way
This is the same style used in the link at the top.
Minor note: In some languages it is recommended to put every class in its own file. I've found it much more pleasant to put several related classes in the same script (treating ModuleScripts as a "package" or mini-library), but this is completely up to you. Roblox's chat system has a different ModuleScript for every class. If you want to use the "multiple classes in one file" system, how you require things is naturally a bit different:
--Animals ModuleScript local Animals = {} --Define Dog class here Animals.Dog = Dog --Define Cat class here Animals.Cat = Cat --etc return Animals --Main script local Animals = require(game.ServerScriptService.Animals) local myDog = Animals.Dog.new() --otherwise the same