SA-MP Forums Archive
[Tutorial] Random Function - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Random Function (/showthread.php?tid=429485)



Random Function - Private200 - 09.04.2013

Hello fellow Sa;Mp members.

I've seen a lot of members PM-ing me, asking me how to make random timers or things like that. First, before we go on explaining everything required here, just see if you got the requirements below:

Skills: 4-5/10 [I ain't good on voting]
Time: 10-20Minutes
Pawno time experience: 1 week - 1 month
Includes number: 1
Includes name: a_samp


So? You got these requirements? If yes, then, let's go!

First, we've got to use news on top of the script so we will be allowed to define what will change. So:

pawn Код:
// This code goes on top of the script, wherever there.

new Randoming;
That's it. We just made the first step of the tutorial. Now, we have to use the switch function. Since this tutorial explains it in every case we use it, I'm just going to show part of spawning the player, which can be used for other things except this.

SWITCH function is being used to switch between the cases we're going to use. Let's see below.
pawn Код:
// This code is going to be under OnPlayerSpawn.

public OnPlayerSpawn(playerid)
{
     Randoming = random(2); // ty for reminding O_x
     switch(Randoming)
     {
          case 0: SetPlayerPos(playerid, X, Y, Z); // Instead of this, here, you can use another function or just use more of 1 function. Example will be shown below.
          case 1: SetPlayerPos(playerid, X, Y, Z);
      }
}
This part was showing only 1 function, but, I'm going to show how to use more functions instead.

pawn Код:
public OnPlayerSpawn(playerid)
{
      Randoming = random(2);
      switch(Randoming)
      {
             case 0:
             {
                    SetPlayerPos(playerid, X, Y, Z); // This will spawn the player and will give him 100 money. You can use more functions here instead of this.
                    GivePlayerMoney(playerid, 100);
              }
              case 1:
              {
                    SetPlayerPos(playerid, X, Y, Z);
                    GivePlayerMoney(playerid, 200);
              }
       }
}
That's the part of when player spawns, but, it's something random. We can use other things instead of things, as there are houndred and houndred of combinations to use.

Let's see a full part of the code.

pawn Код:
// On top of the script

#include <a_samp>

new Randoming;

// Under OnPlayerSpawn or everywhere you want to make random things.
Randoming = random(2);

switch(Randoming)
{
      case 0: // Function here
      case 1: // Function here
}
That's all guys. You're done. Why not test it? If you're having any problem with it, please leave comment below.



Re: Random Function - [XST]O_x - 09.04.2013

Nice of you to write this tutorial, but a couple of things:
Firstly, you didn't actually randomize anything - you just have a variable called Randoming, which contains junk because you didn't give it any value, and you're trying to switch its value - but it doesn't have any, so you'll have to add this:
pawn Код:
new Randoming;

public OnPlayerSpawn(playerid)
{
    Randoming = random(2);
    switch(Randoming)
    {
        //
    }
    return 1;
}
Second thing is, you can actually make this far easier with two-dimensional arrays:

pawn Код:
new randstuff[2][4] = {
{float: 1305.25, float: 524.19, float: 15.04, 100},
{float: 1240.61, float: 1351.35, float: 12.54, 200}
};

public OnPlayerSpawn(playerid)
{
    new RandomIze = random(sizeof(randstuff));
    SetPlayerPos(playerid, randstuff[RandomIze][0], randstuff[RandomIze][1], randstuff[RandomIze][2]);
    GivePlayerMoney(playerid, randstuff[RandomIze][3]);
    return 1;
}
This will actually save you up a lot of writing space.


Re: Random Function - Private200 - 09.04.2013

Yea, true, I forgot about the first. About the second, the tutorial is for newbies, so, i think it will make things harder.