Hey, I've been coding stuff for quite a while now, and it's starting to bug me that I still don't understand what the : is for when you do stuff like ply:Alive() or GM:PlayerDeath(ply)
-Snip-
I tried to draw parallels to programming while I was tired, sorry.
It's used to run a function on the parent item (the thing on the left of the semicolon).
[QUOTE=LuaMilkshake;31083333]The semicolon is used to run a function on the parent item, usually something like an entity. This is different from the period, which is usually used on something "static", like the draw library (the periods can also be used to add a property to a "non-static" item. This has to do with how the table is laid out in Lua, but the best way to think of it is that it is (I think) a "non-static" method of a class (If you're a programmer).
Simpler version: It's used to run a function on the parent item (the thing on the left of the semicolon).[/QUOTE]
What the hell your explanation is so confusing even I can't understand it. Also the semicolon is ';', ':' is called a colon as far as I know.
Basically, obj:func(a, b, c, d) is the same as obj.func(obj, a, b, c, d).
In the case of a function declaration, like "function GM:PlayerDeath(ply)", it is equivalent to "function GM.PlayerDeath(self, ply)", which explains why you use "self" in such functions.
That kind of syntax is there so you don't have to write things such as "ply.Alive(ply)", which gets really tedious after a while.
There is only one Alive function used by all players, and even though it seems to take no argument, its first argument is the player you want to check. If you wrote "ply.Alive()", it would call the Alive function with no argument, and it wouldn't be able to know which player you are talking about.
However, writing "ply:Alive()" translates to "ply.Alive(ply)", and that's how the function knows which player you are talking about.
It's pretty much the same for all objects that aren't unique. Like entities, vectors, angles, strings, everything that's mentioned as a "class" on the Gmod wiki, as opposed to "libraries" which are unique and are simply a set of utility functions.
As for why the gamemode table (GM) needs to have its functions declared with ':', I don't know. I guess it's just a convention that Garry made up, and everyone ended up following it without really wondering about it.
Its like gravity, its there, you can't get rid of it (unless you leave earth/ working code), and it just work.
Sorry, you need to Log In to post a reply to this thread.