Raised This Month: $12 Target: $400
 3% 

Solved How to set SDKHook on round_start correctly?


Post New Thread Reply   
 
Thread Tools Display Modes
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 10-24-2018 , 07:09   Re: How to set SDKHook on round_start correctly?
Reply With Quote #11

Quote:
Originally Posted by 8guawong View Post
how do you know the game engine unhooks after round end?
Quote:
Originally Posted by Dragokas
After hooking it once, engine never unhook it again itself.
Sorry. It was incorrect test. Engine really unhook it automatically on map end as I stated in 1st post (at least in L4D game). Tested on such code:
Code:
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>

bool g_bHooked[MAXPLAYERS+1];

public void OnPluginStart()
{
	HookEvent("player_team", Event_Player_Team);
}

void Set_SDKHook(int client)
{
	if (!g_bHooked[client])
	{
		SDKHook(client, SDKHook_OnTakeDamage, OnTakeDamage);
		g_bHooked[client] = true;
	}
}

public Action Event_Player_Team(Event event, char[] name, bool dontBroadcast)
{
	int UserId = event.GetInt("userid");
	if (UserId != 0)
	{
		int client = GetClientOfUserId(UserId);
		if (client != 0)
		{
			int team = event.GetInt("team");
			if (team == 2) // joined to survivor
				Set_SDKHook(client);
		}
	}
}

public Action OnTakeDamage(int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &weapon, float damageForce[3], float damagePosition[3])
{
	if (GetClientTeam(victim) == 2)
	{
		damage = 0.0;
		return Plugin_Handled;		
	}
	return Plugin_Continue;
}
After vote for new map, damage is taken.

So, considering all recommendations and my wish not to hook every possible clients, my final code will be:

Code:
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>

public void OnPluginStart()
{
	HookEvent("player_disconnect", 		Event_PlayerDisconnect);
	HookEvent("player_team", Event_Player_Team);
}

public Action Event_Player_Team(Event event, char[] name, bool dontBroadcast)
{
	int UserId = event.GetInt("userid");
	if (UserId != 0)
	{
		int client = GetClientOfUserId(UserId);
		if (client != 0)
		{
			int team = event.GetInt("team");
			if (team == 2) // joined to survivor
				SDKHook(client, SDKHook_OnTakeDamage, OnTakeDamage);
		}
	}
}

public Action Event_PlayerDisconnect(Event event, char[] name, bool dontBroadcast)
{
	int UserId = event.GetInt("userid");
	if (UserId != 0)
	{
		int client = GetClientOfUserId(UserId);
		if (client != 0)
			SDKUnhook(client, SDKHook_OnTakeDamage, OnTakeDamage);
	}
}

public Action OnTakeDamage(int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &weapon, float damageForce[3], float damagePosition[3])
{
	if (GetClientTeam(victim) == 2)
	{
		damage = 0.0;
		return Plugin_Handled;		
	}
	return Plugin_Continue;
}
Here, I don't care anymore about flags previously preventing attempt to hook/unhook twice since it looks like it doesn't raise errors.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 10-24-2018 , 15:16   Re: How to set SDKHook on round_start correctly?
Reply With Quote #12

Just for future reference:

When a client disconnects, it counts as their entity being deleted. You don't need to use SDKUnhook on clients when they leave since it's automatic.
__________________
Psyk0tik is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 10-25-2018 , 02:29   Re: How to set SDKHook on round_start correctly?
Reply With Quote #13

Thanks for letting me know, Crasher.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 10-25-2018 , 06:37   Re: How to set SDKHook on round_start correctly?
Reply With Quote #14

PHP Code:
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>

public void OnPluginStart()
{
    
HookEvent("player_team"Event_Player_Team);
}

public 
Action Event_Player_Team(Event eventchar[] namebool dontBroadcast)
{
    
int UserId event.GetInt("userid");
    if (
UserId != 0)
    {
        
int client GetClientOfUserId(UserId);
        if (
client != 0)
        {
            
int team event.GetInt("team");
            if (
team == 2// joined to survivor
                
SDKHook(clientSDKHook_OnTakeDamageOnTakeDamage);
            else if (
team)
            {
                
SDKUnhook(clientSDKHook_OnTakeDamageOnTakeDamage);
                
// you hook the damage in player_team, you should unhook it when the player changes his team (then you can remove if(GetClientTeam(client) == 2) from OnTakeDamage)
            
}
        }
    }
}

public 
Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetypeint &weaponfloat damageForce[3], float damagePosition[3])
{
    return 
Plugin_Handled;        

Ilusion9 is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 10-25-2018 , 07:00   Re: How to set SDKHook on round_start correctly?
Reply With Quote #15

Quote:
Originally Posted by Ilusion9
then you can remove if(GetClientTeam(client) == 2) from OnTakeDamage
Right. Thank you. It make sense.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 10-25-2018 , 07:04   Re: How to set SDKHook on round_start correctly?
Reply With Quote #16

I think checking for "oldteam" will be better:

PHP Code:
#include <sourcemod> 
#include <sdktools> 
#include <sdkhooks> 

public void OnPluginStart() 

    
HookEvent("player_team"Event_Player_Team); 


public 
Action Event_Player_Team(Event eventchar[] namebool dontBroadcast

    
int UserId event.GetInt("userid"); 
    if (
UserId != 0
    { 
        
int client GetClientOfUserId(UserId); 
        if (
client != 0
        { 
            
int team event.GetInt("team"); 
            if (
team == 2// joined to survivor 
                
SDKHook(clientSDKHook_OnTakeDamageOnTakeDamage); 
            else if (
event.GetInt("oldteam") == 2)
            {
                
SDKUnhook(clientSDKHook_OnTakeDamageOnTakeDamage); 
            } 
        } 
    } 


public 
Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetypeint &weaponfloat damageForce[3], float damagePosition[3]) 

    return 
Plugin_Handled;         

__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 10-25-2018 , 11:00   Re: How to set SDKHook on round_start correctly?
Reply With Quote #17

Doesn't L4D only have like 8 players? Seems like this should suffice:

PHP Code:
public void OnClientPutInServer(int client)
{
    
SDKHook(clientSDKHook_OnTakeDamageOnTakeDamage);
}

public 
Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetypeint &weaponfloat damageForce[3], float damagePosition[3])
{
    if (
GetClientTeam(victim) == 2) {
        return 
Plugin_Handled;
    }

    return 
Plugin_Continue;

__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.

Last edited by DJ Tsunami; 10-25-2018 at 11:00.
DJ Tsunami is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 10-25-2018 , 11:37   Re: How to set SDKHook on round_start correctly?
Reply With Quote #18

I don't need extra unnecessary hooks.
OnClientPutInServer() => @all, team 2 and team 3.
My last code (with all help in this topic) looks most efficient and fast.

Quote:
Doesn't L4D only have like 8 players?
I saw 16.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]

Last edited by Dragokas; 10-25-2018 at 11:38.
Dragokas is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 10-28-2018 , 03:50   Re: How to set SDKHook on round_start correctly?
Reply With Quote #19

Quote:
Originally Posted by Dragokas View Post
Impossible. GetClientTeam == 0 at that point of time.

Looks like the only opportunity is player_team or round_start + timer.


Right.

... All right. It's enough info for me. Thanks @all.
Hook on all players no matter who and if the team == 3 then execute your code.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 10-28-2018 , 06:26   Re: How to set SDKHook on round_start correctly?
Reply With Quote #20

eyal282,
checking for "team == 3" in callback is additional CPU cycles consuming.
No need to check if you can initially set hook on necessary clients only.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
Reply


Thread Tools
Display Modes

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 22:17.


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