SA-MP Forums Archive
Car spawn via car name? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Car spawn via car name? (/showthread.php?tid=420813)



Car spawn via car name? - Nathan_Taylor - 06.03.2013

So, I have this command, which works, spawning the car via the vehicle id, however, I want to use the vehicle name instead.

pawn Код:
CMD:v(playerid, params[])
    {
        new
            toid,
            carid,
            color1,
            color2;
           
        if(pInfo[playerid][Admin] > 1){

             if(!sscanf(params, "iiii", toid, carid, color1, color2)) {
               
                if(IsPlayerConnected(toid)){
                    if(carid >399 && carid < 612){
                    new
                        Float: curX,
                        Float: curY,
                        Float: curZ,
                        Float: curR;
                       
                    GetPlayerPos(toid, curX, curY, curZ);
                    GetPlayerFacingAngle(playerid, curR);
                    CreateVehicle(carid, curX+1, curY+1, curZ, curR, color1, color2, -1);
                       
                    new MessageToSender[128];
                    new MessageToReciever[128];
                    new allmessage[128];
                    new SenderName[25];
                    new RecieverName[25];
                       
                    GetPlayerName(playerid, SenderName, 18);
                    GetPlayerName(toid, RecieverName, 18);
                       
                    format(MessageToSender, sizeof(MessageToSender), "Car '%s' sent to %s.",  GetVehicleName(carid), RecieverName);
                    format(MessageToReciever, sizeof(MessageToReciever), "%s has just spawned you a car (%s).", SenderName, GetVehicleName(carid));
                    format(allmessage, sizeof(allmessage), "%s has sent a(n) %s to %s!", SenderName, GetVehicleName(carid), RecieverName);
                    SendClientMessage(playerid, COLOR_LIGHTPINK, MessageToSender);
                    SendClientMessage(toid, COLOR_LIGHTPINK, MessageToReciever);
                    SendClientMessageToAllButTwo(playerid, toid, COLOR_LIGHTPINK, allmessage);
                   
                    return true;

                    } else {
                        new errorstring[128];
                        format(errorstring, sizeof(errorstring), "ERROR: %i is not a valid car id!", carid);
                        SendClientMessage(playerid, COLOR_LIGHTPINK, errorstring);
                    }
                   
                } else {
                    new errorstring[128];
                    format(errorstring, sizeof(errorstring), "ERROR: %i is not a valid player!", toid);
                    SendClientMessage(playerid, COLOR_LIGHTPINK, errorstring);
                }

             } else {
                SendClientMessage(playerid, -1, "USAGE: /v [playerid] [car] [color1] [color2]");
             }

        } else {
            SendClientMessage(playerid, COLOR_LIGHTBLUE, "You don't have access to this command.");
        }
        return 1;
    }
Now, Can I do it somehow with an array like this?
pawn Код:
new VehicleNames[][] =
{
    "Landstalker", "Bravura", "Buffalo", "Linerunner", "Perrenial", "Sentinel",
    "Dumper", "Firetruck", "Trashmaster", "Stretch", "Manana", "Infernus",
    "Voodoo", "Pony", "Mule", "Cheetah", "Ambulance", "Leviathan", "Moonbeam",
    "Esperanto", "Taxi", "Washington", "Bobcat", "Whoopee", "BF Injection",
    "Hunter", "Premier", "Enforcer", "Securicar", "Banshee", "Predator", "Bus",
    "Rhino", "Barracks", "Hotknife", "Trailer", "Previon", "Coach", "Cabbie",
    "Stallion", "Rumpo", "RC Bandit", "Romero", "Packer", "Monster", "Admiral",
    "Squalo", "Seasparrow", "Pizzaboy", "Tram", "Trailer", "Turismo", "Speeder",
    "Reefer", "Tropic", "Flatbed", "Yankee", "Caddy", "Solair", "Berkley's RC Van",
    "Skimmer", "PCJ-600", "Faggio", "Freeway", "RC Baron", "RC Raider", "Glendale",
    "Oceanic","Sanchez", "Sparrow", "Patriot", "Quad", "Coastguard", "Dinghy",
    "Hermes", "Sabre", "Rustler", "ZR-350", "Walton", "Regina", "Comet", "BMX",
    "Burrito", "Camper", "Marquis", "Baggage", "Dozer", "Maverick", "News Chopper",
    "Rancher", "FBI Rancher", "Virgo", "Greenwood", "Jetmax", "Hotring", "Sandking",
    "Blista Compact", "Police Maverick", "Boxville", "Benson", "Mesa", "RC Goblin",
    "Hotring Racer A", "Hotring Racer B", "Bloodring Banger", "Rancher", "Super GT",
    "Elegant", "Journey", "Bike", "Mountain Bike", "Beagle", "Cropduster", "Stunt",
    "Tanker", "Roadtrain", "Nebula", "Majestic", "Buccaneer", "Shamal", "Hydra",
    "FCR-900", "NRG-500", "HPV1000", "Cement Truck", "Tow Truck", "Fortune",
    "Cadrona", "FBI Truck", "Willard", "Forklift", "Tractor", "Combine", "Feltzer",
    "Remington", "Slamvan", "Blade", "Freight", "Streak", "Vortex", "Vincent",
    "Bullet", "Clover", "Sadler", "Firetruck", "Hustler", "Intruder", "Primo",
    "Cargobob", "Tampa", "Sunrise", "Merit", "Utility", "Nevada", "Yosemite",
    "Windsor", "Monster", "Monster", "Uranus", "Jester", "Sultan", "Stratium",
    "Elegy", "Raindance", "RC Tiger", "Flash", "Tahoma", "Savanna", "Bandito",
    "Freight Flat", "Streak Carriage", "Kart", "Mower", "Dune", "Sweeper",
    "Broadway", "Tornado", "AT-400", "DFT-30", "Huntley", "Stafford", "BF-400",
    "News Van", "Tug", "Trailer", "Emperor", "Wayfarer", "Euros", "Hotdog", "Club",
    "Freight Box", "Trailer", "Andromada", "Dodo", "RC Cam", "Launch", "Police Car",
    "Police Car", "Police Car", "Police Ranger", "Picador", "S.W.A.T", "Alpha",
    "Phoenix", "Glendale", "Sadler", "Luggage", "Luggage", "Stairs", "Boxville",
    "Tiller", "Utility Trailer"
};
The current script takes the id, and then looks in the array for the car name, can I reverse that and make the command paramaters a string (for vehicleid), and two integers (for colors) and grab the ID (slot of the array) from the vehicle name? Or what is the easiest way of doing it?


