Creating random spawns


Introduction:
Welcome to my step to step tutorial on how to create random spawns. This tutorial is created as if I were completly new at scripting pawn, in this way I make the tutorial understandable for all types of users, from Noobs to Pro's.


The tutorial:

Step 1: Getting the coordinates
To create random spawns we offcourse need to get coordinates wich we will use for our random spawns, This is the easy way to get these:
Go ingame.
Go to the location where you want one of the spawns to be.
Type "/save" (without the " ")
Repeat above steps until you have all your spawns.
Note: It is important that your character is facing the way you want them to spawn. It is pretty annoying when your character spawns facing a wall!
Note2: I advise you to add a word behind the "/save" so that you will easily know wich ones are for the random spawns.
(Example: /save randomspawn)


Step 2: Finding the coordinates
In step 1 we have saved our spawns and now it is time to get them out of the default file where the "/save" command saves the positions.

Navigate too
Code:
...\My Documents\GTA San Andreas User Files\SAMP
Search the file
Code:
savedpositions.txt
And open it.

Now search through the file until you find your positions wich are marked with whatever word you added to your "/save" command (In the tutorial this will be randomspawn).


Step 3: Converting the coordinates
In step 2 we have located the positions, now we'll have to make them fit into our array.

IMPORTANT NOTE: This tool can convert the coordinates automaticly!! Thanks to JaTochNietDan

Copy all your spawn point positions and paste them inside your gamemode (or wherever you want the random spawns to be)
Code:
AddPlayerClass(101,1249.7258,-2047.9263,59.9209,90.2055,0,0,0,0,0,0); // Randomspawn
AddPlayerClass(101,1241.2084,-2057.6521,60.0190,94.9352,0,0,0,0,0,0); // Randomspawn
AddPlayerClass(101,1241.0105,-2052.6873,59.9975,2.8144,0,0,0,0,0,0); // Randomspawn
AddPlayerClass(101,718.4906,-1477.3024,5.4688,357.9947,0,0,0,0,0,0); // Randomspawn
AddPlayerClass(101,722.3772,-1477.2856,5.4688,272.3814,0,0,0,0,0,0); // Randomspawn
This is what we have, but we will need to convert this to an array. But first!
What is what?

Code:
AddPlayerClass(skinid, x, y, z, angle, weapon1, weapon1_ammo, weapon2, weapon2_ammo, weapon3, weapon3_ammo)
Note: Check the wiki for further information: Click me

For our random spawns we will only need x, y, z coordinates and the angle.
Code:
1249.7258, -2047.9263, 59.9209, 90.2055 // Randomspawn
1241.2084, -2057.6521, 60.0190, 94.9352 // Randomspawn
1241.0105, -2052.6873, 59.9975, 2.8144 // Randomspawn
718.4906, -1477.3024, 5.4688, 357.9947 // Randomspawn
722.3772, -1477.2856, 5.4688, 272.3814// Randomspawn
Now that we have extracted the x, y, z coordinates and the angle out of the AddPlayerClass function we can easily convert that into an array!

pawn Code:
new Float:RandomSpawns[][] =
{
{1249.7258, -2047.9263, 59.9209, 90.2055}, // Randomspawn
{1241.2084, -2057.6521, 60.0190, 94.9352}, // Randomspawn
{1241.0105, -2052.6873, 59.9975, 2.8144}, // Randomspawn
{718.4906, -1477.3024, 5.4688, 357.9947}, // Randomspawn
{722.3772, -1477.2856, 5.4688, 272.3814} // Randomspawn
};
Important: The array should be located outside any function!!
Wow, what is this?
Time to explain don't you think:

pawn Code:
new Float:RandomSpawns[][] =
{
};
new is the default word to indicate the creation of a new variable, array, ....
Float indicates that the data inside the array will be Floats.
RandomSpawns is the name of the array (You may change this if you want but keep in mind to change it everywhere we will use it!)
The first [] is left blank so that we don't have to change the number when we add more spawns, this basicly allows unlimited data.
The second [] is left blank so that we don't have to change the number when we want to add more data to a spawn. (Example: x, y, z, angle, skin, weapon)
The rest of the code should be pretty easy to understand.

pawn Code:
{1249.7258, -2047.9263, 59.9209, 90.2055}, // Randomspawn
Code:
{ x , y , z , angle }, //Randomspawn
Note: The last line does not contain a "," at the end wich indicates that this is the last line in the array.


Step 4: Creating the spawn code
In the previous steps we have done everything containing coordinates, now it is time to use them!

The array has to be in the same script as where we will put this piece of code!

Find
pawn Code:
public OnPlayerSpawn(playerid)
{
return 1;
}

Once you found this function it is time to create the actual random spawning.
pawn Code:
public OnPlayerSpawn(playerid)
{
new Random = random(sizeof(RandomSpawns));
SetPlayerPos(playerid, RandomSpawns[Random][0], RandomSpawns[Random][1], RandomSpawns[Random][2]);
SetPlayerFacingAngle(playerid, RandomSpawns[Random][3]);
return 1;
}
random(sizeof(RandomSpawns)) is a function wich randomly picks a number out of a given amount. The sizeof(RandomPlayerSpawns) automaticly outputs the amount of data inside the array, in this example this is 5.
SetPlayerPos: Click me
RandomSpawns[Random][0] stands for the x coördinate that coresponds with the number that our variable "Random" contains.
RandomSpawns[Random][1] stands for the y coordinate.
RandomSpawns[Random][2] stands for the z coordinate.
SetPlayerFacingAngle: Click me
RandomSpawns[Random][3] stands for the angle.