[Include] CreateVehicleForPlayer - Use Names to create vehicles, Easily.
#23

Why do you use 200 if statements to get the vehicle's model ID from the name of the vehicle? Why not use a simple array, which is pretty much a standard array that everyone uses that was compiled by betamaster a long time ago:

pawn Код:
new aVehicleNames[212][] = {    // Vehicle Names - Betamaster
    {"Landstalker"},
    {"Bravura"},
    {"Buffalo"},
    {"Linerunner"},
    {"Perrenial"},
    {"Sentinel"},
    {"Dumper"},
    {"Firetruck"},
    {"Trashmaster"},
    {"Stretch"},
    {"Manana"},
    {"Infernus"},
    {"Voodoo"},
    {"Pony"},
    {"Mule"},
    {"Cheetah"},
    {"Ambulance"},
    {"Leviathan"},
    {"Moonbeam"},
    {"Esperanto"},
    {"Taxi"},
    {"Washington"},
    {"Bobcat"},
    {"Mr Whoopee"},
    {"BF Injection"},
    {"Hunter"},
    {"Premier"},
    {"Enforcer"},
    {"Securicar"},
    {"Banshee"},
    {"Predator"},
    {"Bus"},
    {"Rhino"},
    {"Barracks"},
    {"Hotknife"},
    {"Trailer 1"}, //artict1
    {"Previon"},
    {"Coach"},
    {"Cabbie"},
    {"Stallion"},
    {"Rumpo"},
    {"RC Bandit"},
    {"Romero"},
    {"Packer"},
    {"Monster"},
    {"Admiral"},
    {"Squalo"},
    {"Seasparrow"},
    {"Pizzaboy"},
    {"Tram"},
    {"Trailer 2"}, //artict2
    {"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"}, //hotrina
    {"Hotring Racer B"}, //hotrinb
    {"Bloodring Banger"},
    {"Rancher"},
    {"Super GT"},
    {"Elegant"},
    {"Journey"},
    {"Bike"},
    {"Mountain Bike"},
    {"Beagle"},
    {"Cropdust"},
    {"Stunt"},
    {"Tanker"}, //petro
    {"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 LA"}, //firela
    {"Hustler"},
    {"Intruder"},
    {"Primo"},
    {"Cargobob"},
    {"Tampa"},
    {"Sunrise"},
    {"Merit"},
    {"Utility"},
    {"Nevada"},
    {"Yosemite"},
    {"Windsor"},
    {"Monster A"}, //monstera
    {"Monster B"}, //monsterb
    {"Uranus"},
    {"Jester"},
    {"Sultan"},
    {"Stratum"},
    {"Elegy"},
    {"Raindance"},
    {"RC Tiger"},
    {"Flash"},
    {"Tahoma"},
    {"Savanna"},
    {"Bandito"},
    {"Freight Flat"}, //freiflat
    {"Streak Carriage"}, //streakc
    {"Kart"},
    {"Mower"},
    {"Duneride"},
    {"Sweeper"},
    {"Broadway"},
    {"Tornado"},
    {"AT-400"},
    {"DFT-30"},
    {"Huntley"},
    {"Stafford"},
    {"BF-400"},
    {"Newsvan"},
    {"Tug"},
    {"Trailer 3"}, //petrotr
    {"Emperor"},
    {"Wayfarer"},
    {"Euros"},
    {"Hotdog"},
    {"Club"},
    {"Freight Carriage"}, //freibox
    {"Trailer 3"}, //artict3
    {"Andromada"},
    {"Dodo"},
    {"RC Cam"},
    {"Launch"},
    {"Police Car (LSPD)"},
    {"Police Car (SFPD)"},
    {"Police Car (LVPD)"},
    {"Police Ranger"},
    {"Picador"},
    {"S.W.A.T. Van"},
    {"Alpha"},
    {"Phoenix"},
    {"Glendale"},
    {"Sadler"},
    {"Luggage Trailer A"}, //bagboxa
    {"Luggage Trailer B"}, //bagboxb
    {"Stair Trailer"}, //tugstair
    {"Boxville"},
    {"Farm Plow"}, //farmtr1
    {"Utility Trailer"} //utiltr1
};

stock GetModelIDFromName(name[])
{
    for(new i; i < sizeof(aVehicleNames); i++) if(strfind(aVehicleNames[i], name, true) != -1) return i;
    return -1;
}
Then all you have to do when spawning a vehicle using the name of the vehicle is:

pawn Код:
new modelid = GetModelIDFromName("NRG-500");
if(modelid == -1) // The name was not a valid one
else // It was a valid one and the modelid variable now contains the model ID of the vehicle
This also means you can use the array for other things, like if you wanted to get the vehicle's name from the modelid, you could do this:

pawn Код:
new VehicleName[] = aVehicleNames[modelid - 400];

// Wholla, that string "VehicleName" now contains the name of the vehicle, gotten from the modelid
See how much cleaner and more understandable the code is? I'm honestly surprised that people are using such strange coding methods for such trivial problems.
Reply


Messages In This Thread
CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by iPLEOMAX - 26.06.2011, 19:59
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by FireCat - 26.06.2011, 21:52
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by Markx - 26.06.2011, 22:28
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by RyDeR` - 26.06.2011, 22:31
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by iPLEOMAX - 26.06.2011, 22:37
Re : CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by ludesert - 29.06.2011, 14:08
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by juraska - 29.06.2011, 14:10
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by sasuga - 29.06.2011, 14:49
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by Genious - 29.06.2011, 15:03
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by yuri29971102 - 01.07.2011, 14:00
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by marwan - 01.07.2011, 14:23
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by iPLEOMAX - 01.07.2011, 14:27
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by Horrible - 15.07.2011, 08:40
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by Horrible - 28.07.2011, 23:59
AW: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by Tigerkiller - 29.07.2011, 00:39
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by Zh3r0 - 29.07.2011, 00:47
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by iPLEOMAX - 29.07.2011, 11:10
AW: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by Pablo Borsellino - 29.07.2011, 11:29
Re: AW: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by iPLEOMAX - 29.07.2011, 11:34
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by Killer786 - 12.10.2011, 14:38
Re : CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by TheBest6 - 12.10.2011, 15:51
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by Emmet_ - 12.10.2011, 15:57
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by JaTochNietDan - 12.10.2011, 19:45
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by iPLEOMAX - 12.10.2011, 19:48
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by THE_KING$5$ - 15.10.2011, 11:22
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by THE_KING$5$ - 15.10.2011, 11:25
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by gamer931215 - 15.10.2011, 11:25
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by manchestera - 20.11.2011, 17:26
Respuesta: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by [Nikk] - 26.12.2011, 22:25
Re: CreateVehicleForPlayer - Use Names to create vehicles, Easily. - by [xT]Emaratii - 28.06.2012, 10:59

Forum Jump:


Users browsing this thread: 3 Guest(s)