Raised This Month: $ Target: $400
 0% 

Index out of bounds


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
egbertjan
Senior Member
Join Date: Mar 2007
Location: The Netherlands
Old 05-13-2014 , 17:02   Index out of bounds
Reply With Quote #1

I would like to know why the following code is out of bounds.


Code:
public fw_TakeDamage(victim, inflictor, attacker, Float:damage) {
	
	if (is_user_connected(victim) && is_user_connected(attacker)) {
	
	new Float:extradmg = 1 + (player_power[attacker]*0.01)

	if (is_user_connected(attacker) && is_user_alive(attacker) && player_power[attacker] > 0) {
			SetHamParamFloat(4, damage * extradmg)
		} else {
			SetHamParamFloat(4, damage)
		}
	}
}


Also I've read something of returning the HAM function, however there is no return value. How is it possible to get this?

Code:
if (!is_user_connected(victim) || !is_user_connected(attacker)) return HAM_IGNORED

Thanks in advance.
egbertjan is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 05-13-2014 , 17:10   Re: Index out of bounds
Reply With Quote #2

No idea why you're checking for is_user_connected and is_user_alive, just check if both players are player entities.

You're probably getting that error because your player_power variable doesn't have enough cells, how do you initialize it?
Backstabnoob is offline
Fr33m@n
Veteran Member
Join Date: May 2008
Location: France Marne
Old 05-13-2014 , 17:33   Re: Index out of bounds
Reply With Quote #3

Return values only matter if you are in Pre, Pre mean forward the takedamage call. In PRE, they allow you to modify some stuff, like blocking the incoming takedamage.

See ham_const.inc

PHP Code:
/**
 * Ham return types.
 * -
 * Return these from hooks to disable calling the target function.
 * Numbers match up with fakemeta's FMRES_* for clarity.  They are interchangable.
 * 0 (or no return) is also interpretted as HAM_IGNORED.
 */
#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*()) */ 
In your case you are in pre, so i think it's better to call it like that :
PHP Code:
public fw_TakeDamage(victiminflictorattackerFloat:damage)
{
    if ( 
is_user_connected(victim) && is_user_connected(attacker) && player_power[attacker] > )
    {
        
//new Float:extradmg = 1.0 + player_power[attacker]*0.01
        // skip useless variable when you can, this is the base of optimisation

        
SetHamParamFloat(4damage * (1.0 player_power[attacker]*0.01))

        return 
HAM_HANDLED
    
}
    return 
HAM_IGNORED


Last edited by Fr33m@n; 05-13-2014 at 18:03.
Fr33m@n is offline
meTaLiCroSS
Gaze Upon My Hat
Join Date: Feb 2009
Location: Viņa del Mar, Chile
Old 05-13-2014 , 20:36   Re: Index out of bounds
Reply With Quote #4

Assuming that you registered that forward to "player" entities, there's no need to check if victim is a player; it would be always a player.
__________________
Quote:
Originally Posted by joropito View Post
You're right Metalicross
meTaLiCroSS is offline
egbertjan
Senior Member
Join Date: Mar 2007
Location: The Netherlands
Old 05-14-2014 , 06:47   Re: Index out of bounds
Reply With Quote #5

Quote:
Originally Posted by Backstabnoob View Post
No idea why you're checking for is_user_connected and is_user_alive, just check if both players are player entities.

You're probably getting that error because your player_power variable doesn't have enough cells, how do you initialize it?
I read somewhere that this could be a way to check for it, however if a player is already checked upon executing the script then it's kind of useless indeed.

To initialize it I got the following in client_connect:
Code:
player_power[id] = 0
If a player wants to level this up he use a menu to advance this. It's been done the same way with 5 other skills.

Quote:
Originally Posted by Fr33m@n View Post
Return values only matter if you are in Pre, Pre mean forward the takedamage call. In PRE, they allow you to modify some stuff, like blocking the incoming takedamage.

See ham_const.inc

PHP Code:
/**
 * Ham return types.
 * -
 * Return these from hooks to disable calling the target function.
 * Numbers match up with fakemeta's FMRES_* for clarity.  They are interchangable.
 * 0 (or no return) is also interpretted as HAM_IGNORED.
 */
#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*()) */ 
In your case you are in pre, so i think it's better to call it like that :
PHP Code:
public fw_TakeDamage(victiminflictorattackerFloat:damage)
{
    if ( 
is_user_connected(victim) && is_user_connected(attacker) && player_power[attacker] > )
    {
        
//new Float:extradmg = 1.0 + player_power[attacker]*0.01
        // skip useless variable when you can, this is the base of optimisation

        
SetHamParamFloat(4damage * (1.0 player_power[attacker]*0.01))

        return 
HAM_HANDLED
    
}
    return 
HAM_IGNORED

I get the part that you have to be in "pre" to use the return function, however I don't quite understand how to use the pre function and recognize when this is used. Could you explain where I can see where the pre is in the script you provided?

Quote:
Originally Posted by meTaLiCroSS View Post
Assuming that you registered that forward to "player" entities, there's no need to check if victim is a player; it would be always a player.
Yes I've forwarded it to a player entity, so I think it's kind of useless indeed.
Code:
RegisterHam(Ham_TakeDamage, "player", "fw_TakeDamage")
egbertjan is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 05-14-2014 , 07:05   Re: Index out of bounds
Reply With Quote #6

Yes, just check if the attacker is a player entity (if his index is between 0 and your maxplayers).

What you showed is not how you initialize the array, show me the line with new in front of it.
Backstabnoob is offline
egbertjan
Senior Member
Join Date: Mar 2007
Location: The Netherlands
Old 05-14-2014 , 11:04   Re: Index out of bounds
Reply With Quote #7

Oh. In that case this is my init: new player_power[33]
egbertjan is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 05-14-2014 , 11:24   Re: Index out of bounds
Reply With Quote #8

What line are you getting the error on exactly? With your current code
Backstabnoob is offline
egbertjan
Senior Member
Join Date: Mar 2007
Location: The Netherlands
Old 05-14-2014 , 11:56   Re: Index out of bounds
Reply With Quote #9

The actual errors are gone for now, however I would still like to get some information about the "pre" part that Fr33m@n was talking about. Is there a link where I can read about this?
egbertjan is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 05-14-2014 , 12:04   Re: Index out of bounds
Reply With Quote #10

In every Ham hook you can specify if it's a pre or post hook (the last argument in RegisterHam).

If it's a pre hook, it happens before the action happens so you can use the return values to block the action, you also have an arsenal of natives to use such as SetHamParamInt/Float/...

However in post hook the action has already happened and there's nothing you can do about it. It's good to hook post if you for example want to check the DEFINITE damage in TakeDamage. If you hooked it as a pre, there's a chance another plugin might still change the value.
Backstabnoob is offline
Reply


Thread Tools
Display Modes

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 09:40.


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