AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [SOLVED]hook cmd to register/unregister forward (https://forums.alliedmods.net/showthread.php?t=155243)

One 04-19-2011 08:05

[SOLVED]hook cmd to register/unregister forward
 
which way is better and efficienter to register and unregister my forwards.

using Cvar Utilities or ClientCommand?

Arkshine 04-19-2011 12:27

Re: hook cmd to register/unregister forward
 
Cvar Utilities is about cvar, not command.

Now all is depending what you want to do.
If you want to provide a way to customize your plugin, cvar is more appropriate. There is not reason to try to use a command.

About un/registering I don't think there is real difference.
The difference would be more when the forward is enabled.
ClientCommand will be called for all the commands and you would need to check the strings each time.
With CvarUtilities, the forward is called only on cvar value change, and for the cvar(s) specified. And in the situation you hook a bunch of cvar on the same callback, you could check the cvar pointer instead of string.

So, when the forward is enabled, Cvar Utililies would be more efficient.

One 04-20-2011 00:48

Re: hook cmd to register/unregister forward
 
the forwards are enabled. so i will use Utilities.
thanks Arkshine.

One 04-22-2011 09:51

Re: hook cmd to register/unregister forward
 
1 Attachment(s)
so i dont open a new thread.
i think we knew that this will happend :P
init:
PHP Code:

CV_Semiclip register_cvar"amx_semiclip_enabled""1" )


CvarHookChangeCV_Semiclip"SemiclipCvar" )
    if(
get_pcvar_num(CV_Semiclip)) 
    {
        
Prethink register_forwardFM_PlayerPreThink"PreThink" )
        
Postthink register_forwardFM_PlayerPostThink"PostThink" )
        
Fullpack register_forwardFM_AddToFullPack"Fullpack_Post")
    } 

PHP Code:

public SemiclipCvar()
{
    if(
get_pcvar_num(CV_Semiclip)) 
    {
        
Prethink register_forwardFM_PlayerPreThink"PreThink" )
        
Postthink register_forwardFM_PlayerPostThink"PostThink" )
        
Fullpack register_forwardFM_AddToFullPack"Fullpack_Post")
        
client_print(0,print_chat,"DEBUG: FORWARD STARTED")
    }
    else
    {
        
unregister_forwardFM_PlayerPreThinkPrethink)
        
unregister_forwardFM_PlayerPostThinkPostthink )
        
unregister_forwardFM_AddToFullPackFullpack)
        
client_print(0,print_chat,"DEBUG: FORWARD STOPPED")
    }


first problem : this will not unregister my forward.
secund : when i change the cvar ingame ( with amxx menu ) and set it to 0, for first time will the cvars value showed as Fullpack_post
you can see it in attached pic.
i changed the cvar to 0 but ...

http://forums.alliedmods.net/attachm...1&d=1303480215

Arkshine 04-22-2011 10:18

Re: hook cmd to register/unregister forward
 
You do things wrongly.

Please take your time by reading the examples provided in the module thread.
Like this one : https://forums.alliedmods.net/showth...okSpecificCvar
You see clearly how the header of the callback should be.

How I would do :

Code:
public plugin_init() {     CvarHookChange( register_cvar( "amx_semiclip_enabled", "1" ), "SemiclipCvar" ); } public SemiclipCvar( const handleCvar, const oldValue[], const newValue[], const cvarName[] ) {     switch( newValue[ 0 ] )     {         case '1' :         {             Prethink  = register_forward( FM_PlayerPreThink, "PreThink" );             Postthink = register_forward( FM_PlayerPostThink, "PostThink" );             Fullpack  = register_forward( FM_AddToFullPack, "Fullpack_Post", 1 );                         client_print( 0, print_chat, "DEBUG: FORWARD STARTED" );                     }         case '0' :         {             unregister_forward( FM_PlayerPreThink, Prethink );             unregister_forward( FM_PlayerPostThink, Postthink );             unregister_forward( FM_AddToFullPack, Fullpack, 1 );                         client_print( 0, print_chat,"DEBUG: FORWARD STOPPED" );         }     } }

You could have used str_to_num( newValue ) or get_pcvar_num( handleCvar ), but here we can avoid the use of natives.

Actually, I would use a code more safer, because people could have written wrong things.
So, I would recommendend something like to be sure :

Code:
if( clamp( str_to_num( newValue ), 0, 1 ) ) {     } else { }

EDIT :

