[Tutorial] Simple Car Spawn Command
#1

Car Spawn System

Hello, This is my first Tutorial. Its A simple command aimed at those new to scripting.
It Allows a player to spawn any vehicle of their choice by the vehicle ID number.
Firstly Lets start off with the basics:

You should see this at the start of the Gamemode Or Filterscript
pawn Code:
#include <a_samp>
Under it add the following:
pawn Code:
#include <zcmd>
You Can Download This At: https://sampforum.blast.hk/showthread.php?tid=91354 - CreditsTo Zeex

Info: ZCMD is one of or the fastest command processor. Its also easier to understand.

pawn Code:
#include <sscanf2>
You Can Download This At:https://sampforum.blast.hk/showthread.php?tid=120356 - Credits To ******


Once you have downloaded and installed those plugins and includes to the correct place (info how to on their respective pages) We Can continue on the tutorial.

At the top of the script you will need an variable. Variables are simple structures for holding data, both numerical and string.
The variable we need will be called "Vehicle"

So Add:

pawn Code:
new Vehicle[MAX_PLAYERS];
Above your "OnGameModeInit".
Its value will be at default 0. Which is what we want



Now for the command Itself

With ANY ZCMD you need one of the following:

-COMMAND:mycommand(playerid, params[])

-command(mycommand, playerid, params[])

Or my favourite and the one we will be using:

=CMD:mycommand(playerid, params[])

So at the bottem of you script create the command for your car spawner.

Example:
pawn Code:
CMD:veh(playerid, params[])
And DONT forget your brackets!
So it should look like this
pawn Code:
CMD:veh(playerid, params[])
{
return 1;
}

Now we need some more varibles.

We need a varible for the vehicle and for the message (string) and the position of the vehicle

So add:

pawn Code:
new car;
new string[128];
new Float:X, Float:Y, Float:Z; //This will be where the vehicle spawns!
Your Script should look something like this:

pawn Code:
CMD:veh(playerid, params[])
{
new car;
new string[128];
new Float:X, Float:Y, Float:Z;
return 1;
}
If not, read through the tutorial again!

Now to set where the car will spawn we need the players position. We will be doing this with "GetPlayerPos".
Instead of adding values to the line of code add the "Floats" to where the numbers will go.

It should look something like this

pawn Code:
GetPlayerPos(playerid, Float:X, Float:Y, Float:Z);
And add that to the command as well
Which now should look like this:
pawn Code:
CMD:veh(playerid,params[])
{
new car;
new string[128];
new Float:X, Float:Y, Float:Z;
GetPlayerPos(playerid, Float:X, Float:Y, Float:Z);
return 1;
}
Right now the command is useless. So we need to make the varibles do stuff.
So what are the vehicle ids? The first vehicle's id is 400 and the last is 611.

Now what happens if the player just uses "/veh" ? Well in order to stop the command from doing anything we need this line in it

pawn Code:
if(sscanf(params,"i", car)) return SendClientMessage(playerid,0xff0000ff,"USAGE: /Veh <Vehicle ID 400 - 611>");
Add the following code to your script.
pawn Code:
if(sscanf(params,"i", car)) return SendClientMessage(playerid,0xff0000ff,"USAGE: /Veh <Vehicle ID 400 - 611>");
Now to understand the line.

params: is short for parameters.

"i" means interger. In SSCANF there are many "letters" which mean different things. "s" means string (text).

SendClientMessage(playerid,0xff0000ff,"USAGE: /Veh <Vehicle ID 400 - 611>"); : Is just what happens if this "If" statement is added.

0xff0000ff is the color of the text. It stands for 0XRRGGBBAA (red red green green blue blue Alpha Alpha). If you know basic hex colors. it should be easy for you to understand.


Your code should now look like this. If not read the tutorial again.

