sscanf problem
#1

Код HTML:
CMD:give(playerid, params[])
{
	new targetid, type, amount, msg[256];
	if(sscanf(params, "us[10]i", targetid, type, amount))
	{

		if(amount < 1 || amount > 9999999) 
		{
			SendClientMessage(playerid, COLOR_RED, "You have typed an invalid amount.");
			return 1;
		}
		
		if(targetid == playerid) 
		{
			SendClientMessage(playerid, COLOR_RED, "You cannot give an item to yourself.");
			return 1;
		}
		
		if(PlayerInfo[playerid][LoggedIn] == false) 
		{
			SendClientMessage(playerid, COLOR_RED, "This player was not found.");
			return 1;
		}
		printf("%s", targetid);
		
		new Float:X, Float:Y, Float:Z;
		GetPlayerPos(targetid, X, Y, Z);
		if(!IsPlayerInRangeOfPoint(playerid, 5.5, X, Y, Z))
		{
			SendClientMessage(playerid, COLOR_RED, "You are too far away from this player.");
			return 1;
		}
		
		if(!strcmp(params, "Pot", true))
		{
			if(amount > PlayerInfo[playerid][Marijuana]) 
			{
				SendClientMessage(playerid, COLOR_RED, "You don't have enough of this item on you.");
				return 1;
			}
			
			PlayerInfo[playerid][Marijuana] -= amount;
			PlayerInfo[targetid][Marijuana] += amount;

			format(msg, sizeof(msg), "You gave %s grams of marijuana to %s.", amount, GetPlayerNameEx(targetid));
			SendClientMessage(playerid, COLOR_GREEN, msg);
				
			format(msg, sizeof(msg), "You received %s grams of marijuana from %s.", amount, GetPlayerNameEx(playerid));
			SendClientMessage(targetid, COLOR_GREEN, msg);
		
			format(msg, sizeof(msg), "* %s hands some marijuana to %s.",GetPlayerNameEx(playerid), GetPlayerNameEx(targetid));
			ProxDetector(GetPlayerVirtualWorld(playerid) == 0 ? 20.0 : 7.0, playerid, msg, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
			return 1;
		}
	} 
	else 
	{
		SendClientMessage(playerid, COLOR_GREY, "HINT: /give [playerid] [item] [amount]");
		SendClientMessage(playerid, COLOR_GREY, "HINT: Items: pot");
	}
	return 1;
}
When I execute the command in game on someone it says "You are too far away from the player" even though I am within distance. Also, the print message says the targetid is "ot". Can anyone teach me what I'm doing wrong?
Reply
#2

Код:
		if(!IsPlayerInRangeOfPoint(playerid, 5.5, X, Y, Z))
		{
			SendClientMessage(playerid, COLOR_RED, "You are too far away from this player.");
			return 1;
		}
to

Код:
		if(IsPlayerInRangeOfPoint(playerid, 5.5, X, Y, Z))
		{
			SendClientMessage(playerid, COLOR_RED, "You are too far away from this player.");
			return 1;
		}
what you are doing is checking if NOT in range of point by inserting ! before IsPlayerInRangeOfPoint. remove the !.
i dont see what is wrong with printf tho.
Reply
#3

what u mean by amount < 1 ??
it should be amount >1....
Reply
#4

First of all you are doing all the stuff when sscanf stuff. It returns 0 if it succeeds so change
Код:
if (sscanf...)
to
Код:
if (!sscanf...)
if (!...) is the same thing as if (... == 0)
so as sscanf fails the targetid is 0 or INVALID_PLAYER_ID I assume.
Second you are using strcmp on params when you check the type, you must use it on the "type" string.
Third you should also check if the target ID is invalid by checking if it's INVALID_PLAYER_ID.

Also it's printing something else because targetid is an integer and you are trying to print it as a string, use %i or %d instead. Also you don't need 256 cells for messages because the max length of a client message is 144 as far as I know. You should even use less than 144.
Reply
#5

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
First of all you are doing all the stuff when sscanf stuff. It returns 0 if it succeeds so change
Код:
if (sscanf...)
to
Код:
if (!sscanf...)
if (!...) is the same thing as if (... == 0)
so as sscanf fails the targetid is 0 or INVALID_PLAYER_ID I assume.
Second you are using strcmp on params when you check the type, you must use it on the "type" string.
Third you should also check if the target ID is invalid by checking if it's INVALID_PLAYER_ID.

Also it's printing something else because targetid is an integer and you are trying to print it as a string, use %i or %d instead. Also you don't need 256 cells for messages because the max length of a client message is 144 as far as I know. You should even use less than 144.
I understand this. I tried using ! before but it still didn't work. I fixed the print but it now displays the 'targetid' as 111. How did that happen? It still says "You are too far away from the player." It clearly is something wrong with targetid.
Reply
#6

Код:
CMD:give(playerid, params[])
{
	new targetid, type[10], amount, msg[144];
	if(!sscanf(params, "us[10]i", targetid, type, amount))
	{
	    SendClientMessage(playerid, COLOR_GREY, "HINT: /give [playerid] [item] [amount]");
		SendClientMessage(playerid, COLOR_GREY, "HINT: Items: pot");
		return 1;
	}
	
	if(amount < 1 || amount > 9999999) return SendClientMessage(playerid, COLOR_RED, "You have typed an invalid amount.");

	if(targetid == playerid) return SendClientMessage(playerid, COLOR_RED, "You cannot give an item to yourself.");

        if(targetid == INVALID_PLAYER_ID) return SendClientMessage(playerid, COLOR_RED, "This player was not found.");

	if(PlayerInfo[targetid][LoggedIn] == false) return SendClientMessage(playerid, COLOR_RED, "This player was not found.");

	new Float:X, Float:Y, Float:Z;
	GetPlayerPos(targetid, X, Y, Z);
	if(IsPlayerInRangeOfPoint(playerid, 5.5, X, Y, Z))
	{
		if(!strcmp(type, "Pot", true))
		{
			if(amount > PlayerInfo[playerid][Marijuana])
			{
				SendClientMessage(playerid, COLOR_RED, "You don't have enough of this item on you.");
				return 1;
			}

			PlayerInfo[playerid][Marijuana] -= amount;
			PlayerInfo[targetid][Marijuana] += amount;

			format(msg, sizeof(msg), "You gave %s grams of marijuana to %s.", amount, GetPlayerNameEx(targetid));
			SendClientMessage(playerid, COLOR_GREEN, msg);

			format(msg, sizeof(msg), "You received %s grams of marijuana from %s.", amount, GetPlayerNameEx(playerid));
			SendClientMessage(targetid, COLOR_GREEN, msg);

			format(msg, sizeof(msg), "* %s hands some marijuana to %s.",GetPlayerNameEx(playerid), GetPlayerNameEx(targetid));
			ProxDetector(GetPlayerVirtualWorld(playerid) == 0 ? 20.0 : 7.0, playerid, msg, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
			return 1;
		}
	} else SendClientMessage(playerid, COLOR_RED, "You are too far away from this player.");
	return 1;
}
1. You didn't make "type" a string, you created it as an integer.

2. Use a cellsize of 144 for messages, as thats the maximum that can be displayed on the screen

3. Use if(!sscanf(...)) and return 1 to display the USAGE: etc.

4. I have inverted your !IsPlayerInRangeOfPoint to IsPlayerInRangeOfPoint. Added else and the message at the bottom to show the error if he is too far away from the player.

5. It's not if(!strcmp(params, "Pot", true)), it's if(!strcmp(type, "Pot", true)), because you are compaing the string "type" and "Pot"

6. Added an error if a player is not connected "if(targetid == INVALID_PLAYER_ID) return SendClientMessage(...)", keep it in mind and also always use them before arrays, i.e. PlayerInfo[targetid][LoggedIn], otherwise you will get errors like "Array index out of bounds" since the targetid is 65535 (defined as INVALID_PLAYER_ID) and the size of the array is probably MAX_PLAYERS.

7. I have made your errors a bit more simple with just "if(...) return SendClientMessage(...)"

Hope this works lad!
Reply
#7

Thanks for pointing out a lot of my mistakes. However, I'm still having some issues. Now when I perform the command, it is saying I've inserted a invalid amount.

Sorry for this, but as you can probably tell, I've never used sscanf2 before.
Reply
#8

Quote:
Originally Posted by boy
Посмотреть сообщение
Thanks for pointing out a lot of my mistakes. However, I'm still having some issues. Now when I perform the command, it is saying I've inserted a invalid amount.

Sorry for this, but as you can probably tell, I've never used sscanf2 before.
It is very convenient and easy to use, check out more about it here.

Did you enter a valid amount? Everything seems to be fine in "if(amount < 1 || amount > 9999999)".
Reply
#9

Quote:
Originally Posted by iamjems
Посмотреть сообщение
It is very convenient and easy to use, check out more about it here.

Did you enter a valid amount? Everything seems to be fine in "if(amount < 1 || amount > 9999999)".
Whenever I type just /give (nothing else) it says "You have entered a invalid amount." However, when I type in the amount it just sends the "HINT: /give" message. Do you know why this is happening?
Reply
#10

In iamjems code remove the ! before the sscanf.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)