[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
#2

Why do you have global variables, when they are only used locally? Change it.
This wasn't a tutorial, but rather a Copy & Paste topic, but with some tiny explanation. In my opinion this can be improved a lot!
Reply
#3

I havnt made a mistake. Im going to build on this tutorial and add some features for the vehicle. Perhaps teh ability to teleport the vehicle to you. It will be added when im done testing them.
Maybe I should of made that clear.

Anyway, the script isnt a copy and paste from a gamemode, its something ive scripted myself.
Ive tried to explain each step to people. And if the reader wishes to skip through and not read it. Thats their problem.

However, I thank you for you comments.
Reply
#4

Quote:
Originally Posted by AaronFarley
View Post
I havnt made a mistake. Im going to build on this tutorial and add some features for the vehicle. Perhaps teh ability to teleport the vehicle to you. It will be added when im done testing them.
Maybe I should of made that clear.

Anyway, the script isnt a copy and paste from a gamemode, its something ive scripted myself.
Ive tried to explain each step to people. And if the reader wishes to skip through and not read it. Thats their problem.

However, I thank you for you comments.
Hey Aaron, didnt notice you being on this forums too -Zumorio

OT: Nice tutorial, you could try to improve it by using vehicle names.
Reply
#5

First: How can you judge if your script is correctly or not? I just asked you, why did you create some global and local variables while your "tutorial" is using it locally? It just doesn't make any sense.
Anyway I read your tutorial, but you are not even going into subject. This is what you do: -Anyway. Just add the tiny tine line of code at the end.
That was an examply, and after that line, you identicated to a code. If i must admit it. A tutorial must not even have that much code. A tutorial is explanation of the function or command, not "paste this line & then it just happends"

Anyway this is your example:
pawn Code:
PutPlayerInVehicle(playerid, Vehicle[playerid], 0);
What you could've done to make it as a tutorial example:
Now we need to put the player inside the vehicle, but how? We could try to get the positions of the player and then try to put him into the vehicle? No it will be to much hassle, instead we could use this function which i got from the wiki: PutPlayerInVehicle(playerid,vehicleid,seatid);
That function would be useful right? Since this function is created to put the player inside the vehicle & our variables will also be in use too. Let me explain. Who do we want to be inside the vehicle? The playerid ofcourse. Which vehicleid is it? Well, we don't know it, but since we have defined a Variable which get's the specified vehicle id, we could use it. In this case: Vehicles[playerid], the [playerid] is used so, the variable doesn't apply for all the players around the server. But only for the specified player. Seat id. We could have made a command which was PutPlayerInVehicle <vehicle> <seat id>. But we didn't. So we just want the playerid to be in the driver seat. How do we do that? That would be done, by changing it to 0. The 0 is stated as the driver seat, and if you wanted to read more about the seats or the PutPlayerInVehicle function, i suggest you read this page. https://sampwiki.blast.hk/wiki/PutPlayerInVehicle
By this we now know how to put the player into a vehicle, but with explanation.

This is what I mean. That would be described as a tutorial, not the way you did it. But ofcourse examples are accepted, but you are just doing like:

it should look like:

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

That ain't even an explanation. The only reason I posted this was to make it clear the difference between a good and your tutorial. No offense tho.

If you can't explain what each single character does, then you shouldn't even consider writing a tutorial, just a tip.
Reply
#6

boring tutorial to read, why not to make the full command from the first time and explain using comments
pawn Code:
//your comment here
Reply
#7

AaronFarley ignore all bad comments about your tutorial I'm new in scripting and this tutorial explains me really a lot good job on that just I have some problems with my scripting... I followed step by step but I think I made some mistakes and I'm keep getting error on this line

if(Vehicle[playerid] != 0)

error 017: undefined symbol "Vehicle"
warning 215: expression has no effect
error 001: expected token: ";", but found "]"
error 029: invalid expression, assumed zero
fatal error 107: too many error messages on one line


Someone can help me?
Reply
#8

Not bad anyway good
Reply
#9

Its good for the new scripters.
It have some problems but still, it was your first attempt.
Keep it up, mate.
Reply
#10

How would I make it so only admins can spawn cars?

My variable is:
Code:
if(PlayerInfo[playerid][pAdmin] >= )
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)