functions args

I’m trying to learn lua and I’m having some difficulties grasping the complete function function (lol)


I get that

function func()


That basiclly prints hi to console because you ran that function, but what I don’t get is


Here is a sample code from that I don’t quite understand.

local function superjump( ply, command)
concommand.Add( "SuperJump", superjump )

From what I read in the wiki you have to define the args in the function, so in that case what is ply and what and why is command not defined.

it’s like… if we do:

function lol( test )

then you can call that function by doing

lol( "what ever" )

and variable “test” will in this case become “what ever”


and it will print it in console too

Looking at your final question vexx, the reason Arguments is not needed there is because of what that function is actually doing. That console command is just making the player (ply is the player who called the console command) jump stupidly high by setting their velocity. You do not need any parameters for that. Think about it, why would you need any exactly? Use the Wiki as a reference not as a “set in stone” law otherwise you will get a bit confused. Coding is a very flexible thing.

If the function set a custom velocity on the other hand then you would need to pass an argument through. Here’s a quick example:

[lua]RunConsoleCommand( “SuperJump”, “2000” );[/lua]

[lua]local function SuperJump( Player, Command, Arguments )
if( #Arguments < 1 ) then

Player:SetVelocity( Vector( 0, 0, tonumber( Arguments[1] ) ) );

concommand.Add( “SuperJump”, SuperJump );[/lua]

See now arguments has to be defined as you’re passing the value of 2000 (the “force” of the jump) instead of just having a fixed value of 1000.

I hope this helps, if not feel free to PM me or add me on Steam.

that kinda answered my question but not entirly. I also code for e2 and in that you have to define every variable, one, commands isn’t being defined so why is it there, and player isn’t being defined so why is it automaticly the player?
In expression 2 you would say player=entity():owner()

whenever you run a command it passes three things to the server to use, the player entity, the exact command typed in and what was added on the end

Think of it this way: Joe Blogs types “I_Like Pie”

It goes to the server, and says to the function ply (player) is Joe Blogs, He typed “I_Like” for a cmd, and his args (arguments, addons to the end) are “Pie”

You define the function to be a console command using concommand.Add. The concommand functions are there to handle the creation or removal of valid commands for players to use. It is utilizing a system from the Source Engine if you take a peek at the developer wiki. A console command becomes an instance of the ConCommand class which will handle the initialisation. Lua’s concommand.Add will utilise this system and “do the work for you” when it is parsed.

Like ashashinand said, once you call the function client side the information regarding which player is passed through to the server.

Sorry if this doesn’t help you get it, to me it’s a weird thing to explain in a post.

oh ok now i kinda get it.