[SOLVED \o/]sscanf and multiple optional parameters
#21

Thanks but, it didn't help much as it uses strtok! Lol

sscanf is lacking in decent documentation I think Where is ****** when you need him! :P

If someone can just tell me how the fuck to use sscanf properly .. I mean, now I can't even get the name of the vehicle, I'm totally giving up on this command right now
Reply
#22

The plugin has lots of documentation http://forum.sa-mp.com/index.php?topic=145539.0.

For your command I think it would be something like this (untested sorry):
pawn Код:
CMD:v(playerid, params[])
{
    if(PlayerInfo[playerid][Level] < 3 && !IsPlayerAdmin(playerid)) return 0;

    new vname[32], colour1, colour2;
    if(unformat(params, "s[32]I(-1)I(-1)", vname, colour1, colour2)) {
        SendClientMessage(playerid, 0xFF0000FF, "USAGE: /v [ModelID/Name] (Colour 1) (Colour 2)");
        return 1;
    }

    new modelid = strval(vname);
    if(modelid > 611 || modelid < 400) {
        modelid = GetVehicleModelIDFromName(vname);
        if(modelid == INVALID_MODEL_ID) {   // whatever GetVehicleModelIDFromName returns when no model is found
            SendClientMessage(playerid, 0xFF0000FF, "Invalid Vehicle Model");
            return 1;
        }
    }

    new Float:px, Float:py, Float:pz, Float:prot;
    if(IsPlayerInAnyVehicle(playerid)) {
        GetVehiclePos(GetPlayerVehicleID(playerid), px, py, pz);
        GetVehicleZAngle(GetPlayerVehicleID(playerid), prot);
    } else {
        GetPlayerPos(playerid, px, py, pz);
        GetPlayerFacingAngle(playerid, prot);
    }

    new vid = CreateVehicle(modelid, px + 4*floatcos(prot + 90.0, degrees), py + 4*floatsin(prot + 90.0, degrees), pz, prot + 90, colour1, colour2, -1);
    if(vid == INVALID_VEHICLE_ID) {
        SendClientMessage(playerid, 0xFF0000FF, "Vehicle limit reached");
        return 1;
    }
    if(vid >= sizeof(DynamicV)) {
        SendClientMessage(playerid, 0xFF0000FF, "Unable to create vehicle");
        DestroyVehicle(vid);
        return 1;
    }
    LinkVehicleToInterior(vid, GetPlayerInterior(playerid));
    SetVehicleVirtualWorld(vid, GetPlayerVirtualWorld(playerid));
    DynamicV[vid] = 1;

    new string[128];
    format(string, sizeof(string), "%s successfully spawned. To destroy it, use /dc", GetVehicleName(vid));
    SendClientMessage(playerid, 0xFFFFFFFF, string);
    return 1;
}
Reply
#23

@Dabombber: He said he's using the "old" sscanf. ^^

Try this;
pawn Код:
CMD:v(playerid, params[])
{
    if(PlayerInfo[playerid][Level] >= 3 || IsPlayerAdmin(playerid))
    {
        new car[ 64 ], vIsName, colour1[10], colour2[10], _colour1, _colour2, vehicle, Int, Float: Angle, Float: X,Float: Y,Float: Z, world;
        if(sscanf(params, "szz", car, colour1, colour2)) return SendClientMessage(playerid, Red, "USAGE: /v [ModelID/Name] [Colour 1] [Colour 2] (Colours optional)");
        {
            GetPlayerPos(playerid, X, Y, Z); Int = GetPlayerInterior(playerid); world = GetPlayerVirtualWorld(playerid); GetPlayerFacingAngle(playerid,Angle);
            if(!IsNumeric(car)) vIsName = GetVehicleModelIDFromName(car); else vIsName = strval(car);
            if(vIsName < 400 || vIsName > 611) return SendClientMessage(playerid, Red, "Invalid Vehicle Model");
            if(strlen(colour1) && IsNumeric(colour1)) _colour1 = strval(colour1); else _colour1 = random(126);
            if(strlen(colour2) && IsNumeric(colour2)) _colour2 = strval(colour2); else _colour2 = random(126);
            vehicle = CreateVehicle(vIsName, X+3, Y, Z, Angle, _colour1, _colour2, -1);
            if(world > 0) return SetVehicleVirtualWorld(vehicle,world);
            LinkVehicleToInterior(vehicle,Int);
            DynamicV[vehicle] = 1;
            format(string, sizeof(string), "%s successfully spawned. To destroy it, use /dc", GetVehicleName(vehicle));
            return SendClientMessage(playerid,White,string);
        }
    } else return 0;
    return 1;
}
Reply
#24

Quote:
Originally Posted by _❼_
The old one, I thought I'd start off with the original one, thought it might be easier to learn/implement?
The plugin has more features, better documentation and is faster.

