Function with optional parameters
#1

Can anyone teach me how to make functions with one or more optional parameters?

Example:
pawn Код:
format(string, sizeof(string), "%s and %s are two strings", var_1, var_2);
format(param_1, param_2, param_3, optional_param_1, optional_param_2, ...); // this function has optional paramters
Reply
#2

pawn Код:
stock OptionalParameter(playerid, vehicleid = INVALID_VEHICLEID)
{

}
Reply
#3

I mean those functions that have a "{Float,_}:..." parameter, how do they work?
Reply
#4

Use functions numargs, getarg, setarg.
Reply
#5

Quote:
Originally Posted by MadeMan
Посмотреть сообщение
Use functions numargs, getarg, setarg.
Can you tell me how do they work?
Reply
#6

numargs shows the number of arguments
getarg gets the argument value
setarg sets the argument value

If you have a function like

pawn Код:
SetPlayerData(playerid, ...)
and you call it like this

pawn Код:
new number = 789;
SetPlayerData(playerid, 45, 1235, number);
numargs() is 4
getarg(0) is playerid
getarg(1) is 45
getarg(2) is 1235
getarg(3) is 789 (because number = 789)
setarg(3, 0, 123) sets 'number' to 123

There is also an example on the wiki

https://sampwiki.blast.hk/wiki/ResetPlayerWeaponsEx
Reply
#7

Thank you very much, I owe you a cookie!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)