AlliedModders

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

Depresie 01-12-2016 12:23

[HELP] Index out of bounds
 
1 Attachment(s)
so im trying to create am ambience + round countdown plugin in one.. everything works fine, except i get index out of bounds error on line 129

PHP Code:

#include <amxmodx> #include <csx>
 #include <amxmisc>
 #include <zp50_gamemodes>
 

 #define PLUGIN "[ZP] Ambience Sounds"
 #define VERSION "1.0"
 #define AUTHOR "ADN"
 

 #define TASK_COUNTER 12345
 #define TASK_AMBIENCE 12346
 #define TASK_SOUNDS 12347
 

 #define MAX_SPEAK 10
 #define MAX_SOUNDS 8
 

 #define SoundAmbience "adnx_sound/adnx_ambience.wav"
 #define SoundAnnounce "adnx_sound/adnx_countdown_announce.wav"
 #define SoundPrepare "adnx_sound/adnx_countdown_prepare.wav"
 

 
new speak[MAX_SPEAK][] = 
 {
     
"adnx_sound/adnx_countdown_1.wav"
     
"adnx_sound/adnx_countdown_2.wav"
     
"adnx_sound/adnx_countdown_3.wav",
     
"adnx_sound/adnx_countdown_4.wav"
     
"adnx_sound/adnx_countdown_5.wav"
     
"adnx_sound/adnx_countdown_6.wav"
     
"adnx_sound/adnx_countdown_7.wav",
     
"adnx_sound/adnx_countdown_8.wav"
     
"adnx_sound/adnx_countdown_9.wav",
     
"adnx_sound/adnx_countdown_10.wav"
 
}
 

 new 
sounds[MAX_SOUNDS][] =
 {
     
"adnx_sound/adnx_ambience_sound1.wav"
     
"adnx_sound/adnx_ambience_sound2.wav"
     
"adnx_sound/adnx_ambience_sound3.wav",
     
"adnx_sound/adnx_ambience_sound4.wav",
     
"adnx_sound/adnx_ambience_sound5.wav",
     
"adnx_sound/adnx_ambience_sound6.wav",
     
"adnx_sound/adnx_ambience_sound7.wav",
     
"adnx_sound/adnx_ambience_sound8.wav",
 }
 

 new 
g_timerg_msgsync
 

 
public plugin_init() 
 {
     
register_plugin(PLUGINVERSIONAUTHOR)
     
     
register_event("HLTV""event_round_start""a""1=0""2=0")
     
     
g_msgsync CreateHudSyncObj()
 }
 

 public 
plugin_precache() 
 {
     for(new 
0sizeof(speak); x++) 
     {
         
precache_sound(speak[x])
     }
     for(new 
0sizeof(sounds); x++) 
     {
         
precache_sound(sounds[x])
     }
     
precache_sound(SoundAmbience)
     
precache_sound(SoundAnnounce)
     
precache_sound(SoundPrepare)
 

 

     
     return 
PLUGIN_CONTINUE
 
}
 

 public 
event_round_start()
 {
     
g_timer get_cvar_num("zp_gamemode_delay")
     
     
set_task(0.1"Countdown"TASK_COUNTER)
     
set_task(0.1"AmbienceBackground"TASK_AMBIENCE)
     
set_task(0.1"AmbienceSounds"TASK_SOUNDS)
     
     return 
PLUGIN_CONTINUE
 
}
 

 public 
zp_fw_gamemodes_start()
 {
     
set_task(5.0"AmbienceSounds"TASK_SOUNDS)
 }
 

 public 
