I am new (2 hours OLD) to this MOD, just heard of this from the SA-MP forum guys. Anyways, I read over your wiki and got a bit confused! (I am not a newbie at scripting, I do know PAWN, C++ and SQL) What is it with events and hooks? Suppose I would want to send a message to a guy who just connected. Can't I directly code INTO the event? Or would I have to do something like:
[code]
function Connection_message(playerid)
if playerid:IsPlayer() then
playerid:PrintMessage(HUD_PRINTTALK, ..playerid:Nick().." has joined, welcome!\n")
end
end
hook.Add("PlayerConnect", "MyPlayerConnect", Connection_message)
[/code]
So here, is [B]PlayerConnect[/B] is event? Can't I code directly something like?:
[code]
function PlayerConnect(..)
PrintMessage...
end
[/code]
Sorry, but I am quite unfamiliar to hooking methods, so would anyone be very kind to explain?
PS: Also - I visited the wiki, looked up google - I am quite unsure as to where/what to start coding. What would you suggest?
Thanks!
Gamemode functions are called from C++ to Lua during parts where garry thought it would be nice to have engine access in the form:
[lua]GM:FunctionName(arg1, arg2, ..., argn)
end[/lua]
Since there's more addons than just full gamemodes we have the hook system. Hooks are called before the gamemode function, I'm pretty sure they're called in the order they're added.
[lua]hook.Add("GamemodeFunctionName", "MyUniqueName", function(arg1, arg2, ..., argn)
end)
-- Can also use hook.Remove("GamemodeFunctionName", "MyUniqueName")[/lua]
You can also use an entity userdata for the second argument of hook.Add. That will make the first argument in the function the entity. The hook is auto-removed when the entity is removed.
[lua]
function ENT:Initialize()
hook.Add("PreDrawPlayer", self, self.PreDrawPlayer)
end
function ENT:PreDrawPlayer(pl)
if pl ~= self:GetParent() then return end
print('called before my parent player is rendered.')
end[/lua]
Hooks are called BEFORE the gamemode function. If you return anything (besides nil) inside your hook function, all subsequent hooks will not be called and the gamemode function won't either. If the gamemode function was expecting something in C++ it will catch what you returned in your hook.
Before the gamemode function? Does that mean in the code I have posted [URL="http://facepunch.com/showthread.php?t=1294193&p=41634099&viewfull=1#post41634099"](click here to view)[/URL] the hook MyPlayerConnect would be called before PlayerConnect (which is a DEFAULT gamemode function called on a player disconnecting, right?) That is weird!
It's called before so you have power over the gamemode function running or not. There's very complicated things you can do thanks to this.
[lua]<player joins>
C++ calls player connect in engine
Call to Lua for -> gamemode.Call("PlayerConnect", ....)
hooks called
gamemode function called (if hooks not overriding it)
C++ does stuff in engine based on what you returned in hooks or gamemode function (in this case nothing since the function doesn't expect anything returned)
<back to engine>[/lua]
That would allow for a lot of good banning techniques! Thank you for clearing that out. Now, could you answer my last question, it would be a nice thing if you could :)
[quote]PS: Also - I visited the wiki, looked up google - I am quite unsure as to where/what to start coding. What would you suggest?
Thanks![/quote]
Dunno that's up to you. Just don't be too ambitious.
Sorry, you need to Log In to post a reply to this thread.