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

Solved How to set SDKHook on round_start correctly?


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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 #12

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
 



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 07:00.


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