Countdown() 
 {
     if((
zp_gamemodes_get_current() == ZP_NO_GAME_MODE) && g_timer 0)
     {
         if(
g_timer == 20)
         {
             
client_cmd(0"spk %s"SoundAnnounce)
         }
         if(
g_timer == 12)
         {
             
client_cmd(0"spk %s"SoundPrepare)
         }
         
set_task(1.0"Countdown"TASK_COUNTER)
         
         if(
g_timer <= 10
         {
             
set_hudmessage(17900, -1.00.1520.021.00.010.110)
             
ShowSyncHudMsg(0g_msgsync"=================^n=======================^n==========================^n=========|  WARNING!!!  |=========^n=========|  BIOHAZZARD  |=========^n===========|  [%d]  |===========^n=========================^n================="g_timer)
             
client_cmd(0"spk %s"speak[g_timer -1])
             
         }
         --
g_timer
         
     
}
     return 
PLUGIN_CONTINUE
 
}
     
 public 
AmbienceBackground()
 {
     
set_task(20.0"AmbienceBackground"TASK_AMBIENCE)
     
client_cmd(0"spk %s"SoundAmbience)
 }
 

 public 
AmbienceSounds()
 {
     
set_task(random_float(10.030.0), "AmbienceSounds"TASK_SOUNDS)
     
client_cmd(0"spk %s"sounds[random_num(0MAX_SOUNDS)])
 }
         
 public 
zp_fw_gamemodes_end()
 {
     
remove_task(TASK_COUNTER)
     
remove_task(TASK_AMBIENCE)
     
remove_task(TASK_SOUNDS)
 } 


siriusmd99 01-12-2016 15:12

Re: [HELP] Index out of bounds
 
PHP Code:

new sounds[MAX_SOUNDS][] =
{
    
"adnx_sound/adnx_ambience_sound1.wav"
    
"adnx_sound/adnx_ambience_sound2.wav"
    
"adnx_sound/adnx_ambience_sound3.wav",
    
"adnx_sound/adnx_ambience_sound4.wav",
    
"adnx_sound/adnx_ambience_sound5.wav",
    
"adnx_sound/adnx_ambience_sound6.wav",
    
"adnx_sound/adnx_ambience_sound7.wav",
    
"adnx_sound/adnx_ambience_sound8.wav",


to

PHP Code:

new sounds[MAX_SOUNDS][] =
{
    
"adnx_sound/adnx_ambience_sound1.wav"
    
"adnx_sound/adnx_ambience_sound2.wav"
    
"adnx_sound/adnx_ambience_sound3.wav",
    
"adnx_sound/adnx_ambience_sound4.wav",
    
"adnx_sound/adnx_ambience_sound5.wav",
    
"adnx_sound/adnx_ambience_sound6.wav",
    
"adnx_sound/adnx_ambience_sound7.wav",
    
"adnx_sound/adnx_ambience_sound8.wav"



Depresie 01-12-2016 16:01

Re: [HELP] Index out of bounds
 
ohh... damn... i dont know how i missed that.. i had 10 sounds and i guess i forgot the ","
it's funny, shouldn't it give an error at compilation in this case?

Later Edit: It looks like the compiler has major issues in this way, i found more cases like this.. i posted in the issues section

siriusmd99 01-13-2016 06:14

Re: [HELP] Index out of bounds
 
client_cmd(0, "spk %s", sounds[random_num(0, MAX_SOUNDS)])


To:

client_cmd(0, "spk %s", sounds[random_num(0, MAX_SOUNDS - 1)])

You have 8 sounds but array starts with 0 .
Sounds[0] = sound1
Sounds[7] = sound8

And sound[8] shall be null.

addons_zz 01-13-2016 07:43

Re: [HELP] Index out of bounds
 
siriusmd99, hawk eyes.

Update:

Bugsy, hawk eyes. addons zz, duck eyes.

Bugsy 01-13-2016 08:36

Re: [HELP] Index out of bounds
 
Quote:

Originally Posted by siriusmd99 (Post 2382845)
client_cmd(0, "spk %s", sounds[random_num(0, MAX_SOUNDS)])


To:

client_cmd(0, "spk %s", sounds[random_num(0, MAX_SOUNDS - 1)])

You have 8 sounds but array starts with 0 .
Sounds[0] = sound1
Sounds[7] = sound8

And sound[8] shall be null.

Just do random( MAX_SOUNDS ) , this automatically does 0 to num-1.

Sound[8] shall be index out of bounds, not null.

siriusmd99 01-13-2016 10:28

Re: [HELP] Index out of bounds
 
Quote:

Originally Posted by Bugsy (Post 2382878)
Just do random( MAX_SOUNDS ) , this automatically does 0 to num-1.

Sound[8] shall be index out of bounds, not null.

Thanks , i had some bad experience with amxx, didn't know what's the difference between random and random_num.


All times are GMT -4. The time now is 09:24.

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