JPTRON Posted May 16, 2020 Share Posted May 16, 2020 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". Link to comment https://forums.kleientertainment.com/forums/topic/118303-mod-strike-attacker-when-equipped-a-custom-hat/ Share on other sites More sharing options...
krylincy Posted May 18, 2020 Share Posted May 18, 2020 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 Link to comment https://forums.kleientertainment.com/forums/topic/118303-mod-strike-attacker-when-equipped-a-custom-hat/#findComment-1334775 Share on other sites More sharing options...
JPTRON Posted May 19, 2020 Author Share Posted May 19, 2020 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... Link to comment https://forums.kleientertainment.com/forums/topic/118303-mod-strike-attacker-when-equipped-a-custom-hat/#findComment-1334818 Share on other sites More sharing options...
krylincy Posted May 20, 2020 Share Posted May 20, 2020 then you should try to add the event to the player, not the armor inst. Link to comment https://forums.kleientertainment.com/forums/topic/118303-mod-strike-attacker-when-equipped-a-custom-hat/#findComment-1335122 Share on other sites More sharing options...
JPTRON Posted May 31, 2020 Author Share Posted May 31, 2020 Is there any option to give a tag to player when wearing this specific hat? Is so, I could do it on the player. Link to comment https://forums.kleientertainment.com/forums/topic/118303-mod-strike-attacker-when-equipped-a-custom-hat/#findComment-1338393 Share on other sites More sharing options...
IronHunter Posted June 1, 2020 Share Posted June 1, 2020 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. 1 Link to comment https://forums.kleientertainment.com/forums/topic/118303-mod-strike-attacker-when-equipped-a-custom-hat/#findComment-1338487 Share on other sites More sharing options...
JPTRON Posted June 4, 2020 Author Share Posted June 4, 2020 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 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 1 Link to comment https://forums.kleientertainment.com/forums/topic/118303-mod-strike-attacker-when-equipped-a-custom-hat/#findComment-1339772 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now