AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   index out of bounds (https://forums.alliedmods.net/showthread.php?t=271935)

Kowalsky 09-20-2015 14:16

index out of bounds
 
Code:
new players[32], num;             get_players( players, num, "aeh", "CT" );             for( new i = 0; i < num ;i++ ) {                 set_user_health( players[ i ], 200 );             }                         Boss1 = players[ random( num ) ];

this line -> Boss1 = players[ random( num ) ];

is giving me an "index out of bounds" error. Why?

Bugsy 09-20-2015 15:15

Re: index out of bounds
 
It does that when get_players() finds no players, resulting in a random( 0 ) call which will return a number outside the boundaries of your array, and is irrelevant for what you're doing.

Do this:
PHP Code:

if ( num )
     
Boss1 playersrandomnum ) ]; 


siriusmd99 09-20-2015 15:23

Re: index out of bounds
 
Because players[random(num)] has a value bigger r than Boss1supports. For example you declare boss[32] and players[random(num)] has 33 (bigger than 32).i had the same problem these days

fysiks 09-20-2015 15:36

Re: index out of bounds
 
Quote:

Originally Posted by siriusmd99 (Post 2345299)
Because players[random(num)] has a value bigger r than Boss1supports. For example you declare boss[32] and players[random(num)] has 33 (bigger than 32).i had the same problem these days

That doesn't make any sense.

Kowalsky 09-21-2015 05:44

Re: index out of bounds
 
Quote:

Originally Posted by Bugsy (Post 2345293)
It does that when get_players() finds no players, resulting in a random( 0 ) call which will return a number outside the boundaries of your array, and is irrelevant for what you're doing.

Do this:
PHP Code:

if ( num )
     
Boss1 playersrandomnum ) ]; 


Thanks.

But could you please explain why does it sometimes gives 0? For example I had a situation where we had 4 CTS and it gave me the result 0 and nothing happened next.. (my code didnt work) How can I fix it?

Bugsy 09-21-2015 20:51

Re: index out of bounds
 
Quote:

Originally Posted by Kowalsky (Post 2345481)
Thanks.

But could you please explain why does it sometimes gives 0? For example I had a situation where we had 4 CTS and it gave me the result 0 and nothing happened next.. (my code didnt work) How can I fix it?

When exactly is this called when it errors? Is it ever called immediately after a server start or map change when there are no players on teams yet?

There are/were issues with get_players() when certain flags were passed. What AMX-X version are you running? If 1.8.2 or earlier then I suggest upgrading to a 1.8.3 build and see if it resolves your issue. I believe this was addressed in 1.8.3.

If you search around the forums you will find a few threads regarding the problems with flags.. here's one: https://forums.alliedmods.net/showthread.php?t=256059


All times are GMT -4. The time now is 22:13.

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