I've just noticed something, if you use register_cvar() then CvarHookChange(), you won't be noticed the first time.
I will fix it, but I would like to encourage you to use CVarRegister() instead, where the bug doesn't exist,
also, you can provide directly a description and a bound (so no need to check manually).

EDIT2: Actually, CvarRegister may have the same problem. Just check anyway and you will see. If so, enable forwards manually like you have done in plugin_init(). It will be fixed for the next version.

So, at the end, I would do :

Code:
#include <amxmodx> #include <cvar_util> public plugin_init() {     new handleCvar = CvarRegister( "amx_semiclip_enabled", "1", "My description", .hasMin = true, .minValue = 0.0, .hasMax = true, .maxValue = 1.0 );     CvarHookChange( handleCvar, "SemiclipCvar" ); } public SemiclipCvar( const handleCvar, const oldValue[], const newValue[], const cvarName[] ) {     switch( newValue[ 0 ] )     {         case '1' :         {             Prethink  = register_forward( FM_PlayerPreThink, "PreThink" );             Postthink = register_forward( FM_PlayerPostThink, "PostThink" );             Fullpack  = register_forward( FM_AddToFullPack, "Fullpack_Post", 1 );                         client_print( 0, print_chat, "DEBUG: FORWARD STARTED" );                     }         case '0' :         {             unregister_forward( FM_PlayerPreThink, Prethink );             unregister_forward( FM_PlayerPostThink, Postthink );             unregister_forward( FM_AddToFullPack, Fullpack, 1 );                         client_print( 0, print_chat,"DEBUG: FORWARD STOPPED" );         }     } }


EDIT 3: Sorry for my answers. Actually, it's fine as it is, I mean about what i've said in my first edit. I would prefer to not change to avoid further problem.
So I would suggest finaly something like :p :

Code:
#include <amxmodx> #include <cvar_util> public plugin_init() {     new handleCvar = CvarRegister( "amx_semiclip_enabled", "1", "My description", .hasMin = true, .minValue = 0.0, .hasMax = true, .maxValue = 1.0 );     CvarHookChange( handleCvar, "SemiclipCvar" );         toggleForward( .enable = true ); } public SemiclipCvar( const handleCvar, const oldValue[], const newValue[], const cvarName[] ) {     toggleForward( .enable = newValue[ 0 ] == '1' ); } toggleForward( const bool:enable ) {     if( enable)     {         Prethink  = register_forward( FM_PlayerPreThink, "PreThink" );         Postthink = register_forward( FM_PlayerPostThink, "PostThink" );         Fullpack  = register_forward( FM_AddToFullPack, "Fullpack_Post", 1 );                 client_print( 0, print_chat, "DEBUG: FORWARD STARTED" );     }     else     {         unregister_forward( FM_PlayerPreThink, Prethink );         unregister_forward( FM_PlayerPostThink, Postthink );         unregister_forward( FM_AddToFullPack, Fullpack, 1 );                 client_print( 0, print_chat,"DEBUG: FORWARD STOPPED" );     } }

One 04-25-2011 06:35

Re: hook cmd to register/unregister forward
 
hmmm thanks but its not working :(

here my console :

Code:

] amx_semiclip_enabled
"amx_semiclip_enabled" is "0.000000"
] amx_semiclip_enabled 1
] amx_semiclip_enabled
"amx_semiclip_enabled" is "0.000000"
] amx_semiclip_enabled  0
] amx_semiclip_enabled 
"amx_semiclip_enabled"

for first its on 0 and semiclip works.
when i check the cvar its on 0.
so i try to change it to 1 & semiclip is off ( unregistered ).
i check the cvar value it showed me its on 0.000000

did i forget something ?

should i post the full code ?

Arkshine 04-25-2011 06:40

Re: hook cmd to register/unregister forward
 
Just use new handleCvar = CvarRegister( "amx_semiclip_enabled", "1" ) to see.

One 04-25-2011 06:48

Re: hook cmd to register/unregister forward
 
wow. its working. awesome.

Thanksssssssssss.
yaaaaaaaaaay

Arkshine 04-25-2011 07:12

Re: [SOLVED]hook cmd to register/unregister forward
 
Something wrong with bounds, but already fixed in the next release. 'Glad it works now.

One 04-25-2011 07:27

Re: [SOLVED]hook cmd to register/unregister forward
 
its just perfect. i like it :D
w8 for new update :P GJ


All times are GMT -4. The time now is 20:06.

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