pawn Code:
CMD:veh(playerid,params[])
{
new car;
new string[128];
new Float:X, Float:Y, Float:Z;
GetPlayerPos(playerid, Float:X, Float:Y, Float:Z);
if(sscanf(params,"i", car)) return SendClientMessage(playerid,0xff0000ff,"USAGE: /Veh <Vehicle ID 400 - 611>");
return 1;
}
We need to add an IF statement to stop the player from using a id that does not exist.
So in order to do that we need to use SSCANF again.

Add this line under the last line we did

pawn Code:
else if(car < 400 || car >611) return SendClientMessage(playerid, 0xff0000ff, "ERROR: Cannot go under 400 or above 611.");
Now if you read the tutorial you should know what most of this line does. However there is some new stuff added to it.

car < 400 || car >611 - if the value of "car" (the number we used in the command) is less then 400 or more than 611 it will send the player an error message.

!Remember: The Crocodile eats the biggest number "<"! It reminds me of a crocodile's mouth thats how i remember it.

Your code should now look like:

pawn Code:
CMD:veh(playerid,params[])
{
new car;
new string[128];
new Float:X, Float:Y, Float:Z;
GetPlayerPos(playerid, Float:X, Float:Y, Float:Z);
if(sscanf(params,"i", car)) return SendClientMessage(playerid,0xff0000ff,"USAGE: /Veh <Vehicle ID 400 - 611>");
else if(car < 400 || car >611) return SendClientMessage(playerid, 0xff0000ff, "ERROR: Cannot go under 400 or above 611.");
return 1;
}

But what about that lonely variable at the top? Dont worry we haven't forgot about him!

He is used to make sure when we use the command a second time, he removes the previous vehicle!

So after the previous line add an open bracket " { " and insert a simple if statement:

pawn Code:
{
if(Vehicle[playerid] != 0)
    {
        DestroyVehicle(Vehicle[playerid]);
    }
}
if the "Vehicle" variable is anything BUT 0 when this command is used. It will destroy the vehicle spawned by it.

So now the command should look like:

pawn Code:
CMD:veh(playerid,params[])
{
new car;
new string[128];
new Float:X, Float:Y, Float:Z;
GetPlayerPos(playerid, Float:X, Float:Y, Float:Z);
if(sscanf(params,"i", car)) return SendClientMessage(playerid,0xff0000ff,"USAGE: /Veh <Vehicle ID 400 - 611>");
else if(car < 400 || car >611) return SendClientMessage(playerid, 0xff0000ff, "ERROR: Cannot go under 400 or above 611.");
{
if(Vehicle[playerid] != 0)
    {
        DestroyVehicle(Vehicle[playerid]);
    }
}
return 1;
}
Now to create the vehicle (yay!)

We need a "CreateVehicle" line. BUT how does the script know which car was created by the script? Well we use this!:

pawn Code:
Vehicle[playerid] = CreateVehicle(car, X, Y, Z + 3.0, 0, -1, -1, 1);
Now the variable has finally be assigned with something!
In the CreateVehicle code. you may have noticed we have not inserted a vehicle ID. Well that is because it will get its ID from the "i" or integer we used earlier on.

The X Y Z will get the data from the Floats in order to spawn the car at your location but the Z + 3.0 will stop the car spawning in other cars and creating a mess/ It just makes it spawn 3 above the player.

Now it should look almost complete:

pawn Code:
CMD:veh(playerid,params[])
{
new car;
new string[128];
new Float:X, Float:Y, Float:Z;
GetPlayerPos(playerid, Float:X, Float:Y, Float:Z);
if(sscanf(params,"i", car)) return SendClientMessage(playerid,0xff0000ff,"USAGE: /Veh <Vehicle ID 400 - 611>");
else if(car < 400 || car >611) return SendClientMessage(playerid, 0xff0000ff, "ERROR: Cannot go under 400 or above 611.");
{
if(Vehicle[playerid] != 0)
    {
        DestroyVehicle(Vehicle[playerid]);
    }
    Vehicle[playerid] = CreateVehicle(car, X, Y, Z + 2.0, 0, -1, -1, 1);
}
return 1;
}
Ah but we need some fancy text!

Using the String variable we created above we can make it look sexier (or more professional).

