Raised This Month: $32 Target: $400
 8% 

Solved [TF2] Medic Script Help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ravrob
Junior Member
Join Date: Oct 2019
Location: The Ice
Old 07-03-2020 , 23:06   [TF2] Medic Script Help
Reply With Quote #1

I'm fairly new to programming as a whole and I'm slowly starting to understand this so I apologize in advance if I don't understand. To sum up what I'm trying to accomplish here, I'm trying to replicate something I once saw where if a medic healed someone they'd get uber and crits while they were being healed. I actually managed to do this... sorta

Here's the code:
Code:
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <tf2>
#include <tf2_stocks>

Handle g_hEnabled;

public void OnPluginStart()
{
	g_hEnabled = CreateConVar("VSH_Medic", "1", "If 1 anyone healed by a medic will have uber and crits.");

	HookEvent("player_healed", Event_PlayerHealed, EventHookMode_Post);
}

public Action Event_PlayerHealed(Event event, const char[] name, bool dontBroadcast)
{
	if(GetConVarInt(g_hEnabled) == 1)
	{
		int patientId = event.GetInt("patient");
		int healerId  = event.GetInt("healer");
		int patient = GetClientOfUserId(patientId);
		int healer  = GetClientOfUserId(healerId);
		if(TF2_GetPlayerClass(healer) == TF2_GetClass("medic"))
		{
			TF2_AddCondition(patient, TFCond_UberchargedCanteen);
			TF2_AddCondition(patient, TFCond_CritCanteen);
		}
	}
	return Plugin_Continue;
}
Now it did exactly what I told it to, but as it turns out the event "player_healed" just applies to lost health being regained which isn't quite what I want so I wanted to know if there was an alternative way to maybe detect when a person is being healed. And yes I know according to this code the conditions would be applied forever I don't really know how to remove conditions using events. Advice for any of this including my formatting and such would be greatly appreciated.

Last edited by Ravrob; 07-04-2020 at 03:40.
Ravrob is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 07-04-2020 , 00:55   Re: [TF2] Medic Script Help
Reply With Quote #2

One simple approach would be to iterate over all players, check if their active weapon has a healing target (and is a medigun), then add your condition:

Code:
public void OnGameFrame() {
    // you could also add a frame skip e.g. if (GetGameTickCount() % 3) { return; }
    
    for (int i = 1; i <= MaxClients; i++) {
        if (!IsClientInGame(i) || !IsPlayerAlive(i)) {
            // player isn't available
            continue;
        }
        
        int activeWeapon = GetEntPropEnt(i, Prop_Send, "m_hActiveWeapon");
        if (!IsValidEntity(activeWeapon) || !HasEntProp(activeWeapon, Prop_Send, "m_hHealingTarget")) {
            // no weapon active or weapon isn't medigun
            continue;
        }
        
        int healTarget = GetEntPropEnt(activeWeapon, Prop_Send, "m_hHealingTarget");
        if (!IsValidEntity(healTarget) || healTarget < 1 || healTarget > MaxClients) {
            // no heal target, or heal target isn't player
            continue;
        }
        
        // player `i` is healing player `healTarget` with a medigun, do stuff here
    }
}
Since that runs every frame, you can set a duration on TF2_AddCondition that is long enough that the uber / kritz is still applied by the next time the check runs.

If anything's unclear, just ask.

----

Further advice / review:
  • Your formatting style is fine.
  • Replace Handle g_hEnabled; with ConVar g_hEnabled; -- this lets you use the ConVar methodmap, and you can then use g_hEnabled.BoolValue instead of GetConVarInt(g_hEnabled). You can also drop the == 1; all non-zero values are treated as "truthy", and will evaluate to true.
  • Use the TFClass_Medic value instead of doing the lookup with TF2_GetClass("medic").
  • For future event hooks, you can also remove patientId and healerId by inlining those operations like so:
    Code:
    int patient = GetClientOfUserId(event.GetInt("patient"));
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 07-04-2020 at 01:14.
nosoop is offline
Ravrob
Junior Member
Join Date: Oct 2019
Location: The Ice
Old 07-04-2020 , 03:36   Re: [TF2] Medic Script Help
Reply With Quote #3

Thank you so much! This worked perfectly, I had no idea Sourcemod had a OnGameFrame function that is very useful. I really appreciate your help and advice!
Ravrob is offline
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:02.


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