AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Getting random id (https://forums.alliedmods.net/showthread.php?t=62060)

Sputnik53 10-17-2007 17:51

Getting random id
 
How can I get a random id of a player in CT team?
I tried using get_players but it didn't work for me. :(

ConnorMcLeod 10-17-2007 18:04

Re: Getting random id
 
Try this :

Code:
new players[32], ctnum, random_ct_player get_players(players, ctnum, "e", "CT") switch(ctnum) {     case 0:return     case 1:random_ct_player = players[0]     default:random_ct_player = players[random(ctnum)] }

Sputnik53 10-17-2007 19:57

Re: Getting random id
 
Yeah, only I can be dumb enough to forgot to check if there's no CTs. :D
Thanks connorr.

ConnorMcLeod 10-17-2007 20:18

Re: Getting random id
 
Code edited, it should be better with switch.

Sputnik53 10-17-2007 20:22

Re: Getting random id
 
Code:
random_ct_player = players[random(1, ctnum)]
Number of arguments doesn't not match definition.

M249-M4A1 10-17-2007 21:07

Re: Getting random id
 
Change random to random_num

Sputnik53 10-18-2007 05:46

Re: Getting random id
 
I'm only afraid that random_num(1, ctnum) has a chance of returning the id as 1, and you know there's not always someone who has it.

Arkshine 10-18-2007 05:52

Re: Getting random id
 
You're retrieve a player list with get_players().

players[x] return a player id where 0 <= x <= ctnum - 1.

also players[random_num(1, ctnum)] should be players[random(ctnum)] since players[] start at 0.



It should work fine.

Code:
    get_randomCT_id()     {         static iPlayers[32], iCT_num;         get_players( iPlayers, iCT_num, "e", "CT" );                 if( !iCT_num )             return;                 return iCT_num > 1 ? iPlayers[ random( iCT_num ) ] : iPlayers[ iCT_num - 1 ];     }

Sputnik53 10-18-2007 06:25

Re: Getting random id
 
Code:
return iCT_num > 1 ? iPlayers[random(iCT_num)] : iPlayers[iCT_num-1]
Warning: Function get_randomCT_id should return a value on line #
Error: Function uses both "return" and "return <value>" on line #

ConnorMcLeod 10-18-2007 06:31

Re: Getting random id
 
Code:
if( !iCT_num )             return -1

Then when you call the function, make sure to do nothing if it returns -1.


All times are GMT -4. The time now is 01:19.

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