AlliedModders

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

bibu 04-12-2011 17:19

(Cvar Utilities) Hooking cvar question
 
Ok, I've tried these ways:

PHP Code:

new advanced_menu_chicken_sound

in init
:

advanced_menu_chicken_sound register_cvar("advanced_menu_chicken_sound""1")
CvarHookChange(advanced_menu_chicken_sound register_cvar("advanced_menu_chicken_sound""1"), "OnCvarChange"

And I am checking this with
PHP Code:

get_pcvar_num 

. Is this a good way at all? I also tried todo this:

PHP Code:

in init:

new 
advanced_menu_chicken_sound_register CvarRegister("advanced_menu_chicken_sound""1""Enable/Disable Chicken sound"

This didn't work.

Arkshine 04-12-2011 17:29

Re: (Cvar Utilities) Hooking cvar question
 
You can do directly either :

Code:
CvarHookChange( register_cvar( "advanced_menu_chicken_sound", "1" ), "OnChange" );

or :

Code:
new handleCvar = register_cvar( "advanced_menu_chicken_sound", "1" ); CvarHookChange( handleCvar, "OnChange" );

or using CvarRegister :

Code:
new handleCvar = CvarRegister( "advanced_menu_chicken_sound", "1", "Enable/Disable Chicken sound" ); CvarHookChange( handleCvar, "OnChange" );

Now, you say it doesn't work. It doesn't help.

- Show the whole code
- Check the console output if you see errors
- You can also check the return value of CvarHookChange

bibu 04-13-2011 14:58

Re: (Cvar Utilities) Hooking cvar question
 
But how can I use get_pcvar_num then?

Exolent[jNr] 04-13-2011 15:12

Re: (Cvar Utilities) Hooking cvar question
 
With handleCvar variable from his example.

bibu 04-13-2011 15:14

Re: (Cvar Utilities) Hooking cvar question
 
Can't I hook the change of the cvars together, but still having support for multiple cvars for sure.

Arkshine 04-13-2011 16:03

Re: (Cvar Utilities) Hooking cvar question
 
You can use a same callback with different cvars, no problem.

bibu 04-13-2011 16:18

Re: (Cvar Utilities) Hooking cvar question
 
How can I do that?

Arkshine 04-13-2011 16:21

Re: (Cvar Utilities) Hooking cvar question
 
Just use CvarHookChange() for all your cvars with the cvar handle from register_cvar or CvarRegister, and write the same callback as second argument, for example "OnChange".

Like :

Code:
CvarHookChange( register_cvar( "MyCvar1", "1" ), "OnChange" ); CvarHookChange( register_cvar( "MyCvar2", "1" ), "OnChange" ); ...

or :

Code:
CvarHookChange( CvarRegister( "MyCvar1", "1" ), "OnChange" ); CvarHookChange( CvarRegister( "MyCvar2", "1" ), "OnChange" ); ...

or :

Code:
handleCvar1 = register_cvar( "MyCvar1", "1" ); handleCvar2 = register_cvar( "MyCvar2", "1" ); CvarHookChange( handleCvar1, "OnChange" ); CvarHookChange( handleCvar2, "OnChange" ); ...

or :

Code:
handleCvar1 = CvarRegister( "MyCvar1", "1" ); handleCvar2 = CvarRegister( "MyCvar2", "1" ); CvarHookChange( handleCvar1, "OnChange" ); CvarHookChange( handleCvar2, "OnChange" ); ...

Code:
public OnChange( handleCvar, const oldValue[], const newValue[], const cvarName[] ) {     // Called for all your cvars. }

Simple, isn't it ?

bibu 04-13-2011 17:44

Re: (Cvar Utilities) Hooking cvar question
 
Yes it is, I understood it before you edited. And I am using your first method.

I've got this now:

PHP Code:

/*Line 1774*/ if(get_pcvar_num(am_lightning_sound))
        {
                    
code
        


Quote:

L 04/13/2011 - 23:40:53: [AMXX] Run time error 10: native error (native "get_pcvar_num")
L 04/13/2011 - 23:40:53: [AMXX] [0] advanced_menu.sma::menu_handler_lightning (line 1774)
Rest:

PHP Code:

new am_lightning_sound

//in precache (using this since precache is loaded before init and I need it for precaching / playing the sounds:

CvarHookChange(register_cvar("am_lightning_sound""1"), "OnCvarChange"


Arkshine 04-13-2011 17:57

Re: (Cvar Utilities) Hooking cvar question
 
Bibu, please, when you have a problem :

- Give the full error
- Give the full code (at least a part where we can fully understand).

What is am_lightning_sound ? Where it's set ?

You don't show enough code to see what you have done exactly, nor the full error to see what happens.


All times are GMT -4. The time now is 19:49.

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