[Tutorial] The simpliest vehicle spawner! (dialog)
#1

Hey guys!
Time for another tutorial.
In this one I'll show you how to script the easiest vehicle spawner without adding a hella lot lines because every vehicle has it's own name, etc.

First off, you will need to place this inside your script:

pawn Code:
#include <zcmd>
// Description: Including the best command processor out there - zcmd by Zeex.
// Note: Add this to the top of the script.

#define DIALOG_VEHICLES 123321
// Description: Defining the dialog for later use.

static const VehicleName[212][] = {
    "Landstalker","Bravura","Buffalo","Linerunner","Perennial","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",
    "ZR-350","Walton","Regina","Comet","BMX","Burrito","Camper","Marquis","Baggage","Dozer","Maverick","News Chopper",
    "Rancher","FBI Rancher","Virgo","Greenwood","Jetmax","Hotring Racer","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","Stuntplane","Tanker","Road Train","Nebula","Majestic",
    "Buccaneer","Shamal","Hydra","FCR-900","NRG-500","HPV-1000","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","Emperor","Wayfarer",
    "Euros","Hotdog","Club","Trailer","Trailer","Andromada","Dodo","RCCam","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"
};

// Description: A static constant including vehicle names, very useful for getting them.

#define GetVehicleName(%0) VehicleName[GetVehicleModel(%0)-400]
// Description: Get any vehicle's name from the vehicle's ID.

#define GetVehicleModelName(%0) VehicleName[%0-400]
// Description: Get any vehicle's name from the vehicle's model ID.

stock randomEx(minnum = cellmin, maxnum = cellmax)
{
    return random(maxnum - minnum + 1) + minnum;
}
// Description: Get a randomized number between the minimum and the maximum number.
// Usage: randomEx(2, 10);
// Will return: A randomized value between 2 and 10.

stock SpawnVehicleInFrontOfPlayer(playerid, vehiclemodel, color1, color2)
{
    new Float:x,Float:y,Float:z;
    new Float:facing;
    new Float:distance;

        GetPlayerPos(playerid, x, y, z);
        GetPlayerFacingAngle(playerid, facing);

        new Float:size_x,Float:size_y,Float:size_z;
    GetVehicleModelInfo(vehiclemodel, VEHICLE_MODEL_INFO_SIZE, size_x, size_y, size_z);

    distance = size_x + 0.5;

    x += (distance * floatsin(-facing, degrees));
        y += (distance * floatcos(-facing, degrees));

    facing += 90.0;
    if(facing > 360.0) facing -= 360.0;

    return CreateVehicle(vehiclemodel, x, y, z + (size_z * 0.25), facing, color1, color2, -1);
}
// Description: Spawning a vehicle in front of the player. VERY useful.
Now when you have added these functions, you will start to script the command. I'll explain each line.

pawn Code:
CMD:vspawner(playerid,params[])
{
        if(PlayerInfo[playerid][pAdmin] >= 2)
    {
        new dstring[2048];
        new string[64];
        format(dstring,sizeof(dstring),"{FFFFFF}");
        for(new i=400; i < 612; i++)
        {
            format(string,sizeof(string),"%s\n",GetVehicleModelName(i));
            strcat(dstring, string);
        }
        ShowPlayerDialogEx(playerid, DIALOG_VEHICLES, DIALOG_STYLE_LIST, "{FFFFFF}Vehicles",dstring,"Spawn","Cancel");
    }
    return 1;
}
pawn Code:
if(PlayerInfo[playerid][pAdmin] >= 2)
This checks if the player's admin level is equal or higher than 2. You will have to change this to your admin variable, or use this:
pawn Code:
if(IsPlayerAdmin(playerid))
Now, if the player is a RCON admin, it will allow him to use this command.

pawn Code:
new dstring[2048];
The line above creates a huge variable to store all the vehicle names, later used in the dialog.

pawn Code:
new string[64];
Creating a variable to store the a vehicle's name, which will later be conected with the big string.

pawn Code:
format(dstring,sizeof(dstring),"{FFFFFF}");
Formatting the huge string to be white. You can set this to whichever color you want to.

pawn Code:
for(new i=400; i < 612; i++)
Creating a loop that will loop through all vehicle models.

pawn Code:
format(string,sizeof(string),"%s\n",GetVehicleModelName(i));
Formatting the little string to include the vehicle's name. GetVehicleModelName(modelid), mentioned earlier, is used here. \n is also used to secure the next line.

pawn Code:
strcat(dstring, string);
Joining the two strings together. The "string" will be connected with "dstring" now. Once the loop loops through all the vehicles, the "dstring" will have all the vehicle names in it.
More about strcat here.

pawn Code:
ShowPlayerDialog(playerid, DIALOG_VEHICLES, DIALOG_STYLE_LIST, "{FFFFFF}Vehicles",dstring,"Spawn","Cancel");
Showing the long dialog to the player, with the title "Vehicles" in white and including the "dstring".

Now that we got the command done, we must script the vehicle to spawn under OnDialogResponse.

pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case DIALOG_VEHICLES: // If the dialog ID is DIALOG_VEHICLES (defined earlier as 123321)
        {
            if(response) // If the player clicked "Spawn" or the enter key on the keyboard
            {
                SpawnVehicleInFrontOfPlayer(playerid, listitem+400, randomEx(0,255), randomEx(0,255));
                            // Spawn a vehicle in front of the player.
                            // listitem+400 you ask. This is used because the list items are actually 0, 1, 2 etc., so it will add 400 to it, because that's the lowest vehicle model ID.
                            // randomEx(0,255) - Randomizing the vehicle's color 1 and color 2.
            }
        }
        }
        return 1;
}
That is pretty much it. I hope you have enjoyed this tutorial!
Reply
#2

Have you tested this tutorial?

Because dialogs only show up to 50 lines, all 212 vehiclemodels won't be listed, even when they fit inside your huge string.
Reply
#3

Quote:
Originally Posted by AmigaBlizzard
View Post
Have you tested this tutorial?

Because dialogs only show up to 50 lines, all 212 vehiclemodels won't be listed, even when they fit inside your huge string.
I tested it indeed. Lists all 212 vehicles.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)