Use this code here:
pawn Code:
format(string,sizeof(string),"You Have Spawned Vehicle ID %i",car);
Basically this is assigning the string with that text. The "%i" will only work if we tell it what its going to show. So we add the name of the variable after the text. We will just use "car".

Dont forget to add a sendclientmessage!

it should look like:

pawn Code:
SendClientMessage(playerid, 0xffffffff, string);
The text will be what we just did in the line above.

Your almost finished code will look like:

pawn Code:
CMD:veh(playerid,params[])
{
new car;
new string[128];
new Float:X, Float:Y, Float:Z;
GetPlayerPos(playerid, Float:X, Float:Y, Float:Z);
if(sscanf(params,"i", car)) return SendClientMessage(playerid,0xff0000ff,"USAGE: /Veh <Vehicle ID 400 - 611>");
else if(car < 400 || car >611) return SendClientMessage(playerid, 0xff0000ff, "ERROR: Cannot go under 400 or above 611.");
{
if(Vehicle[playerid] != 0)
    {
        DestroyVehicle(Vehicle[playerid]);
    }
    Vehicle[playerid] = CreateVehicle(car, X, Y, Z + 2.0, 0, -1, -1, 1);
    format(string,sizeof(string),"You Have Spawned Vehicle ID %i",car);
    SendClientMessage(playerid, 0xffffffff, string);
}
return 1;
}
The script will work now BUT lets make it better.
Now how about allowing the player to be put into the car that was just spawned. It can save a lot of... seconds?

Anyway. Just add the tiny tine line of code at the end.

pawn Code:
PutPlayerInVehicle(playerid, Vehicle[playerid], 0);
You should know what it means now. If not, read the bloody tutorial! >:L
The 0 is just the seat id.

And now to close the command.

It should look when completed:

pawn Code:
CMD:veh(playerid,params[])
{
new car;
new string[128];
new Float:X, Float:Y, Float:Z;
GetPlayerPos(playerid, Float:X, Float:Y, Float:Z);
if(sscanf(params,"i", car)) return SendClientMessage(playerid,0xff0000ff,"USAGE: /Veh <Vehicle ID 400 - 611>");
else if(car < 400 || car >611) return SendClientMessage(playerid, 0xff0000ff, "ERROR: Cannot go under 400 or above 611.");
{
if(Vehicle[playerid] != 0)
    {
        DestroyVehicle(Vehicle[playerid]);
    }
    Vehicle[playerid] = CreateVehicle(car, X, Y, Z + 2.0, 0, -1, -1, 1);
    format(string,sizeof(string),"You Have Spawned Vehicle ID %i",car);
    SendClientMessage(playerid, 0xffffffff, string);
    PutPlayerInVehicle(playerid, Vehicle[playerid], 0);
}
return 1;
}
Congratulations. You just created a car spawn script. Kudos!


Now you know the basics of sscanf you could be able to make a /setskin command!


Credits: Zeex And ****** and the SAMP Forums for helping me to learn pawno and spread the information in an easy step by step guide!
Reply


Messages In This Thread
Simple Car Spawn Command - by AaronFarley - 05.11.2013, 14:01
Re: Simple Car Spawn Command - by CrazyChoco - 05.11.2013, 16:09
Re: Simple Car Spawn Command - by AaronFarley - 05.11.2013, 16:33
Re: Simple Car Spawn Command - by Jimmy0wns - 05.11.2013, 16:37
Re: Simple Car Spawn Command - by CrazyChoco - 05.11.2013, 16:55
Re: Simple Car Spawn Command - by AnonScripter - 08.11.2013, 00:52
Re: Simple Car Spawn Command - by Marcynka - 25.11.2013, 21:06
Re: Simple Car Spawn Command - by ReD_DeVi - 26.11.2013, 16:25
Re: Simple Car Spawn Command - by Avi Raj - 24.02.2014, 02:48
Re: Simple Car Spawn Command - by DownDuckling - 20.10.2014, 23:38

Forum Jump:


Users browsing this thread: 1 Guest(s)