Parameters

I’m still a bit confused about what parameters I put in custom functions. Any examples/help/etc…?

Whatever parameters you need for the function to run. For example,


function PrintX() print( x ) end

would print the global variable “x”. However, if you did this:


function PrintX(x) print( x ) end

then the argument you pass to the function is printed.

When functions are called, it contains the function call and it’s parameters.

[lua]parameter1 = “Hello”
parameter2 = “World”
RandomFunction(parameter1,parameter2)[/lua]

Then, inside the function, it could look like so:

[lua]function RandomFunction(param1,param2)
print(param1…" “…param2…”!")
end[/lua]

This would print “Hello World!” in your console. Outside of Lua, there is much more to parameters, but this is all you need to know for what it is.

The parameters you put into the actual function, in this case param1 and param2, would be called as param1 and param2 inside the function, and they’d be the same as parameter1 and parameter2.

Did that answer your question?

Could you give me an example of that?

[lua]PrintX( “Hello World” )[/lua]

So would something like this work?



function FirstSpawn( ply )
 
	LoadAccount( ply )
 
end

hook.Add( "PlayerInitialSpawn", "loadingaccountsblah", FirstSpawn )

function LoadAccount( ply )
    /*
    do account loading shit here
    */
end


And the ‘ply’ parameter in LoadAccount() would be the player that initially spawned?

Yes, it works exactly like that. Note that the parameter name doesn’t need to be the same, so this would also work:
[lua]function FirstSpawn( ply )

LoadAccount( ply )

end

hook.Add( “PlayerInitialSpawn”, “loadingaccountsblah”, FirstSpawn )

function LoadAccount( aPlayer )
/*
do account loading shit here, using aPlayer
*/
end[/lua]

Thank you!

Lua always passes by value, so don’t worry about local variables not getting passed or anything like that.

Apart from when it passes by reference, such as for tables and userdata values( ie players, vectors, entities etc).