12.10.2011, 19:45
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:
Then all you have to do when spawning a vehicle using the name of the vehicle is:
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:
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.
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;
}
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
pawn Код:
new VehicleName[] = aVehicleNames[modelid - 400];
// Wholla, that string "VehicleName" now contains the name of the vehicle, gotten from the modelid