Re: Car spawn via car name? - Jefff - 06.03.2013

pawn Код:
FindVehicleByNameID(const vname[])
{

    if('4' <= vname[0] <= '6') return INVALID_VEHICLE_ID;

    for(new i,LEN = strlen(vname); i != sizeof(VehicleNames); i++)
        if(!strcmp(VehicleNames[i],vname,true,LEN))
            return i + 400;

    return INVALID_VEHICLE_ID;
}

CMD:v(playerid, params[])
{
    new
        toid,
        carid[20],
        color1,
        color2;

    if(pInfo[playerid][Admin] < 2) SendClientMessage(playerid, COLOR_LIGHTBLUE, "You don't have access to this command.");
    else if(sscanf(params, "is[20]ii", toid, carid, color1, color2)) SendClientMessage(playerid, -1, "USAGE: /v [playerid] [carid / part of carname] [color1] [color2]");
    else if(!IsPlayerConnected(toid)) SendClientMessage(playerid, COLOR_LIGHTPINK, "ERROR: That ID is not a valid player!");
    else{
        new vID = FindVehicleByNameID(carid);
        if(vID == INVALID_VEHICLE_ID)
        {
            vID = strval(carid);
            if(!(399 < vID < 612)) return SendClientMessage(playerid, COLOR_LIGHTPINK, "ERROR: That ID is not a valid car id!");
        }
        new
            Float: curX,
            Float: curY,
            Float: curZ,
            Float: curR;

        GetPlayerPos(toid, curX, curY, curZ);
        GetPlayerFacingAngle(toid, curR);
        CreateVehicle(vID, curX+1, curY+1, curZ, curR, color1, color2, -1);

        new
            Message[128],
            SenderName[MAX_PLAYER_NAME + 1],
            RecieverName[MAX_PLAYER_NAME + 1];

        GetPlayerName(playerid, SenderName, MAX_PLAYER_NAME);
        GetPlayerName(toid, RecieverName, MAX_PLAYER_NAME);

        format(Message, sizeof(Message), "Car '%s' sent to %s.", GetVehicleName(vID), RecieverName);
        SendClientMessage(playerid, COLOR_LIGHTPINK, Message);
        format(Message, sizeof(Message), "%s has just spawned you a car (%s).", SenderName, GetVehicleName(vID));
        SendClientMessage(toid, COLOR_LIGHTPINK, Message);
        format(Message, sizeof(Message), "%s has sent a(n) %s to %s!", SenderName, GetVehicleName(vID), RecieverName);
        SendClientMessageToAllButTwo(playerid, toid, COLOR_LIGHTPINK, Message);
    }
    return 1;
}



Re: Car spawn via car name? - MP2 - 07.03.2013

Look in the fsdebug FS that comes with the SA-MP server package.


Re: Car spawn via car name? - Nathan_Taylor - 07.03.2013

@Jefff, that doesn't work, I get messages like "Hydra is not a valid vehicle name" If I'm not mistaken, wont the code...

pawn Код:
//gets vname from id
FindVehicleByNameID(const vname[])
{

    for(new i,LEN = strlen(vname); i != sizeof(VehicleNames); i++)
        if(!strcmp(VehicleNames[i],vname,true,LEN))
            return i + 400;

    return INVALID_VEHICLE_ID;
}
Always return INVALID_VEHICLE_ID no matter what?


Re: Car spawn via car name? - Nathan_Taylor - 07.03.2013

Disregatd last post, got it working


Re: Car spawn via car name? - MP2 - 07.03.2013

Heard of editing?