SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 05/11/2013, 02:01 PM   #1
AaronFarley
Big Clucker
 
AaronFarley's Avatar
 
Join Date: Aug 2012
Location: Pawn Folder on Pilot's Life
Posts: 125
Reputation: 20
Post Simple Car Spawn Command

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: http://forum.sa-mp.com/showthread.php?t=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:http://forum.sa-mp.com/showthread.php?t=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!
__________________
Retired from Samp.

2011-2019
AaronFarley is offline   Reply With Quote
Old 05/11/2013, 04:09 PM   #2
CrazyChoco
Gangsta
 
Join Date: Feb 2012
Posts: 895
Reputation: 35
Default Re: Simple Car Spawn Command

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!
CrazyChoco is offline   Reply With Quote
Old 05/11/2013, 04:33 PM   #3
AaronFarley
Big Clucker
 
AaronFarley's Avatar
 
Join Date: Aug 2012
Location: Pawn Folder on Pilot's Life
Posts: 125
Reputation: 20
Default Re: Simple Car Spawn Command

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.
__________________
Retired from Samp.

2011-2019
AaronFarley is offline   Reply With Quote
Old 05/11/2013, 04:37 PM   #4
Jimmy0wns
Gangsta
 
Join Date: Sep 2012
Location: Netherlands
Posts: 686
Reputation: 208
Default Re: Simple Car Spawn Command

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.
Jimmy0wns is offline   Reply With Quote
Old 05/11/2013, 04:55 PM   #5
CrazyChoco
Gangsta
 
Join Date: Feb 2012
Posts: 895
Reputation: 35
Default Re: Simple Car Spawn Command

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. http://wiki.sa-mp.com/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.
CrazyChoco is offline   Reply With Quote
Old 08/11/2013, 12:52 AM   #6
AnonScripter
Gangsta
 
Join Date: Sep 2013
Posts: 591
Reputation: 6
Default Re: Simple Car Spawn Command

boring tutorial to read, why not to make the full command from the first time and explain using comments
pawn Code:
//your comment here
AnonScripter is offline   Reply With Quote
Old 25/11/2013, 09:06 PM   #7
Marcynka
Little Clucker
 
Marcynka's Avatar
 
Join Date: Aug 2012
Posts: 22
Reputation: 0
Default Re: Simple Car Spawn Command

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?
Marcynka is offline   Reply With Quote
Old 26/11/2013, 04:25 PM   #8
ReD_DeVi
Banned
 
Join Date: Aug 2012
Location: In Bed Room
Posts: 518
Reputation: 34
Default Re: Simple Car Spawn Command

Not bad anyway good
ReD_DeVi is offline   Reply With Quote
Old 24/02/2014, 02:48 AM   #9
Avi Raj
Banned
 
Join Date: Dec 2012
Location: Proud Indian
Posts: 772
Reputation: 126
Default Re: Simple Car Spawn Command

Its good for the new scripters.
It have some problems but still, it was your first attempt.
Keep it up, mate.
Avi Raj is offline   Reply With Quote
Old 20/10/2014, 11:38 PM   #10
DownDuckling
Big Clucker
 
Join Date: Nov 2011
Location: Southern California
Posts: 82
Reputation: 2
Question Re: Simple Car Spawn Command

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

My variable is:
Code:
if(PlayerInfo[playerid][pAdmin] >= )
__________________
Like a boss.
__________________________________________________ ____
My PC Specs: Intel Core i7 4790k @4.5
MOBO GIGABYTE Z97X-UD5H-BK
RAM Crucial Ballistix Sport 8 GB DDR3 RAM
GIGABYTE GTX 980 G1 Gaming
Case is a Corsair Vengeance C70 OD Green.
Damned H110 Broke Using a Hyper 212 EVO for now
__________________________________________________ ____
DownDuckling is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[FilterScript] Simple Repair and Heal command (Simple for newbs) kingcrome Filterscripts 4 01/11/2013 12:16 AM
[Tool/Web/Other] Vehicle spawn generator ( Very Simple ) _chimera_ Tools and Files 5 23/06/2013 08:01 PM
simple NPC doesn't spawn HurtLocker Scripting Help 6 11/04/2013 08:47 PM
Simple Spawn Creation Fierros Scripting Help 1 19/10/2012 04:16 AM
Simple command to spawn vehicle Freddy Z Help Archive 0 07/01/2010 02:47 PM


All times are GMT. The time now is 01:37 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.