Hi,
I'm very new in (G)Lua. Currently I'm testing some stuff with hooks, but how can the function know what a ply or blabala bla is? Is it because of hook.Add?
Or a other example:
Some people write function(ply), but how does gmod know that this is the player?
It's because in your example, the first argument will always be a player. Argument names are purely semantics so they're up to you, but no matter what you name it, it doesn't change the underlying type. Take this hook for example:
https://i.imgur.com/PozoSzg.png
This tells me that the PlayerInitialSpawn hook provides one argument, a player. I could write the code like this:
hook.Add("PlayerInitialSpawn", "MySpawnHook", function(foo)
print(foo)
end)
and foo would be the Player object since its the first argument. You can even drop the argument if you don't need it and it will still be called when a player first spawns:
hook.Add("PlayerInitialSpawn", "MySpawnHook", function()
print("Someone spawned")
end)
Now take a more complex hook for example:
https://i.imgur.com/Cr81C47.png
This hook will always pass an entity object, a string, and another string in that order. This is what is known as the function's header, since all Lua needs to know internally is the function and the order of its arguments. How you use and name those arguments in your hook callback is completely up to you.
Thanks! <3
Sorry, you need to Log In to post a reply to this thread.