Note: I didn't quite get your question, but I hope my answer clears some things up.
An important thing to note is that doing something like Part:Destroy() is exactly the same as doing Part.Destroy(Part). When you invoke a function with :, you are implicitly saying that you want Part to be the first argument of the Destroy function. Doing : is basically syntactic sugar that also reduces the number of keystrokes required to do the same thing.
To simplify things:
a:f(b)
is exactly the same as:
a.f(a, b)
I suppose we should start with something simple. Let's say you have an object called Animal with a property called Sound, and you want to make it so that whenever you do :Cry(n), it prints out Animal.Sound n number of times.
Here's how Animal would look like:
local Animal = {
Sound = "Quack"
}
And, if we were naive, we would write Cry to look something like this:
function Animal.Cry(animal, n)
for i = 1, n do
print(animal.Sound)
end
end
And if we want to call Cry, we would do this:
Animal.Cry(Animal, 3)
But we want to call Cry like this instead:
Animal:Cry(3)
To fix this, we would rewrite Cry so that it uses :.
function Animal:Cry(n)
for i = 1, n do
print(self.Sound)
end
end
self is special in Lua in that is references the object that called Cry. That is -- it secretly passed as the first parameter of the Cry function.
Animal:Cry(3)
|_____|
|
|
self refers to this
Note that this isn't "true" object-oriented programming.
True object-oriented programming would look something like this:
local Duck = Animal.new("Quack")
local Dog = Animal.new("Bark")
Duck:Cry(1)
Dog:Cry(2)
But that is outside the scope of this question. Here's a good reference for Lua OOP: http://www.lua.org/pil/16.html
5 comments
Reading your question again, are you referring to object-oriented programming?: http://www.lua.org/pil/16.html
If you are saying you want to create a custom method for the a class (e.g. BasePart), you cannot as the classes are abstract and can not be made reference to.
What if it's a class like
Partwhich has no abstract members?sigh people don't seem to get it. I want to actually make a custom function which has nothing to do with nay object, except that the object can be used to call. I am making a custom method, but used by parts in a different way.
I addressed your question in my editted answer. I guess that's what you wanted.