Hello, I’ve recently come across a new way to use functions, and I’m wondering what’s the difference.
--New Way
_Functions = {}
_Functions.Test = function()
print("Hello World")
end
--Old Way
local function Test()
print("Hello World")
end
_Functions.Test()
Test()
I would like to know if the new way changes anything? They both work the same way, so I’m not sure if one is better than the other.
There’s no backend difference; they behave the same way. The only difference would be the way you access the function since it’s a property in a table as opposed to being it’s own thing
-- You can also do this
local Test = function()
end
Also, it’s not a “new way” it’s been there since day one
Alright, I didn’t know that, I just now discovered it and I couldn’t find any posts about it, that’s why I thought it was “new”. And I didn’t know if it affected performance or how a script runs.
You are defining a function inside a global table. It changes absolutely nothing, so don’t worry. You just created a function inside a table, and called it from that table, nothing else.
Others have already answered, so my advice is: you should learn the Object-Oriented Programming.
local Handle = {}
Handle.Value = 0
function Handle:IncreaseValue()
self.Value += 1
end
You can simulate class with metatable.
--< Variables
local Person = {}
local PersonClass = {}
PersonClass.__index = PersonClass
--< Methods
function PersonClass:GetName()
return self.Name
end
--< Constructor
function Person.new(name, age)
local self = setmetatable({}, PersonClass)
self.Name = name
self.Age = age
return self
end
--< Initailize
local jackObj = Person.new("Jack", 36)
print(jackObj:GetName())
Object-Oriented Programming
is very useful and can save a lot of time in big project. I therefore encourage you to do research on this subject or read the articles by clicking on the words in blue.