i ve seen this topic about the use of class but there are things about it that have been keeping me wondering
local function PersonConstructor(): Person
local newPerson: Person = {}
return newPerson
end
first i don’t understnad how actually does this work is the (: Person) just for reading purposes or is it actually an imporatant part for the script that is needed for it to work
2.
local function PersonConstructor(name: string, age: number): Person
local newPerson: Person = {}
newPerson.Name = name
newPerson.Age = age
function newPerson:Birthday(): number
self.Age += 1
return self.Age
end
return newPerson
end
this part of the script and the rest ( i think) can all be done using functions and tables in module scripts why would i need the classes here if i can do it with functions and tables without any complexity (other then adding and removing some part of the scripts)
3.
local function PersonConstructor(name: string, age: number): Person
local newPerson: Person = {}
newPerson.Name = name
newPerson.Age = age
function newPerson:Birthday(): number
self.Age += 1
return self.Age
end
function newPerson.Greets(self: Person, who: Person)
print(self.Name .. ' says Hello to ' .. who.Name)
end
local mt = {
__tostring = function (self)
return "This, is " .. self.Name
end,
}
setmetatable(newPerson, mt)
return newPerson
end
why would i need this metamethod __string if i can do it with a check like this
if type(argument) == “string” then
print(self.name)
end
tl;dr i need to see an actual script where classes could be better then normal scripts
This is just OOP without the __index metamethod approach (which you can find tons of tutorials by searching).
Declaring things with : is called type declaration specifically for Luau and is not required. It’s a learnable skill set that can assist you in development through real-time script analysis. You can read more on Luau here: https://luau.org/.
To answer the rest of your questions:
If you are referring to the constructor function, that is what they are doing. If you are referring to the type declaration with local newPerson: Person, that is for type checking purposes.
__tostring is a metamethod that is fired whenever tostring is called on your table. In this case, instead of tostring(person) returning the string table ..., it will return This, is <name>.
local person = PersonConstructor("John", 28)
print(person) -- This, is John
You don’t need to use classes but they are certainty helpful in streamlining the creation and implementation of things that are going to be used often because you don’t have to go and change a million things in a million scripts and can instead change a variable about the class. You also do not have to copy and paste code when making a new enemy for example, and you can just build off the base class you’ve already created. And I personally prefer using and reading classes. Once again you can achieve the same result a class user would while not using classes, its just a difference of approach and design really.
Also, the person and student examples I see often teaching people about oop aren’t good examples of when and where you’d actually want to use oop. Places where I would use oop are: making an enemy class (I can make a host of different enemies really easily), a treasure chest, and a player state and data handler.
You can just check #resources:community-resources most of the resources there are classes that are accessible for use (not necessarily easy) such as profile service, fast cast, simplepath.