09.05.2015, 21:27
(
Last edited by SickAttack; 08/06/2015 at 12:15 AM.
)
Random Aspects
Using numargs(), getarg(...) and random(...)
to make random systems dynamic and easier!
InformationUsing numargs(), getarg(...) and random(...)
to make random systems dynamic and easier!
In this tutorial you will acquire knowledge of a method which can be used to make your random systems dynamic and easier to use. Meaning that you can use a function which returns random values or does random actions in different parts of your script, while having control over what the function returns/applies.
We will use the following integrated SA:MP functions in this tutorial:
- numargs() - Get the number of arguments passed to a function.
- getarg(...) - Get an argument that was passed to a function.
- random(...) - Get a pseudo-random number.
Let's go thru the explanation of what we are going to do in this tutorial first:
We will create a new function and put "..." between the parenthesis of our function, this will tell the script that we can and want to input how many arguments we want while using the function. We will use numargs(...) to get the number of arguments we inputted in the function and we will then store it in a variable called "arguments". We will use getarg(...) to get what we inputted in the index of an argument and we will then store it in a variable called "selected". And finally we will use random(...) to select a random argument of what was entered in the function's parenthesis.
Our basic structure will look like this:
pawn Code:
function(...)
{
new arguments = numargs(), selected = getarg(random(arguments));
}
pawn Code:
public OnPlayerRequestClass(playerid, classid)
{
switch(classid)
{
case 0: PlayRandomAnimation(playerid, 0, 1, 2, 3, 4, 5);
case 1: PlayRandomAnimation(playerid, 4, 5);
case 2: PlayRandomAnimation(playerid, 2, 5);
case 3: PlayRandomAnimation(playerid, 0, 1);
case 4: PlayRandomAnimation(playerid, 2, 4, 5);
case 5: PlayRandomAnimation(playerid, 2);
}
return 1;
}
PlayRandomAnimation(playerid, ...)
{
new arguments = numargs() - 1, selected = getarg(random(arguments) + 1);
switch(selected)
{
case 0: ApplyAnimation(playerid, "BENCHPRESS", "GYM_BP_CELEBRATE", 4.1, 1, 1, 1, 0, 0, 1);
case 1: ApplyAnimation(playerid, "DEALER", "DEALER_IDLE", 4.1, 1, 1, 1, 0, 0, 1);
case 2: ApplyAnimation(playerid, "DEALER", "DEALER_DEAL", 4.1, 1, 1, 1, 0, 0, 1);
case 3: ApplyAnimation(playerid, "COP_AMBIENT", "COPLOOK_THINK", 4.1, 1, 1, 1, 0, 0, 1);
case 4: ApplyAnimation(playerid, "FLAME", "FLAME_FIRE", 4.1, 1, 1, 1, 0, 0, 1);
case 5: ApplyAnimation(playerid, "GRAFFITI", "GRAFFITI_CHKOUT", 4.1, 1, 1, 1, 0, 0, 1);
}
}
Quote:
The 1st class will play a random animation, either 0, 1, 2, 3, 4 or 5. The 2nd class will play a random animation, either 4 or 5. The 3rd class will play a random animation, either 2 or 5. The 4th class will play a random animation, either 0 or 1. The 5th class will play a random animation, either 2, 4 or 5. The 6th class will play the 2nd indexed animation. |
pawn Code:
random(10);
Or even this:
RandomBetween(min, max)
{
new rand = random(max - min) + min;
return rand;
}
pawn Code:
RandomNumber(...)
{
new arguments = numargs(), selected = getarg(random(arguments));
return selected;
}
printf("Random Argument: %d", RandomNumber(500, 1000, 1500, 2000));
You can even send specified random messages randomly!
pawn Code:
SendRandomMessage(playerid, 0, 2);
SendRandomMessage(playerid, 1, 3);
SendRandomMessage(playerid, ...)
{
new arguments = numargs() - 1, selected = getarg(random(arguments) + 1), string[32 char];
switch(selected)
{
case 0: strpack(string, "Hello!");
case 1: strpack(string, "How are you?");
case 2: strpack(string, "Good luck!");
case 3: strpack(string, "What's up?");
}
return SendClientMessage(playerid, -1, string);
}
Notice
Stand-alone arguments affect the result of numargs() and random(...), so if you're planning on adding other arguments to your function, subtract the total amount of arguments by the amount of arguments you put into the function, and add the amount to random(...).
pawn Code:
function(playerid, ...)
{
new arguments = numargs() - 1, selected = getarg(random(arguments) + 1);
}
This method of creating random systems is easier to use and it gives you the ability to pick what you want it to sort out for you.
Additional
This thread is also available in Spanish / Espaсol, click here.