Command not working as intended -
Luke_James - 22.02.2015
I'm having problems using sscanf again, this command doesn't work. If you type the command properly (i.e. /makedonator 23 bronze) it doesn't send the messages nor does it set the pDonator, it just sets it as 0.
pawn Код:
CMD:makedonator(playerid, params[])
{
if (PlayerData[playerid][pAdmin] < 5)
return SendErrorMessage(playerid, "You don't have permission to use this command!");
static
userid,
type[16],
message[128];
if (sscanf(params, "u", userid))
return SendSyntaxMessage(playerid, "/makedonator [playerid/name] [bronze/silver/gold/none]");
if (userid == INVALID_PLAYER_ID)
return SendErrorMessage(playerid, "You have specified an invalid player.");
if (strcmp(type, "bronze", true))
{
format(message, sizeof(message), "You have sed &s's donator rank to Bronze.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Bronze by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 1;
}
else if (strcmp(type, "silver", true))
{
format(message, sizeof(message), "You have sed &s's donator rank to Silver.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Silver by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 2;
}
else if (strcmp(type, "gold", true))
{
format(message, sizeof(message), "You have sed &s's donator rank to Gold.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Gold by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 3;
}
return 1;
}
Re: Command not working as intended -
Golf - 22.02.2015
Hello,
PHP код:
CMD:makedonator(playerid, params[])
{
if (PlayerData[playerid][pAdmin] < 5)
return SendErrorMessage(playerid, "You don't have permission to use this command!");
static
userid,
type[16],
message[128];
if (sscanf(params, "us[16]", userid,type))
return SendSyntaxMessage(playerid, "/makedonator [playerid/name] [bronze/silver/gold/none]");
if (userid == INVALID_PLAYER_ID)
return SendErrorMessage(playerid, "You have specified an invalid player.");
if (!strcmp(param, "bronze", true, 6))
{
format(message, sizeof(message), "You have sed &s's donator rank to Bronze.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Bronze by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 1;
}
if (!strcmp(param, "silver", true, 6))
{
format(message, sizeof(message), "You have sed &s's donator rank to Silver.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Silver by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 2;
}
if (!strcmp(param, "gold", true, 4))
{
format(message, sizeof(message), "You have sed &s's donator rank to Gold.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Gold by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 3;
}
return 1;
}
Re: Command not working as intended -
Luke_James - 22.02.2015
Quote:
Originally Posted by Golf
Hello,
PHP код:
CMD:makedonator(playerid, params[])
{
if (PlayerData[playerid][pAdmin] < 5)
return SendErrorMessage(playerid, "You don't have permission to use this command!");
static
userid,
type[16],
message[128];
if (sscanf(params, "us[16]", userid,type))
return SendSyntaxMessage(playerid, "/makedonator [playerid/name] [bronze/silver/gold/none]");
if (userid == INVALID_PLAYER_ID)
return SendErrorMessage(playerid, "You have specified an invalid player.");
if (!strcmp(param, "bronze", true, 6))
{
format(message, sizeof(message), "You have sed &s's donator rank to Bronze.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Bronze by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 1;
}
if (!strcmp(param, "silver", true, 6))
{
format(message, sizeof(message), "You have sed &s's donator rank to Silver.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Silver by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 2;
}
if (!strcmp(param, "gold", true, 4))
{
format(message, sizeof(message), "You have sed &s's donator rank to Gold.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Gold by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 3;
}
return 1;
}
|
What did I do wrong?
Re: Command not working as intended -
AndySedeyn - 22.02.2015
Glad you asked. The person above your post left you over to the guess-what-you-did-wrong part.
CMD:makedonator requires two parameters. In your case: the targetid and the type of donator. You defined everything correctly till the sscanf function.
pawn Код:
(sscanf(params, "u", userid))
You 'told' sscanf that there is 1 parameter to look out for: targetid.
Since no 'type' parameter was defined in your sscanf function, it will ignore all the strcmp functions underneath returning 1 for the command after inserting the target's id.
We define the second parameter (the 'type' parameter) in the following code:
pawn Код:
(sscanf(params, "us[16]", userid, type))
And we define a size to the 's' specifier since 'type' is a string.
Re: Command not working as intended -
Luke_James - 22.02.2015
Quote:
Originally Posted by Golf
Hello,
PHP код:
CMD:makedonator(playerid, params[])
{
if (PlayerData[playerid][pAdmin] < 5)
return SendErrorMessage(playerid, "You don't have permission to use this command!");
static
userid,
type[16],
message[128];
if (sscanf(params, "us[16]", userid,type))
return SendSyntaxMessage(playerid, "/makedonator [playerid/name] [bronze/silver/gold/none]");
if (userid == INVALID_PLAYER_ID)
return SendErrorMessage(playerid, "You have specified an invalid player.");
if (!strcmp(param, "bronze", true, 6))
{
format(message, sizeof(message), "You have sed &s's donator rank to Bronze.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Bronze by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 1;
}
if (!strcmp(param, "silver", true, 6))
{
format(message, sizeof(message), "You have sed &s's donator rank to Silver.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Silver by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 2;
}
if (!strcmp(param, "gold", true, 4))
{
format(message, sizeof(message), "You have sed &s's donator rank to Gold.", ReturnName(userid, 0));
SendClientMessage(playerid, COLOR_RED, message);
format(message, sizeof(message), "Your donator rank has been set to Gold by %s.", ReturnName(playerid, 0));
SendClientMessage(userid, COLOR_RED, message);
PlayerData[userid][pDonator] = 3;
}
return 1;
}
|
Quote:
Originally Posted by Bible
Glad you asked. The person above your post left you over to the guess-what-you-did-wrong part.
CMD:makedonator requires two parameters. In your case: the targetid and the type of donator. You defined everything correctly till the sscanf function.
pawn Код:
(sscanf(params, "u", userid))
You 'told' sscanf that there is 1 parameter to look out for: targetid.
Since no 'type' parameter was defined in your sscanf function, it will ignore all the strcmp functions underneath returning 1 for the command after inserting the target's id.
We define the second parameter (the 'type' parameter) in the following code:
pawn Код:
(sscanf(params, "us[16]", userid, type))
And we define a size to the 's' specifier since 'type' is a string.
|
Thank you.
I have just tested the command but it still isn't working for some reason, you don't get the message nor does the playerdata update.
Re: Command not working as intended -
AndySedeyn - 22.02.2015
Of course not. You are comparing (strcmp) the params (userid AND type) with a name. That's not what you are aiming for. You want to compare only the 'type' parameter.
Example code:
pawn Код:
if(!strcmp(type, "Bronze", true, 7))
What this means is: "If the parameter 'type' is equal to 'Bronze', execute following code:"
Re: Command not working as intended -
Luke_James - 22.02.2015
Quote:
Originally Posted by Bible
Of course not. You are comparing (strcmp) the params (userid AND type) with a name. That's not what you are aiming for. You want to compare only the 'type' parameter.
Example code:
pawn Код:
if(!strcmp(type, "Bronze", true, 7))
What this means is: "If the parameter 'type' is equal to 'Bronze', execute following code:"
|
That's what Golf has done in his above post, is it not? I have tested his code and it still doesn't work.
Re: Command not working as intended -
AndySedeyn - 22.02.2015
Quote:
Originally Posted by Luke_James
That's what Golf has done in his above post, is it not? I have tested his code and it still doesn't work.
|
He didn't. Have you even tried changing any of his code with the example code that I provided in my previous reply?
His code:
pawn Код:
if(!strcmp(params, "Bronze", true, 7))
Example code:
pawn Код:
if(!strcmp(type, "Bronze", true, 7))
As you can see, after the opening bracket of the strcmp function, he compared the params (userid AND type) with a string. What you want is compare the 'type' parameter to a string and see if it is equal to each other and that is what I did in the example code.
Re: Command not working as intended -
Luke_James - 22.02.2015
Quote:
Originally Posted by Bible
He didn't. Have you even tried changing any of his code with the example code that I provided in my previous reply?
His code:
pawn Код:
if(!strcmp(params, "Bronze", true, 7))
Example code:
pawn Код:
if(!strcmp(type, "Bronze", true, 7))
As you can see, after the opening bracket of the strcmp function, he compared the params (userid AND type) with a string. What you want is compare the 'type' parameter to a string and see if it is equal to each other and that is what I did in the example code.
|
Oh sorry, I understand now, thank you. What does the number 7 do?
Re: Command not working as intended -
AndySedeyn - 22.02.2015
Quote:
Originally Posted by Luke_James
Oh sorry, I understand now, thank you. What does the number 7 do?
|
It's an optional parameter of the strcmp function. It tells the strcmp function to how many characters it has to check.
https://sampwiki.blast.hk/wiki/Strcmp