SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 23/07/2019, 06:39 PM   #1
YouShootIDodge
Little Clucker
 
Join Date: May 2019
Posts: 42
Reputation: 11
Default How To Create Random Spawns!

Creating Random Spawns


So this is a very simple, straight-forward tutorial on how to make random spawns.

For creating random spawns, there are 2 methods i can think of right now. First simple switch () statement and second using milti-dimentional arrays.

There are alot of beginners who are unable to understand the concept of multi-dimensional arrays so in this tutorial i will show them how to make a random spawn system and similar stuff without using the arrays. Since the array method is explained in the wiki, i'll be explaining the second one.

Here is a code showing the OnPlayerSpawn () callback, for-example if you want to spawn the player at one of five random positions, this code will work:

Code:
public OnPlayerSpawn (playerid)
{
    new selected_spawn = random (5);

    switch (selected_spawn)
    {
        case 0: { SetPlayerPos (playerid, 0.0,   0.0,   0.0);  SetPlayerFacingAngle (playerid, 0.0); }
        case 1: { SetPlayerPos (playerid, 10.0, 10.0, 10.0); SetPlayerFacingAngle (playerid, 10.0); }
        case 2: { SetPlayerPos (playerid, 20.0, 20.0, 20.0); SetPlayerFacingAngle (playerid, 20.0); }
        case 3: { SetPlayerPos (playerid, 30.0, 30.0, 30.0); SetPlayerFacingAngle (playerid, 30.0); }
        case 4: { SetPlayerPos (playerid, 40.0, 40.0, 40.0); SetPlayerFacingAngle (playerid, 40.0); }
    }
    return 1;
}
Explained:

1.
Code:
new selected_spawn = random (5);
Here you can see a new integer type variable being created followed by an assignment operator and a function named 'random ()'. This semantic of this line of code works like this:

- It creates an integer variable.
- It then generates a random value between 0 and 4 (5 is the size of its range, since in coding counting starts from 0 so 0, 1, 2, 3, 4 = 5)
- It assigns the randomly generated value to the newly created variable.


2.
Code:
switch (selected_spawn) { }
This is called a switch statement, in simple words it is an alternative to the if-statement with some limitations but it can be used neatly in this case. So it will execute a function or set of functions depending upon the value of variable 'selected_spawn'.

As you can see, we have provided it 2 functions to perform in each case, for-example if the value of 'selected_spawn' turns out to be 1, it will perform the following functions and hence the player's position will be set to the given coordinates.

Code:
switch (selected_spawn)
    {
        case 0: { SetPlayerPos (playerid, 0.0,   0.0,   0.0);  SetPlayerFacingAngle (playerid, 0.0); }
        case 1: { SetPlayerPos (playerid, 10.0, 10.0, 10.0); SetPlayerFacingAngle (playerid, 10.0); }
        case 2: { SetPlayerPos (playerid, 20.0, 20.0, 20.0); SetPlayerFacingAngle (playerid, 20.0); }
        case 3: { SetPlayerPos (playerid, 30.0, 30.0, 30.0); SetPlayerFacingAngle (playerid, 30.0); }
        case 4: { SetPlayerPos (playerid, 40.0, 40.0, 40.0); SetPlayerFacingAngle (playerid, 40.0); }
    }

Now, that was all, if you want to add another random spawn, you can increment the range of the random () function and add it's coordinates in the switch () statement.

You can also make a function out of it, so if you want to set random positions multiple times in the code, it will be useful.

Here is an example code:

Code:
public OnPlayerSpawn (playerid)
{
    SetRandomPosition (playerid); // You can use this multiple times in other parts of code too.
    return 1;
}

stock SetRandomPosition (playerid)
{
    new selected_spawn = random (5);

    switch (selected_spawn)
    {
        case 0: { SetPlayerPos (playerid, 0.0,   0.0,   0.0);  SetPlayerFacingAngle (playerid, 0.0); }
        case 1: { SetPlayerPos (playerid, 10.0, 10.0, 10.0); SetPlayerFacingAngle (playerid, 10.0); }
        case 2: { SetPlayerPos (playerid, 20.0, 20.0, 20.0); SetPlayerFacingAngle (playerid, 20.0); }
        case 3: { SetPlayerPos (playerid, 30.0, 30.0, 30.0); SetPlayerFacingAngle (playerid, 30.0); }
        case 4: { SetPlayerPos (playerid, 40.0, 40.0, 40.0); SetPlayerFacingAngle (playerid, 40.0); }
    }
    return 1;
}
YouShootIDodge is offline   Reply With Quote
Old 24/07/2019, 01:45 PM   #2
Gforcez
Gangsta
 
Gforcez's Avatar
 
Join Date: Jul 2010
Location: The Netherlands.
Posts: 723
Reputation: 390
Default Re: How To Create Random Spawns!

Why not store the positions and looking angles in an array and set spawns based on the array index. It's easier to add positions because you don't need a switch/case, so you don't have to repeat the same code over and over to add more spawn positions.
__________________

Gforcez is offline   Reply With Quote
Old 24/07/2019, 02:06 PM   #3
YouShootIDodge
Little Clucker
 
Join Date: May 2019
Posts: 42
Reputation: 11
Default Re: How To Create Random Spawns!

Quote:
Originally Posted by Gforcez View Post
Why not store the positions and looking angles in an array and set spawns based on the array index. It's easier to add positions because you don't need a switch/case, so you don't have to repeat the same code over and over to add more spawn positions.
Thanks for the response, this tutorial was for the beginners as i said in the above introduction (please read that).

Code:
There are alot of beginners who are unable to understand the concept of multi-dimensional arrays so in this tutorial i will show them how to make a random spawn system and similar stuff without using the arrays. Since the array method is explained in the wiki, i'll be explaining the second one.
YouShootIDodge is offline   Reply With Quote
Old 24/07/2019, 02:23 PM   #4
Gforcez
Gangsta
 
Gforcez's Avatar
 
Join Date: Jul 2010
Location: The Netherlands.
Posts: 723
Reputation: 390
Default Re: How To Create Random Spawns!

Quote:
Originally Posted by YouShootIDodge View Post
Thanks for the response, this tutorial was for the beginners as i said in the above introduction (please read that).

Code:
There are alot of beginners who are unable to understand the concept of multi-dimensional arrays so in this tutorial i will show them how to make a random spawn system and similar stuff without using the arrays. Since the array method is explained in the wiki, i'll be explaining the second one.
My bad, I just looked at the code. Maybe it's a good idea to explain arrays and how they work, and show them how it works with an array. Since you'll be teaching them the best way (without loading spawn positions from a database.).
__________________

Gforcez 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
How would I create random seperate spawns Dinoo Scripting Help 2 26/03/2016 08:21 PM
how do i create weapon spawns johnathon956 Help Archive 9 24/08/2010 09:18 AM
[HELP]How to Create Random Spawns? Tigerbeast11 Help Archive 2 03/02/2010 06:23 AM
[HELP] How can i create random spawns? akis_tze Help Archive 1 14/11/2009 10:53 PM
Random Spawns + Previous Spawns XtremeChio Help Archive 2 01/09/2009 01:57 PM


All times are GMT. The time now is 06:16 AM.


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