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=240364)

egbertjan 05-13-2014 17:02

Index out of bounds
 
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.

Backstabnoob 05-13-2014 17:10

Re: Index out of bounds
 
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?

Fr33m@n 05-13-2014 17:33

Re: Index out of bounds
 
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



meTaLiCroSS 05-13-2014 20:36

Re: Index out of bounds
 
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.

egbertjan 05-14-2014 06:47

Re: Index out of bounds
 
Quote:

Originally Posted by Backstabnoob (Post 2137400)
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 (Post 2137405)
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 (Post 2137442)
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")

Backstabnoob 05-14-2014 07:05

Re: Index out of bounds
 
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.

egbertjan 05-14-2014 11:04

Re: Index out of bounds
 
Oh. In that case this is my init: new player_power[33]

Backstabnoob 05-14-2014 11:24

Re: Index out of bounds
 
What line are you getting the error on exactly? With your current code

egbertjan 05-14-2014 11:56

Re: Index out of bounds
 
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?

Backstabnoob 05-14-2014 12:04

Re: Index out of bounds
 
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.


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

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