Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is the point of putting '.' or ':' in a function?

Asked by 6 years ago

for example

1function Noob:Print()
2 
3 
4 
5or function Poop.Fart

2 answers

Log in to vote
1
Answered by 6 years ago

The main difference here is that : signifies a method while . signifies that a function/value is a member of a table. Also, with any function declared using :, the self parameter is essentially set to the table the function is contained within, unlike functions declared using :, functions declared using "." doesn't have a set self parameter, you can try this yourself by doing the following:

1local tbl = {}
2 
3function tbl:thing ()
4    print(self)--prints the table "tbl"
5end
6 
7functib tbl.thing2 ()
8    print(self)--prints nil
9end

Methods created by the : syntax are extremely useful with Object oriented programming and class constructors. Such a purpose is demonstrated in the 16th chapter of the lua pil

Ad
Log in to vote
5
Answered by 6 years ago

The main difference is that ':' has self as it's first argument without actually typing it down.

01Humanoid = {health = 100}
02 
03function Humanoid:TakeDamage(dmg)
04    self.health = self.health - dmg -- i didn't need to put self as an argument but I can still use it
05end
06 
07-- equivalent
08function Humanoid.TakeDamage(self, dmg)
09    self.health = self.health - dmg
10end
11 
12-- usage
13Humanoid:TakeDamage(10) -- you usually see this
14Humanoid.TakeDamage(Humanoid, 10) -- the same

Answer this question