Sscanf problem.
#1

Hellooooo. I have a sscanf problem. I want to make command that spawns a vehicle. But I want to make command with a choice. You can type Vehicle ID or it's name. I know it's possible, because I have done it already (I don't know how...). I have made this command:
pawn Код:
dcmd_gpv(params[])
{
    new CarName[32], id, OptionalID, Color1, Color2;
  new Float: PlayerCoords[4];
    if(sscanf(params, "udid", id, OptionalID, Color1, Color2)) return print("Usage: /gpv [playerid/PartOfName] [CarID/CarName] [Color 1] [Color 2]"), 1;
    if(!sscanf(params, "usid", id, CarName, Color1, Color2)) goto by_name;
    if(!IsPlayerConnected(id)) return print("Player is not connected!"), 1;
    if(OptionalID < 400 || OptionalID > 611) return print("Wrong Car ID! IDs start at 400 and end at 611!"), 1;
    if(Color1 < 0 || Color1 > 126) return print("Wrong ColorID! Color IDs start at 0 and end at 126!"), 1;
    if(Color2 < 0 || Color2 > 126) return print("Wrong ColorID! Color IDs start at 0 and end at 126!"), 1;
    do{
    GetPlayerPos(id, PlayerCoords[0], PlayerCoords[1], PlayerCoords[2]);
    GetPlayerFacingAngle(id, PlayerCoords[3]);
    CreateVehicle(OptionalID, PlayerCoords[0], PlayerCoords[1], PlayerCoords[2]+1, PlayerCoords[3], Color1, Color2, 99*999);
    SendClientMessage(id, lightblue, "Remote admin has given you a vehicle!");
    printf("Player %s (ID: %i) was given a vehicle!", GetPlayerNick(id), id);
    return 1;
    }
    while(FALSE);
    by_name:
    if(!IsPlayerConnected(id)) return print("Player is not connected!"), 1;
    if(Color1 < 0 || Color1 > 126) return print("Wrong ColorID! Color IDs start at 0 and end at 126!"), 1;
    if(Color2 < 0 || Color2 > 126) return print("Wrong ColorID! Color IDs start at 0 and end at 126!"), 1;
    if(!ReturnCarID(CarName)) return print("Unknown vehicle name!"), 1;
    OptionalID = ReturnCarID(CarName);
    GetPlayerPos(id, PlayerCoords[0], PlayerCoords[1], PlayerCoords[2]);
    GetPlayerFacingAngle(id, PlayerCoords[3]);
    CreateVehicle(OptionalID, PlayerCoords[0]+3.0, PlayerCoords[1], PlayerCoords[2], PlayerCoords[3], Color1, Color2, 99*999);
    SendClientMessage(id, lightblue, "Remote admin has given you a vehicle!");
    printf("Player %s (ID: %i) was given a vehicle! (%s)", GetPlayerNick(id), id, CarName);
    return 1;
}
but it always go to the 'by_name', even if I type ID instead of Name... Anybody can help me?
Reply
#2


if(sscanf(params, "uddd", id, OptionalID, Color1, Color2))

Try this instead:

pawn Код:
if(sscanf(params, "uddd", id, OptionalID, Color1, Color2)) //if it "aint" that
{
    if(!sscanf(params, "usid", id, CarName, Color1, Color2)) //but it is that - go to the appropriate script part
    {
        goto by_name;
    }
    else //if it "aint" that either - tell them they fail
    {
        return print("Usage: /gpv [playerid/PartOfName] [CarID/CarName] [Color 1] [Color 2]")
    }
}
else // yeah whatever
{
//poop
}
Reply
#3

Thanks very much. It works! :*
Reply
#4

Well you taught me a little about goto so I guess I should thank you as well =/

Try this, saves using goto:

pawn Код:
dcmd_gpv(params[])
{
    new CarName[32], id, OptionalID, Color1, Color2;
    new Float: PlayerCoords[4];
    if(sscanf(params, "uddd", id, OptionalID, Color1, Color2))
    {
        if(!sscanf(params, "usid", id, CarName, Color1, Color2))
        {
            OptionalID = ReturnCarID(CarName);
        }
        else
        {
            return print("Usage: /gpv [playerid/PartOfName] [CarID/CarName] [Color 1] [Color 2]")
        }
    }
    else
    {
        if(!IsPlayerConnected(id))
        {
            return print("Player is not connected!");
        }
        if(OptionalID < 400 || OptionalID > 611)
        {
            return print("Wrong Car ID! IDs start at 400 and end at 611!");
        }
        if(Color1 < 0 || Color1 > 126 || Color2 < 0 || Color2 > 126)
        {
            return print("Wrong ColorID! Color IDs start at 0 and end at 126!");
        }
        else
        {
            GetPlayerPos(id, PlayerCoords[0], PlayerCoords[1], PlayerCoords[2]);
            GetPlayerFacingAngle(id, PlayerCoords[3]);
            CreateVehicle(OptionalID, PlayerCoords[0], PlayerCoords[1], PlayerCoords[2]+1, PlayerCoords[3], Color1, Color2, 99*999);
            SendClientMessage(id, lightblue, "Remote admin has given you a vehicle!");
            printf("Player %s (ID: %i) was given a vehicle!", GetPlayerNick(id), id);
        }
    }
    return 1;
}
Reply
#5

You helped me and I helped you. We both learned something.
@up.
I've already made a command that works as it's supposed to.
pawn Код:
dcmd_gpv(params[])
{
    new CarName[32], id, OptionalID, Color1, Color2;
  new Float: PlayerCoords[4];
    if(sscanf(params, "uddd", id, OptionalID, Color1, Color2)) //if it "aint" that
    {
        if(!sscanf(params, "usid", id, CarName, Color1, Color2)) //but it is that - go to the appropriate script part
        {
            goto by_name;
        }
        else //if it "aint" that either - tell them they fail
        {
            return print("Usage: /gpv [playerid/PartOfName] [CarID/CarName] [Color 1] [Color 2]"), 1;
        }
    }
    else
    {
    if(!IsPlayerConnected(id)) return print("Player is not connected!"), 1;
    if(OptionalID < 400 || OptionalID > 611) return print("Wrong Car ID! IDs start at 400 and end at 611!"), 1;
    if(Color1 < 0 || Color1 > 126) return print("Wrong ColorID! Color IDs start at 0 and end at 126!"), 1;
    if(Color2 < 0 || Color2 > 126) return print("Wrong ColorID! Color IDs start at 0 and end at 126!"), 1;
    do{
    GetPlayerPos(id, PlayerCoords[0], PlayerCoords[1], PlayerCoords[2]);
    GetPlayerFacingAngle(id, PlayerCoords[3]);
    CreateVehicle(OptionalID, PlayerCoords[0], PlayerCoords[1], PlayerCoords[2]+1, PlayerCoords[3], Color1, Color2, 99*999);
    SendClientMessage(id, lightblue, "Remote admin has given you a vehicle!");
    printf("Player %s (ID: %i) was given a vehicle! (%s)", GetPlayerNick(id), id, VehicleNames[OptionalID-400]);
    return 1;
    }
    while(FALSE);
    by_name:
    if(!IsPlayerConnected(id)) return print("Player is not connected!"), 1;
    if(Color1 < 0 || Color1 > 126) return print("Wrong ColorID! Color IDs start at 0 and end at 126!"), 1;
    if(Color2 < 0 || Color2 > 126) return print("Wrong ColorID! Color IDs start at 0 and end at 126!"), 1;
    if(!ReturnCarID(CarName)) return print("Unknown vehicle name!"), 1;
    OptionalID = ReturnCarID(CarName);
    GetPlayerPos(id, PlayerCoords[0], PlayerCoords[1], PlayerCoords[2]);
    GetPlayerFacingAngle(id, PlayerCoords[3]);
    CreateVehicle(OptionalID, PlayerCoords[0]+3.0, PlayerCoords[1], PlayerCoords[2], PlayerCoords[3], Color1, Color2, 99*999);
    SendClientMessage(id, lightblue, "Remote admin has given you a vehicle!");
    printf("Player %s (ID: %i) was given a vehicle! (%s)", GetPlayerNick(id), id, CarName);
    return 1;
    }
 return 1;
}
It's also possible to make with IsNumeric. But first of all, IsNumeric should have modified return values.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)