Multiple parameters with a single stock function
#1

I have the following stock function.

pawn Код:
stock SendAdminMessage(string[], level = 0, color = COLOR_LIGHTBLUE)
{
    foreach(Player, i)
    {
        if(pInfo[i][pAdminLevel] > 0)
            if(pInfo[i][pAdminLevel] > level) SendClientMessage(i, color, string);
    }
}
I am trying to use the function, but the only optional parameter I am looking to use would be "color". Is there a way to use the function without needing to set values for all of the other optional parameters?

EDIT: By the way, I am willing to use a public function if doing such with a stock function is not possible.

EDIT: Thanks to randomkid88 and ******, the problem is now solved!
Reply
#2

There should be a way because it is done with strcmp. That's all I could say.
Reply
#3

Quote:
Originally Posted by Tee
Посмотреть сообщение
There should be a way because it is done with strcmp. That's all I could say.
I don't think you fully understand. I am creating a function not a command; therefore strcmp is not even needed here. I think you are confused of the differences...
Reply
#4

Use named arguments, for example:
pawn Код:
SendAdminMessage("Test Message", .color = COLOR_PURPLE);
Put a "." in front of the argument name. I think this only works as long as all the params have default values, like you have above.
Reply
#5

Quote:
Originally Posted by randomkid88
Посмотреть сообщение
Use named arguments, for example:
pawn Код:
SendAdminMessage("Test Message", .color = COLOR_PURPLE);
Put a "." in front of the argument name. I think this only works as long as all the params have default values, like you have above.
I don't believe adding periods does anything, nor does it help the cause for this thread. The stock function currently on the main page of this thread works just fine, when I input values for the other parameters, even if I only need the 'color' variable.
Reply
#6

Quote:
Originally Posted by ******
Посмотреть сообщение
It does in fact do something and does help the cause of this thread, in fact it is the exact correct answer! When CALLING the function (not when defining it), specify the name of the parameter you are passing using ".par_name=value". For an example look at the usage of y_ini's INI_ParseFile function which often has calls looking something like:

pawn Код:
INI_ParseFile(file, "Callback", .bExtra = true, .extra = playerid);
Oh I see. @ randomkid88: Please, excuse me then.

@ ******: Thank you, I'll give that a shot.
Reply
#7

Okay, so this is the new function:

pawn Код:
stock SendAdminMessage(string[], .level = 0, .color = COLOR_LIGHTBLUE)
{
    foreach(Player, i)
    {
        if(pInfo[i][pAdminLevel] > 0)
            if(pInfo[i][pAdminLevel] > level) SendClientMessage(i, color, string);
    }
}
I am executing the following function:

pawn Код:
SendAdminMessage(string, .level=4);
This is what the compiler returns:

Код:
C:\Users\Piccoli\Desktop\samp03\gamemodes\RC.pwn(341) : error 017: undefined symbol "level"
C:\Users\Piccoli\Desktop\samp03\gamemodes\RC.pwn(341) : warning 215: expression has no effect
C:\Users\Piccoli\Desktop\samp03\gamemodes\RC.pwn(341) : error 001: expected token: ";", but found ")"
C:\Users\Piccoli\Desktop\samp03\gamemodes\RC.pwn(341) : error 029: invalid expression, assumed zero
C:\Users\Piccoli\Desktop\samp03\gamemodes\RC.pwn(341) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


4 Errors.
Reply
#8

Read this quote carefully:

Quote:
Originally Posted by ******
Посмотреть сообщение
It does in fact do something and does help the cause of this thread, in fact it is the exact correct answer! When CALLING the function (not when defining it), specify the name of the parameter you are passing using ".par_name=value".
Then you should know what you did wrong.
Reply
#9

Quote:
Originally Posted by roschti
Посмотреть сообщение
Read this quote carefully:



Then you should know what you did wrong.
A friend pointed out my idiotic mistake before you said anything, thanks though.

Problem solved!
Reply
#10

pawn Код:
stock SendAdminMessage(string[], level = 0, color = COLOR_LIGHTBLUE)
{
    foreach(Player, i)
    {
        if(pInfo[i][pAdminLevel] > 0)
            if(pInfo[i][pAdminLevel] > level) SendClientMessage(i, color, string);
    }
}
You call it with:
pawn Код:
SendAdminMessage(string[], level = 0, color = COLOR_LIGHTBLUE)
//String must be used
//Optional - Level is 0 if there is no parameter
//Optional - Color is LightBlue if there is no parameter
SendAdminMessage("Test",2);//There is example
[EDIT]I late a bit xD
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)