[Tutorial] Random spawns
#1

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:
  1. Go ingame.
  2. Go to the location where you want one of the spawns to be.
  3. Type "/save" (without the " ")
  4. 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[][] =
{
};
  1. new is the default word to indicate the creation of a new variable, array, ....
  2. Float indicates that the data inside the array will be Floats.
  3. 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!)
  4. 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.
  5. 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)
  6. 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;
}
  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.
  2. SetPlayerPos: Click me
  3. RandomSpawns[Random][0] stands for the x coцrdinate that coresponds with the number that our variable "Random" contains.
  4. RandomSpawns[Random][1] stands for the y coordinate.
  5. RandomSpawns[Random][2] stands for the z coordinate.
  6. SetPlayerFacingAngle: Click me
  7. RandomSpawns[Random][3] stands for the angle.


Final words:
Congrats! You have created a random spawn and the given information should make it able for you to change whatever you want without problems.

If you find any mistakes, please post them in a comment!

Thanks for reading,
FUNExtreme
Reply
#2

prefect for new players. good job.
Reply
#3

Quote:
Originally Posted by Kar
View Post
prefect for new players. good job.
Thank you, thats what it is meant for.
Reply
#4

Nice tutorial. This is not one of those "How to make account database? Simply copy-paste this code in your script *code here*"-tutorials.

Well done.
Reply
#5

Nice it will help many new scripters... Well built topic good job
Reply
#6

Quote:
Originally Posted by Finn
View Post
Nice tutorial. This is not one of those "How to make account database? Simply copy-paste this code in your script *code here*"-tutorials.

Well done.
I find copy-paste tutorials pretty useless just like you, Thank you

Quote:
Originally Posted by tour15
View Post
Nice it will help many new scripters... Well built topic good job
Thank you


Does anyone know if it is possible to change the topic title? I noticed that there is allready a tutorial (copy-paste) with the same name.
Reply
#7

I tried it to my topic but it didnt work... Try contact a moderator
Reply
#8

Great tutorial, short, useful and explains everything.

Just one thing you forgot, to add ';' at new Random = random(5).
Reply
#9

Great tut

But use this, simple:
Put under onplayerspawn:
Quote:

new RandomSpawn1 = random(4);
if (RandomSpawn1 == 0)
{
SetPlayerPos(playerid,3922.5779,-1608.7214,1441.8087);
SendClientMessage(playerid,COLOR_RED,"You spawned at Sky Road (/skroad)");
}
if (RandomSpawn1 == 1)
{
SetPlayerPos(playerid, 284.668945, 2470.728515, 16.474884);
SendClientMessage(playerid,COLOR_RED,"You spawned at aa. Old Airport (/aa)");
}
if (RandomSpawn1 == 2)
{
SetPlayerPos(playerid,1549.9720,-1675.3169,15.2003);
SendClientMessage(playerid,COLOR_RED,"You spawned at San Fierro (/sf)");
}

if (RandomSpawn1 == 3)
{
SetPlayerPos(playerid,1549.9720,-1675.3169,15.2003);
SendClientMessage(playerid,COLOR_RED,"You spawned at Los Santos (/ls)");
}

Reply
#10

Here's another simple way of randoming spawns :
pawn Code:
switch(random(4))
{
  case 0:
  {
   SetPlayerPos(playerid,3922.5779,-1608.7214,1441.8087);
   SendClientMessage(playerid,COLOR_RED,"You spawned at Sky Road (/skroad)");
  }
  case 1:
  {
   SetPlayerPos(playerid, 284.668945, 2470.728515, 16.474884);
   SendClientMessage(playerid,COLOR_RED,"You spawned at aa. Old Airport (/aa)");
  }
  case 2:
  {
   SetPlayerPos(playerid,1549.9720,-1675.3169,15.2003);
   SendClientMessage(playerid,COLOR_RED,"You spawned at San Fierro (/sf)");
  }
  case 3:
  {
   SetPlayerPos(playerid,1549.9720,-1675.3169,15.2003);
   SendClientMessage(playerid,COLOR_RED,"You spawned at Los Santos (/ls)");
  }
}
anyways, nice tut =)
Reply
#11

@the 2 posts above:
Adding spawns in your method will take much more time then adding them in my method.
+ it will also be a much longer code.

Thanks for the comments!!
Reply
#12

nice tut
Reply
#13

Nice,its useful
Reply
#14

Nice tut! great for noobs
Reply
#15

You should replace "C:\Program Files\Rockstar Games\GTA San Andreas\savedpositions.txt" to "My Documents\GTA San Andreas User Files\SAMP\savedpositions.txt" because since 03b it has changed. (:
Reply
#16

UPDATE:

- Changed the coordinates saving location
- Made a small adjustement too the spawning code. Replaced 5 with sizeof(RandomSpawns)
Reply
#17

Nice work! This will help new scripters
Reply
#18

Sorry for the bump but...when Im trying to compile I get this error and I did exactly as you explained.

Code:
TDM.pwn(516) : error 017: undefined symbol "RandomPlayerSpawns"
TDM.pwn(516) : error 029: invalid expression, assumed zero
TDM.pwn(516) : warning 215: expression has no effect
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB
My code (from what you explained)

This is outside any function.
Code:
new Float:RandomSpawns[][] =
{
    {2319.2280,-1651.7455,14.1590,143.4542}, // Randomspawngrove
    {2175.5120,-1672.8800,16.5499,116.5022}, // Randomspawnballas
    {1920.5571,-1401.1272,13.5703,302.2050}, // Randomspawnskatepark
    {2005.1017,-2437.9197,13.5469,358.3744}, // Randomspawnlsair
    {-2302.4067,-1654.1744,483.4206,295.6445}, // Randomspawnchilliad
    (1695.7886,-2133.8210,144.4791,340.6445), //Randomspawnbigjump
    (1240.2787,-2951.0051,193.1163,340.6445) //Randomspawnstuntjump
};
And OnPlayerSpawn
Code:
public OnPlayerSpawn(playerid)
{
((this is line 516))new Random = random(sizeof(RandomPlayerSpawns));
    SetPlayerPos(playerid, RandomSpawns[Random][0], RandomSpawns[Random][1], RandomSpawns[Random][2]);
    SetPlayerFacingAngle(playerid, RandomSpawns[Random][3]);
    return 1;
}
??
Reply
#19

Your float is defined as "RandomSpawns"
but in the random you use "RandomPlayerSpawns"

pawn Code:
new Random = random(sizeof(RandomSpawns));

I updated the tutorial
Reply
#20

Lol I didn't see that , thank you.

Btw hell'of a fast response for a 2 month old thread lmfao!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)