The pawn version has no bounds checking, so to avoid an overflow you will need to make all the strings big enough.
pawn Код:
new car[128], vIsName, colour1[128], colour2[128]
Otherwise something like
Quote:

/v nrg 0000000000000000001

will cause a crash. Also, in the version you posted, the function will return prematurely if the world is above 0.

Using the plugin would be best, although if you must use the pawn version you could use something like:
pawn Код:
CMD:v(playerid, params[])
{
    if(PlayerInfo[playerid][Level] < 3 && !IsPlayerAdmin(playerid)) return 0;

    if(isnull(params)) {
        SendClientMessage(playerid, 0xFF0000FF, "USAGE: /v [ModelID/Name] (Colour 1) (Colour 2)");
        return 1;
    }
   
    new vname[128], colour1 = -1, colour2 = -1;
    sscanf(params, "sii", vname, colour1, colour2);

    new modelid = strval(vname);
    if(modelid > 611 || modelid < 400) {
        modelid = GetVehicleModelIDFromName(vname);
        if(modelid == INVALID_MODEL_ID) {   // whatever GetVehicleModelIDFromName returns when no model is found
            SendClientMessage(playerid, 0xFF0000FF, "Invalid Vehicle Model");
            return 1;
        }
    }

    new Float:px, Float:py, Float:pz, Float:prot;
    if(IsPlayerInAnyVehicle(playerid)) {
        GetVehiclePos(GetPlayerVehicleID(playerid), px, py, pz);
        GetVehicleZAngle(GetPlayerVehicleID(playerid), prot);
    } else {
        GetPlayerPos(playerid, px, py, pz);
        GetPlayerFacingAngle(playerid, prot);
    }

    new vid = CreateVehicle(modelid, px + 4*floatcos(prot + 90.0, degrees), py + 4*floatsin(prot + 90.0, degrees), pz, prot + 90, colour1, colour2, -1);
    if(vid == INVALID_VEHICLE_ID) {
        SendClientMessage(playerid, 0xFF0000FF, "Vehicle limit reached");
        return 1;
    }
    if(vid >= sizeof(DynamicV)) {
        SendClientMessage(playerid, 0xFF0000FF, "Unable to create vehicle");
        DestroyVehicle(vid);
        return 1;
    }
    LinkVehicleToInterior(vid, GetPlayerInterior(playerid));
    SetVehicleVirtualWorld(vid, GetPlayerVirtualWorld(playerid));
    DynamicV[vid] = 1;

    new string[128];
    format(string, sizeof(string), "%s successfully spawned. To destroy it, use /dc", GetVehicleName(vid));
    SendClientMessage(playerid, 0xFFFFFFFF, string);
    return 1;
}
Reply
#25

Ok .. I converted to the plugin. I used your version and it returned only two errors (\o/) one was something I know about and have fixed (GetVehicleIDFromName function was missing) and the other was INVALID_MODEL_ID not existing .. and I don't know how to handle that. LIke what am I meant to do if .. well, because the way you wrote it .. if someone puts in a model ID that isn't between 400-611 then it checks the vehicle name array for a match .. but what if there isn't a match, it will crash no? SO .. what do I return if there is no match o_O?
Reply
#26

Replace INVALID_MODEL_ID with whatever GetVehicleModelIDFromName returns when it can't find a matching model. If you're not sure then just use
pawn Код:
if(modelid > 611 || modelid < 400) {
again.
Reply
#27

pawn Код:
GetVehicleModelIDFromName(vname[])
{
    for(new i = 0; i < 211; i++)
    {
        if ( strfind(VehicleNames[i], vname, true) != -1 )
            return i + 400;
    }
    return -1;
}
-1? So like:

pawn Код:
if(modelid == -1) { // whatever GetVehicleModelIDFromName returns when no model is found
            SendClientMessage(playerid, 0xFF0000FF, "Invalid Vehicle Model");
            return 1;
        }
Reply
#28

Ok, tested it all and ... it works \o/! Lol... omg I'm so relieved ... I can finally move on, with new knowledge aswell!


Thanks to everyone who helped, and a special thanks to Dabombber for the final solution! :P
Reply
#29

Quote:
Originally Posted by _❼_
Ok, tested it all and ... it works \o/! Lol... omg I'm so relieved ... I can finally move on, with new knowledge aswell!


Thanks to everyone who helped, and a special thanks to Dabombber for the final solution! :P
That doesn't mean you're allowed to double-post, simply edit your post.
Reply
#30

Quote:
Originally Posted by Freddo
Quote:
Originally Posted by _❼_
Ok, tested it all and ... it works \o/! Lol... omg I'm so relieved ... I can finally move on, with new knowledge aswell!


Thanks to everyone who helped, and a special thanks to Dabombber for the final solution! :P
That doesn't mean you're allowed to double-post, simply edit your post.
I was overwhelmed with excitement, your highness.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)