How should I do this?
Hello, I'm working on my giftbox system.
The command /getgift should select a random value between 0 and 10, and give him the random generated gift.
Now I know how to make that command, but I would want it to do the following thing.
* Check if the gift is available, else re-roll till you have an available gift.
So let's say the random generated value is 5.
My code would be like this:
Код:
```CMD:getgift(playerid, params[])
{
new rand = random(10);
if(rand == 5)
{
SendClientMessage(playerid, -1, "You have won \$500,000!");
}
return 1;
}```
But, let's say that "money" isn't available right now.
How would I re-generate the value and make sure he will get a different gift if the other ones are not available?
Alright. I don't sure I have understand everything.
You want make something which is checking if the money of a gift is available to give him to the player?
If I'm totally wrong; can you, please, make a example ?
Quote:
 Originally Posted by Dayrion Alright. I don't sure I have understand everything. You want make something which is checking if the money of a gift is available to give him to the player? If I'm totally wrong; can you, please, make a example ?
So let's say we have this command.

Код:
```CMD:getgift(playerid, params[])
{
new rand = random(10);
if(rand == 5 && boxVariables[1][boxMoney] == 1)
{
SendClientMessage(playerid, -1, "You have won \$500,000!");
}
else if(rand == 0 && boxVariables[1][boxVehicle] == 1)
{
SendClientMessage(playerid, -1, "You have won a car!");
}
return 1;
}```
As you can see, if the random value is 5 and boxMoney == 1, it will give him the money.
But let's say he gets the value 5, but boxMoney is 0, how could I make it so it will choose a different number and give him a different price.
Right now, if he rolls 5 and boxMoney is equal to 0, he won't get anything.
Alright.. I still reticent.
PHP код:
``` CMD:getgift(playerid, params[]){    new bool:done = false;    while(!done)    {        new rand = random(10);        switch(rand)        {            case 0 :            {                if(boxVariables[1][boxMoney] == 1)  SendClientMessage(playerid, -1, "You have won \$500,000!"), GivePlayerMoney(playerid, rand*1000), done = true;                else if (boxVariables[1][boxVehicle] == 1) SendClientMessage(playerid, -1, "You have won a car!"), done = true;                else rand = random(10);            }            // case ...        }    }    return 1;}  ```
Something like that?
Quote:
 Originally Posted by Dayrion Alright.. I still reticent. PHP код: ``` CMD:getgift(playerid, params[]) {     new bool:done = false;     while(!done)     {         new rand = random(10);         switch(rand)         {             case 0 :             {                 if(boxVariables[1][boxMoney] == 1)  SendClientMessage(playerid, -1, "You have won \$500,000!"), GivePlayerMoney(playerid, rand*1000), done = true;                 else if (boxVariables[1][boxVehicle] == 1) SendClientMessage(playerid, -1, "You have won a car!"), done = true;                 else rand = random(10);             }             // case ...         }     }     return 1; }  ``` Something like that?
How could I make a command that will choose a random value till it hits number 3 for example?
So for example:

Код:
```CMD:test(playerid, params[])
{
new rand = random(10);
if(rand == 3)
{
SendClientMessage(playerid, -1, "It has hit number 3!");
}
else
{
//Re-choose a random number till the number is 3.
}
return 1;
}```
OH!
This is more simple.
PHP код:
``` CMD:test(playerid, params[]){    new rand = random(10);    while(rand != 3) rand = random(10);    printf("Rand = %i", rand);}  ```
Quote:
 Originally Posted by Dayrion OH! This is more simple. PHP код: ``` CMD:test(playerid, params[]) {     new rand = random(10);     while(rand != 3) rand = random(10);     printf("Rand = %i", rand); }  ```
Yes! This is what I'm looking for.
Would this work?
Код:
```CMD:test(playerid, params[])
{
new rand = random(10);
while(rand == 5 && boxVariables[1][boxMoney] == 0) rand = random(10);
while(rand == 2 && boxVariables[1][boxVehicle] == 0) rand = random(10);
return 1;
}```
Hmm.. If
PHP код:
``` boxVariables[1][boxMoney] == 0  ```
will be an infinite loop because the condition will never be verified.
This one : boxVariables[1][boxMoney] == 0 mean there is no money in the gift ?
Quote:
 Originally Posted by Dayrion Hmm.. If PHP код: ``` boxVariables[1][boxMoney] == 0  ``` will be an infinite loop because the condition will never be verified. This one : boxVariables[1][boxMoney] == 0 mean there is no money in the gift ?
Yes.
So, you should change your function to:
PHP код:
``` CMD:test(playerid, params[]) {     new rand = random(10);     while(rand == 5 && boxVariables[1][boxMoney] != 0) rand = random(10); // While rand is equal to 5 and box's money isn't equal to 0 - attribute to random a new value     while(rand == 2 && boxVariables[1][boxVehicle] != 0) rand = random(10);     return 1; }  ```

Forum Jump:

Users browsing this thread: 1 Guest(s)