Problem with command
#1

Hey! I have this problem with my command. When I enter /refuel, I got message "USAGE: /refuel [choice]...." and command starts to behave that I typed already "full" so it will do that part of the code. Also it won't start timer. Something is wrong with my command but I don't know what. Can someone please help me?
Код:
CMD:refuel(playerid, params[])
{
	new string[128], choice[24],amount;
	new vehicleid = GetPlayerVehicleID(playerid);
   	if(!IsPlayerLoggedIn(playerid)) return SendClientMessage(playerid, COLOR_GREY, "You need to login first before using any command.");
	if(GetPlayerState(playerid) != PLAYER_STATE_DRIVER) return SendClientMessage(playerid, COLOR_GREY, "You are not driving a vehicle.");
	if(!IsPlayerInRangeOfPoint(playerid, 15, -2620.871,2261.44,8.16) && !IsPlayerInRangeOfPoint(playerid, 15, -2032.98,161.46,29.046)
	&& !IsPlayerInRangeOfPoint(playerid, 15, 1676.151,432.169,17.18) && !IsPlayerInRangeOfPoint(playerid, 15, -2420.154,969.84,45.3)
	&& !IsPlayerInRangeOfPoint(playerid, 15, -1465.75,1873.35,32.63)) return SendClientMessage(playerid, COLOR_GREY, "You are not near any gas station.");
	if(sscanf(params,"s",choice))
	{
        SendClientMessage(playerid, COLOR_GREY, "USAGE: /refuel [choice]");
        SendClientMessage(playerid, COLOR_GREY, "Choices: FULL | LITER");
        format(string,sizeof(string), "Fuel: %d/100 liters.", Fuel[vehicleid]);
        SendClientMessage(playerid, COLOR_WHITE, string);
	}
	if(!strcmp(choice, "full", true)) // problem starts here I think
	{
	    new engine,lights,alarm,doors,bonnet,boot,objective;
	    GetVehicleParamsEx(vehicleid,engine,lights,alarm,doors,bonnet,boot,objective);
	    if(engine == 1) return SendClientMessage(playerid, COLOR_GREY, "You need to turn off your engine first.");
	    new currentFuel;
	    currentFuel = 100 - Fuel[vehicleid];
	    if(PlayerInfo[playerid][pMoney] < currentFuel*3) return SendClientMessage(playerid, COLOR_GREY, "You don't have enough money to refuel your vehicle.");
		GiveDodMoney(playerid, -(3*currentFuel));
		Fuel[vehicleid] += currentFuel;
	}
	if(!strcmp(choice, "liter", true))
	{
	    if(sscanf(params,"si",choice,amount)) return SendClientMessage(playerid, COLOR_GREY, "USAGE: /refuel liter [amount]");
        new currentFuel;
	    currentFuel = 100 - Fuel[vehicleid];
	    new engine,lights,alarm,doors,bonnet,boot,objective;
	    GetVehicleParamsEx(vehicleid,engine,lights,alarm,doors,bonnet,boot,objective);
	    if(engine == 1) return SendClientMessage(playerid, COLOR_GREY, "You need to turn off your engine first.");
	    if(amount > currentFuel) return SendClientMessage(playerid, COLOR_GREY, "Your vehicle can't store that much fuel.");
	    if(PlayerInfo[playerid][pMoney] < amount*3) return SendClientMessage(playerid, COLOR_GREY, "You don't have enough money to refuel your vehicle.");
		GiveDodMoney(playerid, -(3*currentFuel));
		Fuel[vehicleid] += amount;
	}
    format(string, sizeof(string), "%s starts to pump fuel into their vehicle.", RPN(playerid));
	SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
	Refill[playerid] = 1;
	SetTimerEx("RefillTimer", 3000, false, "i", playerid);
	return 1;
}
Reply
#2

The problem lies within strcmp. It can return zero value in two conditions, either the string is equal to other string or the string is empty. That's why it behaves like you wrote "full" when you write nothing after /refuel.

To solve this, simply add an empty string control on if cause.

pawn Код:
if(!strcmp(choice, "full", true) && !isnull(choice))
and...

pawn Код:
if(!strcmp(choice, "liter", true) && !isnull(choice))
should solve your problem.
Reply
#3

thank you, my "full" choice works, but my "liter" won't work. It shows me USAGE: ... but when I type amount, it just skips everything after sscanf and it turns timer on. Do you maybe know why is that happening?
Reply
#4

I am pretty sure that multiple and wrong sscanf usages cause that, but I cannot give you an exact answer to solve your problem. Can you try;

pawn Код:
if(sscanf(choice,"si",choice2,amount))
And tell me if it still happens?
Reply


Forum Jump:


Users browsing this thread: