[Tutorial] Random Aspects - Using numargs(), getarg(...) and random(...) to make random systems dynamic and easier!
#1

Random Aspects
Using numargs(), getarg(...) and random(...)
to make random systems dynamic and easier!
Information

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: Development

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));
}
Alright, let's assume you want to apply different animations randomly on the class selection for different classes. Then we would do something like this:
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);
    }
}
Depending on the class you are currently viewing on the class selection, it will choose a random animation index we specified in our function and then it will apply it.
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.

Traditionally you would do this:
pawn Code:
random(10);

Or even this:

RandomBetween(min, max)
{
    new rand = random(max - min) + min;
    return rand;
}
But what if you want to return specified values randomly?
pawn Code:
RandomNumber(...)
{
    new arguments = numargs(), selected = getarg(random(arguments));
    return selected;
}

printf("Random Argument: %d", RandomNumber(500, 1000, 1500, 2000));
It will return one of the values in the function's arguments.

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);
}
And you can do lots of other things with these functions too, just put your imagination in play!

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);
}
Conclusion

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.
Reply
#2

Damn, never even heard of those functions except for "random" lol..

Very nice tutorial, I've been around here since 2011 and I've got quite alot of scripting experience, though this is new to me.

Thanks!
Reply
#3

Something's not quite right. The random() function can always return 0, so in that case you are actually fetching the "playerid" parameter, resulting in the wrong animation being played, or no animation at all. It should probably look something like:
pawn Code:
selected = 1 + getarg(random(arguments - 1));
Reply
#4

Quote:
Originally Posted by Vince
View Post
Something's not quite right. The random() function can always return 0, so in that case you are actually fetching the "playerid" parameter, resulting in the wrong animation being played, or no animation at all. It should probably look something like:
pawn Code:
selected = 1 + getarg(random(arguments - 1));
I see. I sorted that part out, even though the script returns a different value compared to the player's id.
Reply
#5

Very nice tutorial! I didn't know these functions, thanks a lot!

Little suggestion: use 'strpack' instead of 'format', it's more faster.
Reply
#6

Quote:
Originally Posted by J4Rr3x
View Post
Very nice tutorial! I didn't know these functions, thanks a lot!

Little suggestion: use 'strpack' instead of 'format', it's more faster.
Added, it wasn't required, but yeah, it's now added.
Reply
#7

Quote:
Originally Posted by J4Rr3x
View Post
Little suggestion: use 'strpack' instead of 'format', it's more faster.
Would you stop suggesting that? Have you even used it yourself? Because it doesn't work. You are likely confused with strcat. By the way, literal assignments can simply be done with the assignment operator (=).
Reply
#8

Quote:
Originally Posted by Vince
View Post
Would you stop suggesting that? Have you even used it yourself? Because it doesn't work. You are likely confused with strcat. By the way, literal assignments can simply be done with the assignment operator (=).
provided the LValue and RValue strings are of the same size.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)