Jump to content

Recommended Posts

I am modding a custom hat, and I want it to strike an enemy when my char is attacked.

I tried to use this code that I found but it does not work:

inst.components.armor.ontakedamage = function(inst, damage_amount)
    print('attacked')
        local function OnAttacked(inst, data)
            --if math.random() < 0.25 then
            print('A0')
                if data.attacker ~= nil and data.attacker.components.combat then
                print('A')
                    data.attacker.components.combat:GetAttacked(inst, data.attacker.components.damage)
                    print('A1')
                    TheWorld:PushEvent("ms_sendlightningstrike", data.attacker:GetPosition())
                    print('A2')
                    data.attacker.components.health:DoDelta(-5)
                    print('A3')
                    data.attacker.components.combat:SetTarget(data.attacker)
                    print('A4')
                    data.attacker.components.combat:ShareTarget(data.attacker, 30, ShareTargetFn, 1)
                    print('A5')
                end
            --end
        end
        inst:ListenForEvent("attacked", OnAttacked)
    end

 

In my log only prints "attacked" it does not reach "A".

why are you using this line?

 inst:ListenForEvent("attacked", OnAttacked)

is the "onTakeDamage" function not enough? i think the attacked event is only fired on the player inst, not the amor inst. it should work when you put the function body directly into the take damage function.

inst.components.armor.ontakedamage = function(inst, damage_amount)
    print('attacked')
            --if math.random() < 0.25 then
            print('A0')
                if data.attacker ~= nil and data.attacker.components.combat then
                	print('A')
                    data.attacker.components.combat:GetAttacked(inst, data.attacker.components.damage)
                    print('A1')
                    TheWorld:PushEvent("ms_sendlightningstrike", data.attacker:GetPosition())
                    print('A2')
                    data.attacker.components.health:DoDelta(-5)
                    print('A3')
                    data.attacker.components.combat:SetTarget(data.attacker)
                    print('A4')
                    data.attacker.components.combat:ShareTarget(data.attacker, 30, ShareTargetFn, 1)
                    print('A5')
                end
            --end
    end

 

5 hours ago, krylincy said:

why are you using this line?


 inst:ListenForEvent("attacked", OnAttacked)

is the "onTakeDamage" function not enough? i think the attacked event is only fired on the player inst, not the amor inst. it should work when you put the function body directly into the take damage function.


inst.components.armor.ontakedamage = function(inst, damage_amount)
    print('attacked')
            --if math.random() < 0.25 then
            print('A0')
                if data.attacker ~= nil and data.attacker.components.combat then
                	print('A')
                    data.attacker.components.combat:GetAttacked(inst, data.attacker.components.damage)
                    print('A1')
                    TheWorld:PushEvent("ms_sendlightningstrike", data.attacker:GetPosition())
                    print('A2')
                    data.attacker.components.health:DoDelta(-5)
                    print('A3')
                    data.attacker.components.combat:SetTarget(data.attacker)
                    print('A4')
                    data.attacker.components.combat:ShareTarget(data.attacker, 30, ShareTargetFn, 1)
                    print('A5')
                end
            --end
    end

 

If I don't use the function OnAttacked, the game gives me error because "data" do not exists...

There are already two equippables that strike enemies when you get attacked. The bramble armor and the scale armor. You can copy how those items implement their spikey armor effect.

There are plenty of equippables that give players unique tags when equipped such as the beefalo hat.

2 hours ago, JPTRON said:

Is there any option to give a tag to player when wearing this specific hat? Is so, I could do it on the player.

 

  • Like 1
On 6/1/2020 at 2:16 AM, IronHunter said:

There are already two equippables that strike enemies when you get attacked. The bramble armor and the scale armor. You can copy how those items implement their spikey armor effect.

There are plenty of equippables that give players unique tags when equipped such as the beefalo hat.

 

Thank you man, I was able to do it :D

 

	local function OnBlocked(owner, data)
		if math.random() < 0.25 then
			if data.attacker ~= nil and
				not (data.attacker.components.health ~= nil and data.attacker.components.health:IsDead()) then			
						TheWorld:PushEvent("ms_sendlightningstrike", owner:GetPosition())					
						data.attacker.components.health:DoDelta(-5)               
            end
    
		end
	end

 

  • Like 1

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
×
  • Create New...