AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   randomizing (https://forums.alliedmods.net/showthread.php?t=61758)

iCap 10-08-2007 17:02

randomizing
 
how do you make something random like case 1: and case 2: and stuff like that?

M249-M4A1 10-08-2007 17:09

Re: randomizing
 
PHP Code:

public DoSomethingRandom() {
    switch(
random_num(1,10)) { // create a random number between 1 and 10, inclusive
        
case 1client_print(0print_chat"[AMXX] Random number: 1")
        case 
2client_print(0print_chat"[AMXX] Random number: 2")
        case 
3client_print(0print_chat"[AMXX] Random number: 3")
        case 
4..9client_print(0print_chat"[AMXX] Random number between 4 and 9 (inclusive)"// Something between 4 and 9, inclusive
        
default: client_print(0print_chat"[AMXX] This doesn't fit any of the above cases."// If nothing fits the case, do this
    
}



iCap 10-08-2007 17:24

Re: randomizing
 
okay i'm doing it right, thanks.

_Master_ 10-08-2007 17:26

Re: randomizing
 
One question: When is the default brach executed ?!?

TheNewt 10-08-2007 19:01

Re: randomizing
 
When something that isn't defined with "case <variable> :" like he did case 0-9, if something other then 0-9 was put in like 10 or -1, the default case would be in effect.

_Master_ 10-09-2007 05:28

Re: randomizing
 
Wrong.
The default case will NEVER be executed because of the limits in random_num()

iCap 10-09-2007 06:49

Re: randomizing
 
This is what i'm doing and i think it's the best way I could do it. I'm half-way finished with the other stuff in the code but this is this random part; is it correct? i just started coding so please take it easy if somethings wrong

PHP Code:

public give_award(id)
{
    if (!
get_pcvar_num(killawards))
    return 
PLUGIN_HANDLED
    
    
new attacker read_data(1)
    new 
random random_num(113)
    
    switch(
random)
    {
        case 
1:
        {
            
client_print(attackerprint_chat"*Recieved Health Award")
            
health_award(id)
        }
        case 
2:
        {
            
client_print(attackerprint_chat"*Recieved Armor Award")
            
armor_award(id)
        }
        case 
3:
        {
            
client_print(attackerprint_chat"*Recieved Ammo Award")
            
ammo_award(id)
        }
        case 
4:
        {
            
client_print(attackerprint_chat"*Recieved Speed Award")
            
speed_award(id)
        }
        case 
5:
        {
            
client_print(attackerprint_chat"*Recieved Gravity Award")
            
gravity_award(id)
        }
        case 
6:
        {
            
client_print(attackerprint_chat"*Recieved Assualt Award")
            
assault_award(id)
        }
        case 
7:
        {
            
client_print(attackerprint_chat"*Recieved Money Award")
            
money_award(id)
        }
        case 
8:
        {
            
client_print(attackerprint_chat"*Recieved Noclip Award")
            
noclip_award(id)
        }
        case 
9:
        {
            
client_print(attackerprint_chat"*Recieved Random Award")
            
random_award(id)
        }
        case 
10:
        {
            
client_print(attackerprint_chat"*Recieved Blank Award")
            
blank_award(id)
        }
        case 
11:
        {
            
client_print(attackerprint_chat"*Recieved Suit Award")
            
suit_award(id)
        }
        case 
12:
        {
            
client_print(attackerprint_chat"*Recieved HP+AP Award")
            
hpap_award(id)
        }
    }
    return 
PLUGIN_HANDLED



purple_pixie 10-09-2007 06:51

Re: randomizing
 
Looks fine.

Although you can just switch the return value of random_num, you don't have to store it.

iCap 10-09-2007 06:59

Re: randomizing
 
could you explain that a little more please? sorry i just want to try to be good like alot of you guys

M249-M4A1 10-09-2007 07:32

Re: randomizing
 
Quote:

Originally Posted by _Master_ (Post 540375)
Wrong.
The default case will NEVER be executed because of the limits in random_num()

Wrong.
The default case DOES execute if random_num returns 10.


All times are GMT -4. The time now is 16:02.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.