s2sk Posted July 5, 2018 Share Posted July 5, 2018 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. Link to comment
JeViCo Posted July 5, 2018 Share Posted July 5, 2018 (edited) Check this You also should know lua basics to make your own script correct Edited July 5, 2018 by Juuve Link to comment
s2sk Posted July 5, 2018 Author Share Posted July 5, 2018 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... Link to comment
JeViCo Posted July 5, 2018 Share Posted July 5, 2018 (edited) -- Edited July 5, 2018 by Juuve Link to comment
Mr.Loki Posted July 5, 2018 Share Posted July 5, 2018 @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 1 Link to comment
Discord Moderators Pirulax Posted July 6, 2018 Discord Moderators Share Posted July 6, 2018 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)) Link to comment
Moderators IIYAMA Posted July 6, 2018 Moderators Share Posted July 6, 2018 (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 July 6, 2018 by IIYAMA 2 Link to comment
Discord Moderators Pirulax Posted July 6, 2018 Discord Moderators Share Posted July 6, 2018 Or he could you use a _ instead. But makes no sense to do that Link to comment
Moderators IIYAMA Posted July 6, 2018 Moderators Share Posted July 6, 2018 (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 July 6, 2018 by IIYAMA Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now