Raised This Month: $51 Target: $400
 12% 

Problem with 'index out of bounds' in Ham_TakeDamage


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 07-17-2012 , 20:49   Problem with 'index out of bounds' in Ham_TakeDamage
Reply With Quote #1

Hi.
I have problem with 'index out of bounds' in Ham_TakeDamage.

plugin_init:
Code:
RegisterHam(Ham_TakeDamage, "player", "event_damage", 1);
Code:
PHP Code:
public event_damage(idinflictorattackerFloat:damagedamagebits)
{    
    if ( !
g_alive[attacker] )    return 1;  //line 358
    
if ( damagebits DMG_FALL )    return 1;
    if ( 
attacker == id )        return 1;
    if ( 
get_user_team(attacker) == get_user_team(id) )    return 1;

    
myfunction(attacker);
    
    return 
1;

Error logs:
Code:
L 07/18/2012 - 00:30:13: [AMXX] Run time error 4: index out of bounds 
L 07/18/2012 - 00:30:13: [AMXX]    [0] test.sma::event_damage (line 358)
Where i have error ?

Last edited by hh2; 07-17-2012 at 20:51.
hh2 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-17-2012 , 22:12   Re: Problem with 'index out of bounds' in Ham_TakeDamage
Reply With Quote #2

check if attacker is a player.

Code:
if( 0 < attacker <= get_maxplayer() )
But you should optimize this by caching the value of get_maxplayers().
__________________

Last edited by fysiks; 07-18-2012 at 18:56. Reason: small fix noted by Bugsy
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-18-2012 , 01:05   Re: Problem with 'index out of bounds' in Ham_TakeDamage
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
check if attacker is a player.

Code:
if( 0 < attacker < get_maxplayer() )
But you should optimize this by caching the value of get_maxplayers().
PHP Code:
if( attacker <= get_maxplayer() ) 
You should return one of the Ham return values in ham forwards.
Code:
#define HAM_IGNORED		1	/**< Calls target function, returns normal value */
#define HAM_HANDLED		2	/**< Tells the module you did something, still calls target function and returns normal value */
#define HAM_OVERRIDE	3	/**< Still calls the target function, but returns whatever is set with SetHamReturn*() */
#define HAM_SUPERCEDE	4	/**< Block the target call, and use your return value (if applicable) (Set with SetHamReturn*()) */
PHP Code:
const MAX_PLAYERS 32;

new 
g_aliveMAX_PLAYERS ];

#define IsPlayer(%1)    (1<=%1<=MAX_PLAYERS)

public event_damage(idinflictorattackerFloat:damagedamagebits)
{    
    if ( !
IsPlayerattacker ) || !g_alive[attacker] || ( damagebits DMG_FALL ) || ( attacker == id ) || ( get_user_team(attacker) == get_user_team(id) ) ) 
        return 
HAM_IGNORED;

    
myfunction(attacker);
    
    return 
HAM_IGNORED;

__________________
Bugsy is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 07-18-2012 , 01:23   Re: Problem with 'index out of bounds' in Ham_TakeDamage
Reply With Quote #4

Don't use MAX_PLAYERS for IsPlayer macro, entities indexes start right after maxClient sot if your server is 16 slots, attacker if a nade (for example) can theorically start on 17.
Also, you should make sure that player has taken some damage by checking pev_dmg_take (using engine in example so you don't need to create a var ( new Float:dmg_take ; pev(id, pev_dmg_take, dmg_take) ; if( dmg_take >= 1.0 )
End, in post forwards (at least for fakemeta and hamsandwich) you don't need to return a value.

Code:
const MAX_PLAYERS = 32; new g_alive[ MAX_PLAYERS + 1 ];
new g_iMaxPlayers
#define IsPlayer(%0)    ( 1 <= %0 <= g_iMaxPlayers )
public plugin_init() {     RegisterHam(Ham_TakeDamage, "player", "OnCBasePlayer_TakeDamage_Post", false)
    g_iMaxPlayers = get_maxplayers()
} public CBasePlayer_TakeDamage(id, iInflictor, iAttacker, Float:flDamage, bitsDamageType) {
    if( IsPlayer( iAttacker )
    &&  g_alive[iAttacker]     &&  ~damagebits & DMG_FALL     &&  iAttacker != id     &&  entity_get_float(id, EV_FL_dmg_take) >= 1.0 // check if player has really taken some damage     &&  cs_get_user_team(iAttacker) != cs_get_user_team(id)   )     {         myfunction(iAttacker);     } }
__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 07-18-2012 at 01:29.
ConnorMcLeod is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-18-2012 , 01:27   Re: Problem with 'index out of bounds' in Ham_TakeDamage
Reply With Quote #5

Quote:
Originally Posted by ConnorMcLeod View Post
Don't use MAX_PLAYERS for IsPlayer macro, entities indexes start right after maxClient sot if your server is 16 slots, attacker if a nade (for example) can theorically start on 17.
My fault, forgot about that.

But, if user\person-who-compiles sets MAX_PLAYERS accordingly, then this isnt a problem. ?
__________________

Last edited by Bugsy; 07-18-2012 at 01:28.
Bugsy is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 07-18-2012 , 01:30   Re: Problem with 'index out of bounds' in Ham_TakeDamage
Reply With Quote #6

Obviously, but for sure it's not a good code example to show on this board ;)
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 07-18-2012 , 07:00   Re: Problem with 'index out of bounds' in Ham_TakeDamage
Reply With Quote #7

Thanks for any help.
Plugin now worked without error logs
hh2 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 01:46.


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