Jump to content

Health absorb on hit (custom item)


Recommended Posts

I have problem with onattack function. My question is: how to make critical hit and health absorbtion working together on one "onattack" function?

 

local CRITCHANCE = 0.35
local HPABSORB = 1

local function onattack(weapon, attacker, target)
	  local health = target.components.health
	  local attacker = inst.components.inventoryitem.owner
      if math.random() < CRITCHANCE then
	  			health:DoDelta(-30)
				weapon.components.talker:Say("Crit!")
		end
end

local function onattack(attacker)
	  local attacker = inst.components.inventoryitem.owner
      if math.random() < HPABSORB then
	  		attacker.components.health:DoDelta(1)
		end
end

 of course im connecting it with components:

	inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(46)
	inst.components.weapon:SetOnAttack(onattack)

 

Please help!

 

Link to comment
Share on other sites

What precisely is the problem you face:

  • Is this a unique weapon?
  • Are we dealing with the player's fists?
  • Is this to do with any and all weapons on this character?

I assume you're trying to set a specific weapon in this case, although any error reports or observations could help. Here's my attempt with what you've given so far.

-- Assumption

-- file body
local CRITCHANCE = 0.35
local HPABSORB = 1

local function onattack(weapon, attacker, target)
	local health = target.components.health
	local attacker = inst.components.inventoryitem.owner

	-- Replace "1" with HPABSORB to set rate seperately if you wish
	-- attacker.components.health:DoDelta(1) -- place here if you want healing to take place on every strike

	if math.random() < CRITCHANCE then
		health:DoDelta(-30) -- value you're hitting as extra damage direct to target
		weapon.components.talker:Say("Crit!") -- prompt
		-- attacker.components.health:DoDelta(1) -- place here if you want healing to take place only for criticals
	end
end

--fn
--mainbody

	-- Weapon component section for weapon file.
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(46)
    inst.components.weapon.onattack = onattack -- onattack will find the local function, passing respected data through to function.
    
--end


 In addition: math.random() (equal to anything between 0-1) will only ever be under HPABSORB unless you want to ensure health is absorbed only when exactly 1 is generated; likely never.

Is this the intended design?

 

Edited by MorickClive
Link to comment
Share on other sites

 

51 minutes ago, MorickClive said:

In addition: math.random() (equal to anything between 0-1) will only ever be under HPABSORB unless you want to ensure health is absorbed only when exactly 1 is generated; likely never.

Well, now everything works perfect. Thank You for explain the math.random() function. I just changed HPABSORB value to 0.9999 to make sure it almost 100% chance, and made it seperate just for clean code view. 

Link to comment
Share on other sites

On 2017-5-27 at 4:36 PM, MorickClive said:

Glad to hear it worked out, all the best for your mod!

I don't get it.

local HPABSORB = 1

By choosing 1 you make it almost imposible somehow? For 100% you would need to set it to 0.9999 llike Weexer said? 

Also, i found interesting the idea of making a comment when doing critical, my chaosrevenge prefab has a stun code on  math.random() function, i tried adding: 

weapon.components.talker:Say("My comment")

But all i got was "weapon is a nil value" error. So after a while i decided to try this code:

owner.components.talker:Say("My comment")

And it worked, but now i'm really confused lol. 

Link to comment
Share on other sites

6 hours ago, Jpianist said:

I don't get it.

I think  actually I was thinking of a reverse situation - I got the "math.random() < HPABSORB" thing wrong(must have been overworked/tired, I don't know how I managed to misread it). But it would always activate unless on a rare occasion it was one.

Edited by MorickClive
Link to comment
Share on other sites

6 hours ago, Jpianist said:

I don't get it.

I think  actually i was thinking of a reverse situation - I got the "math.random() < HPABSORB" thing wrong(must have been overworked/tired, I don't know how I managed to misread it). But it would always activate unless on a rare occasion it was one.

 

owner.components.talker:Say("My comment")

Weexer might have added a talker to his weapon so that the weapon prefab talks like Lucy does, adding it to your owner will mean that your character will respond; because your character (and all other characters) draws from a common file called "player_common" where the talker component is initialised.

Link to comment
Share on other sites

7 hours ago, MorickClive said:

Weexer might have added a talker to his weapon so that the weapon prefab talks like Lucy does, adding it to your owner will mean that your character will respond; because your character (and all other characters) draws from a common file called "player_common" where the talker component is initialised.

That explains why i coudln't make it work by adding that line alone. It's okay since i wanted to make my character talk when the math.random event happent and i did with "owner" instead of "weapon". Thanks for explaining Morick!

Link to comment
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
 Share

×
  • Create New...