Raised This Month: $ Target: $400
 0% 

[SOLVED]hook cmd to register/unregister forward


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
One
Veteran Member
Join Date: Oct 2008
Location: Hardstyle-eSports.de
Old 04-19-2011 , 08:05   [SOLVED]hook cmd to register/unregister forward
Reply With Quote #1

which way is better and efficienter to register and unregister my forwards.

using Cvar Utilities or ClientCommand?
__________________
One is offline
Send a message via ICQ to One Send a message via AIM to One Send a message via MSN to One Send a message via Yahoo to One Send a message via Skype™ to One
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-19-2011 , 12:27   Re: hook cmd to register/unregister forward
Reply With Quote #2

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.
__________________
Arkshine is offline
One
Veteran Member
Join Date: Oct 2008
Location: Hardstyle-eSports.de
Old 04-20-2011 , 00:48   Re: hook cmd to register/unregister forward
Reply With Quote #3

the forwards are enabled. so i will use Utilities.
thanks Arkshine.
__________________
One is offline
Send a message via ICQ to One Send a message via AIM to One Send a message via MSN to One Send a message via Yahoo to One Send a message via Skype™ to One
One
Veteran Member
Join Date: Oct 2008
Location: Hardstyle-eSports.de
Old 04-22-2011 , 09:51   Re: hook cmd to register/unregister forward
Reply With Quote #4

so i dont open a new thread.
i think we knew that this will happend
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 ...

Attached Thumbnails
Click image for larger version

Name:	test.JPG
Views:	953
Size:	23.5 KB
ID:	85100  
__________________
One is offline
Send a message via ICQ to One Send a message via AIM to One Send a message via MSN to One Send a message via Yahoo to One Send a message via Skype™ to One
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-22-2011 , 10:18   Re: hook cmd to register/unregister forward
Reply With Quote #5

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" );     } }
__________________

Last edited by Arkshine; 04-22-2011 at 17:12.
Arkshine is offline
One
Veteran Member
Join Date: Oct 2008
Location: Hardstyle-eSports.de
Old 04-25-2011 , 06:35   Re: hook cmd to register/unregister forward
Reply With Quote #6

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 ?
__________________
One is offline
Send a message via ICQ to One Send a message via AIM to One Send a message via MSN to One Send a message via Yahoo to One Send a message via Skype™ to One
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-25-2011 , 06:40   Re: hook cmd to register/unregister forward
Reply With Quote #7

Just use new handleCvar = CvarRegister( "amx_semiclip_enabled", "1" ) to see.
__________________
Arkshine is offline
One
Veteran Member
Join Date: Oct 2008
Location: Hardstyle-eSports.de
Old 04-25-2011 , 06:48   Re: hook cmd to register/unregister forward
Reply With Quote #8

wow. its working. awesome.

Thanksssssssssss.
yaaaaaaaaaay
__________________
One is offline
Send a message via ICQ to One Send a message via AIM to One Send a message via MSN to One Send a message via Yahoo to One Send a message via Skype™ to One
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-25-2011 , 07:12   Re: [SOLVED]hook cmd to register/unregister forward
Reply With Quote #9

Something wrong with bounds, but already fixed in the next release. 'Glad it works now.
__________________
Arkshine is offline
One
Veteran Member
Join Date: Oct 2008
Location: Hardstyle-eSports.de
Old 04-25-2011 , 07:27   Re: [SOLVED]hook cmd to register/unregister forward
Reply With Quote #10

its just perfect. i like it
w8 for new update GJ
__________________
One is offline
Send a message via ICQ to One Send a message via AIM to One Send a message via MSN to One Send a message via Yahoo to One Send a message via Skype™ to One
Reply



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 20:06.


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