Hi! I want to know how to make a default function for instances like part:Destroy()
or instance:FindFirstChild("SomethingElse")
. How do i do it? Thanks! (I know you read this please help me Iβve been working on it for a few weeks already)
Itβs used in Object-Oriented Programming (OOP). When you create a βobjectβ (commonly by using ModuleScripts
), you can assign it functions that use colons.
local myClass = {}
myClass.__index = myClass
function myClass.new(args)
local self = setmetatable({}, myClass)
self.pie = 3.14
self.otherProperties = args
return self
end
function myClass:IncrementPi()
self.pie += 3.14
end
return myClass
A noteworthy difference between using a colon vs. a dot operator when declaring the function is that functions with a colon will pass self
automatically.
local example = myClass.new()
print(example.pie) -- 3.14
example:IncrementPie()
print(example.pie) -- now it's 6.28
Short lesson.
aka pi and tau
two cool numbers
I donβt get all of it but the main reason I want it is to shorten some functions for instances like FFC, FFA, etc. Is there a way to do that?
If youβre new to OOP and donβt understand __index or setmetatable() just do:
Log = {
logger = function(self, v)
print(v)
end
}
Log:logger("hey")
Itβs documented here Programming in Lua : 16
I never heard of OOP and _index βββ βββ βββ βββ βββ βββ βββ βββ
Lucky for you, I made this resource! (Today, mind you)
Stuff like this isnβt supported by default in Roblox but I made it work with metatables like they were mentioning but made it as beginner friendly as I could.
-- Set up my custom module
-- You will need to put this require code above all of your scripts, but that's the only drawback for me
local ezWrapper = ... --> set the path here
ezWrapper:Setup();
-- Add your custom function
-- It adds a function 'test' to all objects that are in the script
ezWrapper:AddBaseFunction("test",function(object,state)
print(object,state)
return "read you, loud and clear"
end)
-- Now you can do what you want
local response = part:test(true) --> "read you, loud and clear"
Is this the kind of thing you were looking for?
Pretty similar, I was wanting to shorten the :FindFirstChild() function and some others by making another function that works the same but the name is FFC() to make it shorter. Is there anyway to do that?
t:f() is syntax sugar for t.f(self)
That means this
local t = {}
t.Name = "foo"
function t:sayHi()
print(self.Name)
end
is equivelent toβ¦
local t = {}
t.Name = "foo"
function t.sayHi(self)
print(self.Name)
end
Both work interchangeably, in fact, you can call every single Roblox method as such: workspace.Destroy(workspace)
(You really shouldnβt though, unless feeding builtin functions like pcall)
Something like this should work:
local ezWrapper = ... --> set the path here
ezWrapper:Setup();
ezWrapper:AddBaseFunction("FFC",function(object,name)
return object:FindFirstChild(name)
end)
print(part:FFC("hi"))
Please note, for all scripts that would use your :FFC method you will need to put that code at the top.
thanks! By the way what do i replace the
local ezWrapper = ...
with?
A reference to their ezWrapper module
ie: require(ReplicatedStorage.ezWrapper)
So, get the module from this: EzWrapper - Roblox
And then place it in somewhere like replicated storage and then you can do:
local replicatedStorage = game:GetService("ReplicatedStorage")
local ezWrapper = require(replicatedStorage:WaitForChild("EzWrapper"))
Please note, for all scripts that would use your :FFC method you will need to put that code at the top.
Ok thx! βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ βββ
If you want to shorten those the best way (in my opinion) would just be to create some regular functions.
Example:
local function ffc(instance, name)
return instance:FindFirstChild(name)
end
local Workspace = game:GetService("Workspace")
print(ffc(Workspace, "Terrain"))
(Iβd recommend not doing this and using the built-in functions to make your code more readable.)
Yep! Iβd forgotten where that function was stored. I had previously thought it was a static function of the Instance
class but couldnβt find it there. WorldModel
is a pretty good place for it. Thanks for letting me know
If you care about efficiency, using a function is the smartest way to do this. Wrapping objects is bad for performance. Number of arguments also doesnβt really matter. (Optimizing FindFirstChild also doesnβt really matter, itβs really not a performance bottle neck). Doing it this way is the same number of characters
Workspace:ffc("Terrain") -- 24
compared to:
ffc(Workspace,"Terrain") -- 24
not to mention more efficient. In the end though, you should probably go for readability. If you have problems typing Iβd highly recommend taking a typing course online, theyβre pretty helpful.
In this case, you could just do:
local ffc = game.FindFirstChild
local terrain = ffc(workspace, "Terrain")
Dude ofc ik that i can also just make it a function but itβs not using colon it uses arguments. Anyone can make that and I donβt even need to make a post on it. But what I need is using a colon to reduce the amount of arguments required. Itβs always about efficiency.
Read these informative resources -
What is self and how can I use it? - Help and Feedback / Scripting Support - DevForum | Roblox
Programming in Lua : 16
Both are gonna be helpful and easy to understand I believe.