Jump to content

[SOLVED] How to get percentage of target's health & change weapon damage based on it?


dezgasting

Recommended Posts

Want to create a character who will execute the enemy under certain threshold of health. Basically, he deals 99999 damage to targets, who're under, let's say, 30% of max health. How do I do that, and is there a way to make him execute certain enemies only under certain thresholds (for example, he'll execute a Spider when the Spider/any other low-health mob (<200hp max) is under 50%; but will execute a Tiger Shark/any other boss only if she's under 10% et cetera.)

I have general ideas about it, but the most important part is how do I get the game to check for the current health of the enemy and see if it's under a 30% threshold with a certain weapon (that doesn't deal DoT if it's important)?

Link to comment
Share on other sites

That's actually an easy one. The CalcDamage function of the combat component has everything you need for this; target, weapon and current multiplier.

local defaultDamageSettings = { executeThreshold = 0.30, executeDamage = 99999 }

local damageSettings = {
	spider = defaultDamageSettings,
	spider_warrior = { executeThreshold = 0.20, executeDamage = 99999 },
	bearger = { executeThreshold = 0.05, executeDamage = 9999 },
}

local oldCalcDamage = inst.components.combat.CalcDamage
inst.components.combat.CalcDamage = function(self, target, weapon, multiplier, ...)
	local result = oldCalcDamage(self, target, weapon, multiplier, ...)
	if result ~= 0 and weapon ~= nil and weapon.prefab == "someprefabname"
	and target ~= nil and target:IsValid() and target.components.health then
		local targetDamageSettings = damageSettings[target.prefab] or defaultDamageSettings
		if target.components.health:GetPercent() <= targetDamageSettings.executeThreshold then
			return targetDamageSettings.executeDamage
		else
			return result
		end
	else
		return result
	end
end

You can play around with that. You can do a bunch of different things with that as your base template. You can even put in specific damage numbers for specific weapons like this:

local defaultDamageSettings = { executeThreshold = 0.30, executeDamage = 5000, myWeaponExecuteDamage = 99999 }

local damageSettings = {
	spider = defaultDamageSettings,
	spider_warrior = { executeThreshold = 0.20, executeDamage = 99999, myWeaponExecuteDamage = 99999 },
	bearger = { executeThreshold = 0.05, executeDamage = 9999, myWeaponExecuteDamage = 99999 },
}

And then move the weapon-part of the if-statement inside the if-statement as its own check.

Link to comment
Share on other sites

17 hours ago, Ultroman said:

That's actually an easy one. The CalcDamage function of the combat component has everything you need for this; target, weapon and current multiplier.


local defaultDamageSettings = { executeThreshold = 0.30, executeDamage = 99999 }

local damageSettings = {
	spider = defaultDamageSettings,
	spider_warrior = { executeThreshold = 0.20, executeDamage = 99999 },
	bearger = { executeThreshold = 0.05, executeDamage = 9999 },
}

local oldCalcDamage = inst.components.combat.CalcDamage
inst.components.combat.CalcDamage = function(self, target, weapon, multiplier, ...)
	local result = oldCalcDamage(self, target, weapon, multiplier, ...)
	if result ~= 0 and weapon ~= nil and weapon.prefab == "someprefabname"
	and target ~= nil and target:IsValid() and target.components.health then
		local targetDamageSettings = damageSettings[target.prefab] or defaultDamageSettings
		if target.components.health:GetPercent() <= targetDamageSettings.executeThreshold then
			return targetDamageSettings.executeDamage
		else
			return result
		end
	else
		return result
	end
end

You can play around with that. You can do a bunch of different things with that as your base template. You can even put in specific damage numbers for specific weapons like this:


local defaultDamageSettings = { executeThreshold = 0.30, executeDamage = 5000, myWeaponExecuteDamage = 99999 }

local damageSettings = {
	spider = defaultDamageSettings,
	spider_warrior = { executeThreshold = 0.20, executeDamage = 99999, myWeaponExecuteDamage = 99999 },
	bearger = { executeThreshold = 0.05, executeDamage = 9999, myWeaponExecuteDamage = 99999 },
}

And then move the weapon-part of the if-statement inside the if-statement as its own check.

Thank you so much! I knew it was something easy but I couldn't exactly figure out how to get the actual health of the enemy. 
You're my savior xd

Link to comment
Share on other sites

Yo, uh... We're seeming to forget something cuz now it crashes with "attempt to index global 'inst' (a nil value)". 

 

On this part.

Quote

local oldCalcDamage = inst.components.combat.CalcDamage
inst.components.combat.CalcDamage = function(self, target, weapon, multiplier, ...)

 

Link to comment
Share on other sites

13 hours ago, Ultroman said:

Where did you put the code? Show me your character prefam lua file. Attach it to a reply here.

Ok, I played with it a bit and now it crashes with a different error: "variable 'inst' is not declared"

i feel ashamed for showing my code because i'm super inexperienced and i suck but oh well gotta learn it seems

 

there. i probably screwed up somewhere didn't i?

pyke.lua

Link to comment
Share on other sites

1 hour ago, Ultroman said:

The whole code snippet has to go inside your fn function.

Like this: pyke.lua

If it complains about the defaultDamageSettings or damageSettings missing, use this instead: pyke2.lua 

funny thing is, i tried to put it in but it still gave me the same error..

...or i WANTED to try it out, but I got distracted. anyways IT FINALLY DIDN'T CRASH WOOHOOOOOOOOOO ty again bro

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...