[Tutorial] Making 'vehicles' Command with LUA
#1

Hello everybody, I released LUA Wrapper.It was too slower than pawn.But today I will fixed it.And put into ******-code project.Now I'm showing how to make 'vehicles' command.This commands shows all vehicles in dialog box...

You can look the tutorial & codes & comments at here (Syntax highlight)

If you dont want syntax highlight:

Code:
local VehicleNames = {
    "Landstalker","Bravura","Buffalo","Linerunner","Pereniel","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","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","ZR3 50","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","Cropdust","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 A",
    "Monster B","Uranus","Jester","Sultan","Stratum","Elegy","Raindance","RC Tiger","Flash","Tahoma","Savanna","Bandito","Freight","Trailer",
    "Kart","Mower","Duneride","Sweeper","Broadway","Tornado","AT-400","DFT-30","Huntley","Stafford","BF-400","Newsvan","Tug","Trailer A","Emperor",
    "Wayfarer","Euros","Hotdog","Club","Trailer B","Trailer C","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","Luggage Trailer B",
    "Stair Trailer","Boxville","Farm Plow","Utility Trailer"
};
DeallowedVehicles = {
	["Hunter"] = 1,
	["Hydra"] = 1, 
	["Rhino"] = 1
	-- You can add deallowed vehicles with this method.Or You can add by the id
	-- ["VehicleID"] = 1,
	-- For example: For deallow infernus;
		-- 	["Infernus"]=1 
		--		or
		--	["411"]=1
};
function showVehicleList(playerid)
	if isPlayerInAnyVehicle(playerid) then -- If player in any vehicle
		return sendClientMessage(playerid, 0xFF0000FF, 'This command deallowed in car.You should down the car.'); -- show message
	end
	local named = "" -- Create a new variable
	for k,v in ipairs(VehicleNames) do -- For each the table
		if ( named=="" ) then -- If named is empty
			named = v -- "named" equals vehicle name
		else -- else
			named = named.."\n"..v -- Named equals Named+NewLine+VehicleName
		end
	end
	showPlayerDialog(playerid, 9122, DIALOG_STYLE_LIST, "Select Car",named,"Select","Cancel"); -- Show dialog
end
addCommandHandler('vehicles',showVehicleList); -- add command handler to function
addCommandHandler('cars',showVehicleList); -- add command handler to function

addEventHandler('onDialogResponse', -1, function(playerid,dialogid, response, listitem, _) -- Add event handlers
	if (dialogid == 9122) then -- If dialogid == VehicleListDialogID
		if (response) then -- If selected "Select" button
			
			local veh_name = VehicleNames[listitem + 1]; -- Get Vehicle Name, we put[listitem+1] because lua is not zero based index.
			-- Pawn is zero based index, Lua is one(1) based index.
			
			local veh_Deallowed = DeallowedVehicles[veh_name]; -- Get key from deallowed vehicles by the vehicle name
			if ( veh_Deallowed == 1 or DeallowedVehicles[tostring(listitem + 400)] == 1) then -- If vehicle deallowed by name or by id
				sendClientMessage(playerid, 0xFF0000FF, "Vehicle '"..veh_name.."' deallowed by the admin"); -- send message
				return 1; -- stop proccesing.
			end
			
			if (getPVarInt(playerid, "vehicle:get") ) then -- If player have a car
				destroyVehicle(getPVarInt(playerid, "vehicle:id")); -- Destroy it
				setPVarInt(playerid, "vehicle:get",false) -- Change it, player does not have a car now.
			end
			local x,y,z = getPlayerPos(playerid); -- Get player positions
			local a = getPlayerFacingAngle(playerid); -- Get player facing angle
			
			local vehid = createVehicle(listitem + 400, x+4, y+4, z+1, a, 1, 1, 60); -- Create vehicle
			putPlayerInVehicle(playerid,vehid, 0); -- put player
			setPVarInt(playerid, "vehicle:get",1) -- Setting
			setPVarInt(playerid, "vehicle:id", vehid); -- Setting
			return 1;
		end
	end
	return 0; -- You don't need "return 0;" Because "onPlayerCommandText" and "onDialogResponse" automatic returns 0; if you don't wrote.
	-- But other events return 1; if you don't wrote.
end);

addEventHandler('onPlayerDisconnect', -1, function(playerid) -- I can get one parameter only :D I can wrote reason parameter to function But I don't need to it.
	if (getPVarInt(playerid, "vehicle:get") ) then -- If player have a car
		destroyVehicle(getPVarInt(playerid, "vehicle:id")); -- Destroy it
	end
end)
Reply
#2

oh man, I didn't touch lua in a long time :/ anyway nice, especially LUA Wrapper
Reply
#3

And is that used in Pawno or how do we use this? You should teach it completly, not only give away some codes.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)