Jump to content

[Scripting] Dodge hit


Recommended Posts

Is there a way to script a random dodge so that all damage from the hit is negated? This is what I have so far (inside of master_postinit):

 

    local oldonhitfn = inst.components.combat.onhitfn
    
    inst.components.combat.onhitfn = function(inst, attacker, damage)
        if oldonhitfn and (math.random() > 0.333333) then
            oldonhitfn(inst, attacker, damage)
        else
            inst.components.talker:Say("Dodged!")
        end
    end

 

It should cause the player to dodge every third hit and have them say "Dodged!", but instead they only say "Dodged!".

 

 

Link to comment
Share on other sites

Is this meant to be a character trait or a creature property? "onhitfn" is a set of stuff that happens after getting hit.

 

If it's a character trait, you could try to sometimes add the tag "alwaysblock" after getting hit, and else remove it. (after getting hit, the character becomes immune to the next hit... or loses the immunity)

Theory:
local function onhit(inst)
if math.random() < CHANCE then
inst:AddTag("block")
else
inst:RemoveTag("block")
end
end

function fn()
[...]
inst.components.combat.onhitfn = onhit

If it's a creature property, you can listen for the attack event and change the damage to zero in similiar fashion.

Theory:
local function dodgechance(inst)
if math.random() < CHANCE then
inst.components.combat.damage = 0
else
inst.components.combat.damage = DAMAGE
end
end

function fn()
[...]
inst:ListenForEvent("onattackother",dodgechance)
Link to comment
Share on other sites

Is this meant to be a character trait or a creature property? "onhitfn" is a set of stuff that happens after getting hit.

 

If it's a character trait, you could try to sometimes add the tag "alwaysblock" after getting hit, and else remove it. (after getting hit, the character becomes immune to the next hit... or loses the immunity)

Theory:

local function onhit(inst)

if math.random() < CHANCE then

inst:AddTag("block")

else

inst:RemoveTag("block")

end

end

function fn()

[...]

inst.components.combat.onhitfn = onhit

If it's a creature property, you can listen for the attack event and change the damage to zero in similiar fashion.

Theory:

local function dodgechance(inst)

if math.random() < CHANCE then

inst.components.combat.damage = 0

else

inst.components.combat.damage = DAMAGE

end

end

function fn()

[...]

inst:ListenForEvent("onattackother",dodgechance)

 

I meant as a character trait. It really bothers me that this effects the FOLLOWING hit, but I guess it's the same considering the player won't know when they have immunity.

 

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