AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Cvar Utilities (https://forums.alliedmods.net/showthread.php?t=242495)

grs4 06-20-2014 10:43

Cvar Utilities
 
Code:

enum e_Cvar {
    CVAR_BASE_HEALTH,
    CVAR_TIME_TO_WAVE,
    CVAR_MONSTER_DAMAGE,
    CVAR_BOSS_DAMAGE,
    CVAR_KILL_GOLD,
    CVAR_KILL_MONEY,
    CVAR_KILL_BONUS_GOLD,
    CVAR_KILL_BOSS_GOLD,
    CVAR_KILL_BP_AMMO,
    CVAR_BLOCK_CMD_KILL,
    CVAR_KILL_MONSTER_FX,
    CVAR_ONE_PLAYER_MODE,
    CVAR_WAVE_GOLD,
    CVAR_WAVE_MONEY,
    CVAR_COUNTDOWN_MODE,
    CVAR_RESPAWN_PLAYER_CMD,
    CVAR_SEND_MONSTER_TIME,
    CVAR_SWAP_MONEY,
    CVAR_SWAP_MONEY_MONEY,
    CVAR_SWAP_MONEY_GOLD,
    CVAR_DAMAGE_RATIO,
    CVAR_DAMAGE_GOLD
}

new gCvarInfo[e_Cvar]
new gCvarValue[e_Cvar]

public plugin_init()
{
gCvarInfo[CVAR_BASE_HEALTH]    = CvarRegister("td_base_health", "80");
    gCvarInfo[CVAR_TIME_TO_WAVE]    = CvarRegister("td_time_to_wave", "12")
    gCvarInfo[CVAR_MONSTER_DAMAGE]    = CvarRegister("td_damage", "4");
    gCvarInfo[CVAR_BOSS_DAMAGE]    = CvarRegister("td_boss_damage", "8");
   
    // =====
   
    gCvarInfo[CVAR_KILL_GOLD]    = CvarRegister("td_kill_gold", "3");
    gCvarInfo[CVAR_KILL_BONUS_GOLD]    = CvarRegister("td_kill_bonus_gold", "10");
    gCvarInfo[CVAR_KILL_BOSS_GOLD]    = CvarRegister("td_kill_boss_gold", "6");
    gCvarInfo[CVAR_KILL_MONEY]    = CvarRegister("td_kill_money", "650");
    gCvarInfo[CVAR_KILL_BP_AMMO]    = CvarRegister("td_kill_bp_ammo", "15");
    gCvarInfo[CVAR_WAVE_GOLD]    = CvarRegister("td_wave_gold", "5");
    gCvarInfo[CVAR_WAVE_MONEY]    = CvarRegister("td_wave_money", "1000");
   
    gCvarInfo[CVAR_SWAP_MONEY]    = CvarRegister("td_swap_money", "1");
    gCvarInfo[CVAR_SWAP_MONEY_MONEY] = CvarRegister("td_swap_money_money", "10000");
}
public OnChangeCvarValue( handleCvar, const oldValue[], const newValue[], const cvarName[], const defaultValue[] )
{
    if(handleCvar != _:gCvarInfo[CVAR_SEND_MONSTER_TIME])
    {
        server_print("%s %d", newValue, handleCvar)
        gCvarValue[e_Cvar:handleCvar] = str_to_num(newValue)
    }
}

I want to set value of Cvar to gCvarValue which is created by enum, when i want to set this value, i've got Index out of bounds error.

How to do it usings enum's ?

Nextra 06-20-2014 12:17

Re: Cvar Utilities
 
The cvar handles do not correspond to your enum values. Doing e_Cvar:handleCvar is pretty much guaranteed to give you OOB errors. You have to actually search for the cvar handle in gCvarInfo.

grs4 06-20-2014 13:15

Re: Cvar Utilities
 
OK thanks for answer, i do it :
Code:

public OnChangeCvarValue( handleCvar, const oldValue[], const newValue[], const cvarName[], const defaultValue[] )
{
    if(handleCvar != _:gCvarInfo[CVAR_SEND_MONSTER_TIME]) {
        for(new i; i < _:e_Cvar; i++) {
            if(gCvarInfo[e_Cvar:i] == handleCvar) {
                gCvarValue[e_Cvar:i] = str_to_num(newValue)
                client_print(0,3,"%d %s %d", i, cvarName, str_to_num(newValue))
                client_print(0,3,"%d", gCvarValue[e_Cvar:i] )
                break
            }
        }
       
    }
}

And it works propely, but if i type under all cvars
Code:

CvarHookChangeAll("OnChangeCvarValue");
I'll give error:
Code:


L 06/20/2014 - 19:19:30: [CVAR_UTIL] Invalid hook callback "100"
L 06/20/2014 - 19:19:30: [AMXX] Displaying debug trace (plugin "td_new.amxx", version "0.4")
L 06/20/2014 - 19:19:30: [AMXX] Run time error 19: function not found
L 06/20/2014 - 19:19:30: [AMXX]    [0] td_new.sma::LoadCvars (line 465)
L 06/20/2014 - 19:19:30: [AMXX]    [1] td_new.sma::plugin_init (line 417)

If i type
Code:

CvarHookChangeAll("OnChangeCvarValue", true);
It will work, but first value is 0, and 0 errors it'll given
How to do this with out ignoring first call, with out any errors ... ?


edit;

i missed the problem using this under all CvarRegister
Code:

    for(new i; i < _:e_Cvar;i++)
        if(i != _:CVAR_SEND_MONSTER_TIME)
            gCvarValue[e_Cvar:i] = get_pcvar_num(gCvarInfo[e_Cvar:i])

I do it with "true", and values are properly loaded, and it works fine, but i want to do without this "true" in hook,how to ?

Arkshine 06-20-2014 15:36

Re: Cvar Utilities
 
If purpose is to cache value, you could use directly CvarCache without the cvar hooking hassle.

grs4 06-20-2014 16:40

Re: Cvar Utilities
 
Love you Mr.Freeman, this is pretty cool :)
Greattings from Poland, and the topic can be closed :)


All times are GMT -4. The time now is 21:10.

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