Raised This Month: $ Target: $400
 0% 

index out of bounds


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Kowalsky
Senior Member
Join Date: Mar 2015
Location: Poland
Old 09-20-2015 , 14:16   index out of bounds
Reply With Quote #1

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?
Kowalsky is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-20-2015 , 15:15   Re: index out of bounds
Reply With Quote #2

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 ) ]; 
__________________

Last edited by Bugsy; 09-20-2015 at 15:22.
Bugsy is offline
Kowalsky
Senior Member
Join Date: Mar 2015
Location: Poland
Old 09-21-2015 , 05:44   Re: index out of bounds
Reply With Quote #3

Quote:
Originally Posted by Bugsy View Post
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?
Kowalsky is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-21-2015 , 20:51   Re: index out of bounds
Reply With Quote #4

Quote:
Originally Posted by Kowalsky View Post
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
__________________

Last edited by Bugsy; 09-21-2015 at 20:53.
Bugsy is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 09-20-2015 , 15:23   Re: index out of bounds
Reply With Quote #5

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
siriusmd99 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-20-2015 , 15:36   Re: index out of bounds
Reply With Quote #6

Quote:
Originally Posted by siriusmd99 View Post
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.
__________________
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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