sscanf optional values
#1

Hello there.
I'm wondering how can I switch 2 type of values with sscanf. What I mean?
Example, a player have to type the ID of a vehicle OR the plate of the vehicle.
So.. how can I make it?
PHP код:
CMD:rplate(playeridparams[])
{
    if(!
GetPlayerVehicleID(playerid)) return SCM(playeridRED"You must be in a vehicle.");
    
    new 
        
target,
        
plate[12+EOS] = EOS;
    if(
sscanf(params"I(65535)S()[12]"targetplate)) 
        return 
SCM(playeridLBLUE"/rplate [blabla]");
    if(
target == INVALID_VEHICLE_ID && strval(plate) < 2)
        return 
SCM(playeridRED"Error"); 
If I type nothing after /rplate, I've "Error" message. If I type an ID, this working too but if I type a plate I've "/rplate" message. I think because I is optionnal but I can't pass it.
So, I hope you understand what I mean.
Thanks you'!
Reply
#2

Example:

PHP код:
CMD:rplate(playeridparams[])
{
    if(!
GetPlayerVehicleID(playerid)) return SCM(playeridRED"You must be in a vehicle.");
    new
        
target INVALID_VEHICLE_ID,
        
plate[13] = EOS
    
;
    if(!
sscanf(params"i"target))
    {
        
// User typed an ID
        
if(target == INVALID_VEHICLE_ID)
            return 
SCM(playeridRED"Error");
    }
    else if(!
sscanf(params"s[13]"plate))
    {
        
// User typed a plate
        
if(strlen(plate) < 2)
            return 
SCM(playeridRED"Error");
    }
    else 
SCM(playeridLBLUE"/rplate [blabla]");
    return 
1;

I'm assuming you were meant to use 'strlen' instead of 'strval'. If you are 'definitely' expecting the user to type in a parameter, it isn't an optional parameter. In this case, the user MUST enter either a string or an integer, so there really aren't any optional parameters in this instance. You could also replace 'plate' with 'params', but that's... optional.
Reply
#3

Not sure if you are going to allow /rplate for both ID and plate (e.g. "/rplate 5 232") well i hope you still understand the logic which in my opinion, your code is confusing:
Код:
native IsValidVehicle(vehicleid); // Read https://sampwiki.blast.hk/wiki/IsValidVe...e(playerid, params[])
{
	if(!GetPlayerVehicleID(playerid)) return SCM(playerid, RED, "You must be in a vehicle.");

	new 
		target,
		plate[12+1];

	if(!sscanf(params, "i", target))
	{
		if(IsValidVehicle(target))
		{
			// the target vehicle id is not valid/not exist
			return SCM(playerid, RED, "Error");
		}
		else
		{
			// codes if player typed /rplate [vehicleid]
			return 1;
		}
	}
	else if(!sscanf(params, "s[12]", plate)) // You sure it's a String (any text) not just a number? because i see strval()
	{
		if(strval(plate) < 2) // did you mean strlen() ?
		{
			// the plate text is a number less than 2 (i.e "/rplate 1"), did you mean strlen() ?
			return SCM(playerid, RED, "Error");
		}
		else
		{
			// codes if player typed /rplate [platetext]
			return 1;
		}
	}
	else
	{
		// either the player mistyped the parameters or didn't specify it
		return SCM(playerid, LBLUE, "/rplate [blabla]");
	}
}
Note: it will not work if the player typed /rplate [vehicleid] [platetext]

EDIT: Threshold is faster
Reply
#4

If you want to make it for veh id OR veh plate - you can do custom sscanf argument.

Code:

Код:
SSCANF:vehicle(string[])
{
	new veh_id = INVALID_VEHICLE_ID;
	if ('0' <= string[0] <= '9')
	{
		new
		ret = strval(string);
		if (IsValidVehicle(ret))
		{
			return ret;
		}
	}

	for(new i = 0; i < GetVehiclePoolSize(); i++)
	{
		if(!strcmp(string, VehicleData[i][vehicle_plate], true))
		{
			veh_id = i;
			break;
		}
	}
	return veh_id;
}
Command:

Код:
new veh_model;
if(sscanf(params, "k<vehicle>", veh_model))
     {
         SCM(playerid, LBLUE, "/rplate [blabla]");
         return 1;
     }
     //Here you go, you can use it now
You just need to change VehicleData[i][vehicle_plate] lines for your vehicle array
Reply
#5

Thanks you guyz. This is very interesting.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)