[Tutorial] Random gifts for the Christmas [SYSTEM TUTORIAL]
#1

Hi, in this tutorial you will learn:

- How to use cases command
- How to develop/upgrade them.
- How to use range function.


Before going on, I wont explain how to create the Christmas box, since that's only a creation of object in the coordinates you get by /save in-game.

So, let's start over.

In order to get cases, we must make a function that will go through those cases and will stop on a random one. To do this, we must define this function on top of our script (Under includes part):

pawn Код:
new Random;
That's it, we are done with the top of the script. Was that hard? Not huh?...

Now let's move over. In my case, I'm going to use zCMD include, so, on top of the script you must include it.

pawn Код:
#include <zcmd>
Till now we got this code:

pawn Код:
#include <a_samp>
#include <zcmd>

new Random;
Now we are going to the part of creating the command for our system. First of all, we create the command without adding any function on it. The conclusion must be something like this:

pawn Код:
COMMAND:gifts(playerid, params[])
{
     // code here
     return 1;
}
Now, after we have created the command, we must remove the "//code here" part, so we can put the functions that are going to be ran when we use the command.

We shall see if the player is in range of the gift place, so we are going to use the "IsPlayerInRangeOfPoint" function. Let's see below how it will seem:

pawn Код:
COMMAND:gifts(playerid, params[])
{
     if(IsPlayerInRangeOfPoint(playerid, Range, X, Y, Z))
     {
            // Code here
     }
     return 1;
}
Now, let me explain what those parameters stand for.

"playerid" => This stands for the ID of player. With it you can define to who the command is working, in our case, the command will work for the player that is using it.
"Range" => Range stands for the range that the player is from the coordinates.
"X" => Stands for the X location of user.
"Y" => Stands for the Y location of user.
"Z" => Stands for the Z location of user.

Let's go further to the tutorial. Now we must define the cases and the random function also. The plain text without command will be something like this:

pawn Код:
Random = random(3);
     switch(Random)
     {
          case 0:
          case 1:
          case 2:
      }
Each of those cases will be filled with our functions. Let's add this part of code to our command.

pawn Код:
COMMAND:gifts(playerid, params[])
{
     if(IsPlayerInRangeOfPoint(playerid, Range, X, Y, Z))
     {
            Random = random(3);
            switch(Random)
            {
                  case 0:
                  case 1:
                  case 2:
            }
     }
     return 1;
}
If you want to proceed by your self, this is the progress of our script till now:

pawn Код:
#include <a_samp>
#include <zcmd>

new Random;


public OnFilterScriptInIt()
{
      return 1;
}

public OnFilterScriptExit()
{
      return 1;
}

COMMAND:gifts(playerid, params[])
{
     if(IsPlayerInRangeOfPoint(playerid, Range, X, Y, Z))
     {
            Random = random(3);
            switch(Random)
            {
                  case 0:
                  case 1:
                  case 2:
            }
     }
     return 1;
}
Now, let's add some gifts for our users.

pawn Код:
COMMAND:gifts(playerid, params[])
{
     if(IsPlayerInRangeOfPoint(playerid, Range, X, Y, Z))
     {
            Random = random(3);
            switch(Random)
            {
                  case 0: // Let's add cash!
                  {
                          GivePlayerCash(playerid, 5000);
                          SendClientMessage(playerid, -1, "Santa Claus gifted you 5000$. Merry Christmas!");
                  }
                  case 1: // Let's add some score to the user!
                  {
                          SetPlayerScore(playerid, GetPlayerScore(playerid)+15);
                          SendClientMessage(playerid, -1, "Santa Claus gifted you 15 score. Merry Christmas!");
                  }
                  case 2: // Let's gift him nothing, he was a bad boy this year.
                  {
                          SendClientMessage(playerid, -1, "You were in Santa Claus blacklist and he didn't even pass over your house. Be a good guy next year");
                  }
            }
     }
     return 1;
}
Now that we added the command, the conclusion of this will be something like this:

pawn Код:
#include <a_samp>
#include <zcmd>

new Random;


public OnFilterScriptInIt()
{
      return 1;
}

public OnFilterScriptExit()
{
      return 1;
}

COMMAND:gifts(playerid, params[])
{
     if(IsPlayerInRangeOfPoint(playerid, Range, X, Y, Z))
     {
            Random = random(3);
            switch(Random)
            {
                  case 0: // Let's add cash!
                  {
                          GivePlayerCash(playerid, 5000);
                          SendClientMessage(playerid, -1, "Santa Claus gifted you 5000$. Merry Christmas!");
                  }
                  case 1: // Let's add some score to the user!
                  {
                          SetPlayerScore(playerid, GetPlayerScore(playerid)+15);
                          SendClientMessage(playerid, -1, "Santa Claus gifted you 15 score. Merry Christmas!");
                  }
                  case 2: // Let's gift him nothing, he was a bad boy this year.
                  {
                          SendClientMessage(playerid, -1, "You were in Santa Claus blacklist and he didn't even pass over your house. Be a good guy next year");
                  }
            }
     }
     return 1;
}
You can also add more features by your self and add more gifts for the user. This is a simple tutorial where you can start off from your huge ideas about Christmas! This is my first tutorial, so please do not think bad about me

Cheers!
Reply
#2

There must be some restrictions ... it can be abused by players by spamming "/gifts" ...

pawn Код:
#include <a_samp>
#include <zcmd>

new Random;
new GotGifts[MAX_PLAYERS];

public OnPlayerConnect(playerid);
{
     GotGifts[playerid] =0; // resets variable
     return 1;
}

public OnFilterScriptInIt()
{
      return 1;
}

public OnFilterScriptExit()
{
      return 1;
}

COMMAND:gifts(playerid, params[])
{
     if(GotGifts[playerid] == 1) return SendClientMessage(playerid, -1, "* You already have got your gift."); // gives error message if he got gift already...
     if(IsPlayerInRangeOfPoint(playerid, Range, X, Y, Z))
     {
            GotGifts[playerid] =1; // "1" means he got gift.
            Random = random(3);
            switch(Random)
            {
                  case 0: // Let's add cash!
                  {
                          GivePlayerCash(playerid, 5000);
                          SendClientMessage(playerid, -1, "Santa Claus gifted you 5000$. Merry Christmas!");
                  }
                  case 1: // Let's add some score to the user!
                  {
                          SetPlayerScore(playerid, GetPlayerScore(playerid)+15);
                          SendClientMessage(playerid, -1, "Santa Claus gifted you 15 score. Merry Christmas!");
                  }
                  case 2: // Let's gift him nothing, he was a bad boy this year.
                  {
                          SendClientMessage(playerid, -1, "You were in Santa Claus blacklist and he didn't even pass over your house. Be a good guy next year");
                  }
            }
     }
     return 1;
}
Reply
#3

I know that it might be abused and I though to update it by using a timer to prevent it getting abused. Means, something like a timer for a hour so players must wait a hour to get gift.
Reply
#4

Why do you even use a global variable?

pawn Код:
switch(random(3))
Reply
#5

Quote:
Originally Posted by iPrivate
Посмотреть сообщение
I know that it might be abused and I though to update it by using a timer to prevent it getting abused. Means, something like a timer for a hour so players must wait a hour to get gift.
A few minutes should do the trick, but don't make a 1 hour long timer for that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)