23.07.2019, 18:39
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; }
1.
Code:
new selected_spawn = random (5);
- 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) { }
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); } }
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; }