Jump to content

Question from Pawn scripter


s2sk

Recommended Posts

Posted

Hi.

I have 2 problem. In Pawn (sa-mp hello) i can write

stock Function(value1, value2 = 5, value3 = 10) // value2 and value3 optional parameters

{

return 1;

}

 

how make it in Lua (i talking about optional parameters)?

 

if make so

function Function(value1, value2 = 5, value3 = 10)
{
}

i have error from interpreter

 

and more question... how skip argument?

 

again in Pawn i can write

stock Function(value1, value2 = 5, value = 10)

{

return 1;
}



public OnPlayerConnect(playerid)
{

Function(value1, .value3 = 20); // <<<----------- skip value2 
}

 

how make it on Lua?

 

thx.

Posted
7 minutes ago, Juuve said:

Check this

You also should know lua basics to make your own script correct

tupie deffki scyk...

i know basics. i asked about optional params and skip params...

Posted

@s2sk

function optional( value, value2, value3 )
    iprint(value,(value2 or 20),(value2 or 30))
end
optional(9) -- Print: 9, 20, 30
optional(9,9) -- Print: 9, 9, 30
optional(9,false,9) -- Print: 9, 20, 9

 

  • Thanks 1
  • Discord Moderators
Posted

You can use everything actually instead of value, I mean every false condition(so only nil and false and _(this is called a dummy variable, and its always a nil value))
 

  • Moderators
Posted (edited)

@s2sk

 

Skipping a parameter.

function test (...)
	local parameters = {...}
  
	local variable1 = parameters[1]
	local variable3 = parameters[3]

	print(variable1, variable3)
end

test(1,2,3)

 

Edited by IIYAMA
  • Like 2
  • Moderators
Posted (edited)

A underline makes sense, because people understand that it means that the parameter has no purpose. But it is technically not skipping, as the underline is a variable after all.

It just depends how you want to pass that information into the function.

 

A single parameter that contains a table with multiple values works sometimes even better, because you can use it as an array(similar to parameters in a way) as well as an object.

Edited by IIYAMA

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...