htv1lla1n

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

Recommended Posts

htv1lla1n    9

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)?

Edited by htv1lla1n

Share this post


Link to post
Share on other sites
Ultroman    506

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.

  • Like 1

Share this post


Link to post
Share on other sites
htv1lla1n    9
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

  • Like 1

Share this post


Link to post
Share on other sites
htv1lla1n    9

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, ...)

 

Edited by htv1lla1n
forgot to add on what line

Share this post


Link to post
Share on other sites
Ultroman    506

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

Share this post


Link to post
Share on other sites
htv1lla1n    9
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

Share this post


Link to post
Share on other sites
Ultroman    506

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 

  • Like 1

Share this post


Link to post
Share on other sites
htv1lla1n    9
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

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now