Quick Lua Questions

I just have some quick Lua questions, and it would seem the simple question megathread has disappeared…

  1. Custom Hooks
    How would you go about creating your own Hook? I think I did it successfully, but can a hook attached to a function with client-side code (for example, creating a VGUI) be called on server-side and run without error? I tried using usermessages and the net library, but I can’t seem to figure out how to transfer such data.

  2. GM and GAMEMODE.
    What’s the difference, if there is any? Can you make your own variables with the GAMEMODE. preceding it? Like GAMEMODE.round_state, etc. Also, what does having XX:functionName do (the XX part, that is)? Can you make your own library with this, and contain functions?

  1. : and .
    In functions like PANEL:Paint(), what’s the difference between something like PANEL:SetPos and PANEL.DoClick? How does the punctuation mark differentiate one from the other?\

Sorry if these are rather simple, but I can’t seem to find an answer elsewhere.

I can answer 3

: is the same as . except that : passes what you put it after to the function as its first argument, self

easiest way to explain:

[lua]PANEL.DoClick = function( PANEL ) PANEL:DoSomething() end[/lua]

is the same as

[lua]function PANEL:DoClick() self:DoSomething() end[/lua]

I personally for that instance prefer the second one, but either works.

I’ll also take a shot at 2. I don’t know the inner workings to be honest but I know that GM is existent while/before the gamemode is initialized, and GAMEMODE is GM except post-gamemode-initialization. Why the change? I don’t really know, someone else will have to fill that in.

Thanks for the help, anyone else have any other answers?

1:
just use hook.Call(hookname, arguments).
So if you wanted a custom hook “test”:
[lua]
hook.Add(“test”, “test1”, function(a) print(a) end)

hook.Call(“test”, 15)
[/lua]

This would print 15.

2:
GM is the variable name when loading the gamemode, GAMEMODE is then set to the GM table and GM is set to nil.

3:
To add to the above response.
The colon is just syntactic sugar for table.method(table).