• Some questions about Lua
    6 replies, posted
I have read the wiki and other guides but I can't seem to understand the concept of Functions and Arguments fully. [code]hook.Add( "PlayerInitialSpawn", "First_Spawn", function( ply ) ply:PrintMessage( HUD_PRINTTALK, "Welcome to the server " .. ply:GetName() .. "!" ); end );[/code] In this code, I have some questions. What is (ply) and why is it written in brackets? At [code]( "PlayerInitialSpawn", "First_Spawn", function( ply )[/code] Why are there commas? I would really appreciate a detailed response about this as I want to understand this and start writing my own code soon. Thanks :D
Commas are to separate arguments in functions. Hook.add is a function that takes a hook name, an identifier, and a callback function as its arguments, in that order. The ply in parentheses is a variable local to the callback function that is sent to the function when it is called. If you don't understand these concepts I recommend you check out the lua documentation on functions and arguments
Lua documentation? Where can I find it?
Arguments are provided data elements which are sent in by the engine or a programmer when they call that function. "ply" is just the name that the programmer made for that example; you can name it anything you want like "Player" or "Whatever," so long that you change all uses of "ply" in the code. The commas separate arguments when you call a function: hook.Add takes three arguments: a string for the event (PlayerInitialSpawn), a string for the name of the hook -- which can be anything you want (First_Spawn), and a callback function for when the event occurs. [editline]20th October 2015[/editline] [QUOTE=Romester;48944362]Lua documentation? Where can I find it?[/QUOTE] [url]http://www.lua.org/docs.html[/url] If you don't want to get the books, check out the reference manual.
[QUOTE=Romester;48944362]Lua documentation? Where can I find it?[/QUOTE] Here is the lua reference manual [URL="http://www.lua.org/manual/5.1/manual.html"]http://www.lua.org/manual/5.1/manual.html[/URL]
[QUOTE=Romester;48944266] In this code, I have some questions. What is (ply) and why is it written in brackets? At [code]( "PlayerInitialSpawn", "First_Spawn", function( ply )[/code] Why are there commas?[/QUOTE] So basically the ply variable is the player thats spawning first time. What your looking at is a hook. It is called whenever said action happens. So the idea is, if oyu have a player spawning the first time, you have a way to detect it, and when it detects it, it calls the function you defined, giving the ply a actaul value, the player that joined. Sorry if i didnt help i tried my best XD
Thanks everyone for helping me out!
Sorry, you need to Log In to post a reply to this thread.