05.11.2013, 14:01
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: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.
You should see this at the start of the Gamemode Or Filterscript
pawn Code:
#include <a_samp>
pawn Code:
#include <zcmd>
Info: ZCMD is one of or the fastest command processor. Its also easier to understand.
pawn Code:
#include <sscanf2>
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];
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[])
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!
pawn Code:
CMD:veh(playerid, params[])
{
new car;
new string[128];
new Float:X, Float:Y, Float:Z;
return 1;
}
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);
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;
}
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>");
pawn Code:
if(sscanf(params,"i", car)) return SendClientMessage(playerid,0xff0000ff,"USAGE: /Veh <Vehicle ID 400 - 611>");
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;
}
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.");
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]);
}
}
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;
}
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);
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;
}
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);
Dont forget to add a sendclientmessage!
it should look like:
pawn Code:
SendClientMessage(playerid, 0xffffffff, string);
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;
}
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);
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;
}
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!