AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   How to hook on late load? [Solved] (https://forums.alliedmods.net/showthread.php?t=256385)

EngHell 01-25-2015 15:29

How to hook on late load? [Solved]
 
I have done this to hook the players picking items when theres a knife round, everything is working fine except it let players get droped items, but since i did a late load of the plugin, i think thats why it's not working, because i did the hook OnClientPutInServer() that is why the plugin is not working well, but since i ussually load and unload plugins every x time, i'll like to get a way to handle it, theres the code:
PHP Code:

public OnClientPutInServer(client) {
    
SDKHook(clientSDKHook_WeaponCanUseBlockPickup);
}

public 
OnClientDisconnect(client) {
    
SDKUnhook(clientSDKHook_WeaponCanUseBlockPickup);


and

PHP Code:

public Action:BlockPickup(clientweapon){
    if (
g_enabled && g_isKnifeRound) {
        
PrintHintText(client"%t","Knife round is enabled");
        return 
Plugin_Handled;
    }
    return 
Plugin_Continue;


How i can manage to hook it when i late load the plugin?

pcmaster 01-25-2015 15:41

Re: How to hook on late load?
 
You can use AskPluginLoad2

11530 01-25-2015 16:24

Re: How to hook on late load?
 
PHP Code:

new bool:g_bLateLoaded false;

public 
APLRes:AskPluginLoad2(Handle:myselfbool:lateString:error[], err_max)
{
  
g_bLateLoaded late;
  return 
APLRes_Success;
}

public 
OnPluginStart()
{
  
//...

  
if (g_bLateLoaded)
  {
    for (new 
1<= MaxClientsi++)
    {
      if (
IsClientInGame(i)) //Add other checks, e.g. IsClientSourceTV, if needed
      
{
        
SDKHook(iSDKHook_WeaponCanUseBlockPickup);
      }
    }
  }



EngHell 01-25-2015 16:34

Re: How to hook on late load?
 
Quote:

Originally Posted by 11530 (Post 2253975)
PHP Code:

new bool:g_bLateLoaded false;

public 
APLRes:AskPluginLoad2(Handle:myselfbool:lateString:error[], err_max)
{
  
g_bLateLoaded late;
  return 
APLRes_Success;
}

public 
OnPluginStart()
{
  
//...

  
if (g_bLateLoaded)
  {
    for (new 
1<= MaxClientsi++)
    {
      if (
IsClientInGame(i)) //Add other checks, e.g. IsClientSourceTV, if needed
      
{
        
SDKHook(iSDKHook_WeaponCanUseBlockPickup);
      }
    }
  }



Thanks, it did the job well, but if I unload the plugin, should I close the hooks? An if yes, what's the best way to do it?

KissLick 01-25-2015 17:09

Re: How to hook on late load?
 
You dont need to close sdhooks hooks, sdkhooks do it for you when entity is destroyed/player disconnected.

psychonic 01-25-2015 17:15

Re: How to hook on late load?
 
Any entities hooked with SDKHooks in a plugin will be automatically unhooked when the plugin unloads.

EngHell 01-25-2015 20:32

Re: How to hook on late load?
 
Quote:

Originally Posted by KissLick (Post 2253986)
You dont need to close sdhooks hooks, sdkhooks do it for you when entity is destroyed/player disconnected.

Quote:

Originally Posted by psychonic (Post 2253988)
Any entities hooked with SDKHooks in a plugin will be automatically unhooked when the plugin unloads.

Then This code isn't needed?
PHP Code:

public OnClientDisconnect(client) {
    
SDKUnhook(clientSDKHook_WeaponCanUseBlockPickup);



psychonic 01-25-2015 20:37

Re: How to hook on late load?
 
Quote:

Originally Posted by EngHell (Post 2254036)
Then This code isn't needed?
PHP Code:

public OnClientDisconnect(client) {
    
SDKUnhook(clientSDKHook_WeaponCanUseBlockPickup);



That is correct.

Chdata 01-26-2015 02:17

Re: How to hook on late load? [Solved]
 
That's correct for a different reason. You don't need to unhook when a client disconnects from the server.

I'm not sure exactly why though. It's either because you can't hook something twice (which I doubt, you probably actually can and have double repeating code), or because client entities don't actually get "killed" or "removed" - at least that's my theory as I don't know - that when someone connects his control is exerted on an existing player entity that's being unused.


Or maybe SM does unhook clients on disconnect, who knows.

Dr. Greg House 01-26-2015 02:43

Re: How to hook on late load? [Solved]
 
I have seen many plugins hook stuff on player spawn. Now I'm wondering how long these hooks stay active. And if hooktype + entindex + callback-address is unique so that it wouldn't be hooked or called multiple times.


All times are GMT -4. The time now is 22:08.

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