Jump to content

Recommended Posts

So i'm making a weapon that does more damage against dragon-type mobs (for now i'm including just dragonfly).
The problem is that i dont know how to access the "target" to check if it has the tag i'm looking for. This part of the code is in the prefab file. I tried to use data.target and just target, as I saw on similar problems but still no luck. It doesn't crash, but my guess is that data is always nil. 

    local damage = 2000  -- absurd damage is just to test it, after i get it working i will fine-tune it
    if data ~= nil then
        if data.target ~= nil and data.target.combat ~= nil then
            if data.target:HasTag("dragonfly") then  -- absurd damage is just to test it, after i get it working i will fine-tune it
                damage = 27500
            end
        end
    end
    
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(damage)

 

Any ideas?

Thanks for the help in advance :D

Edited by Zardexrlz

Try adding your function to the weapon's onattack with weapon:SetOnAttack like

local function fireflykiller(target)
    local damage = 10 -- Default damage

    if target:HasTag("dragonfly") then
        damage = 30000
    end
    
    inst.components.weapon:SetDamage(damage)
end      

inst.components.weapon:SetOnAttack(fireflykiller)

(Disclaimer: I'm kind of a terrible coder, this code probably wouldn't work, or at least, would impact performance a bit since it runs the function everytime it attacks anything)

Edited by Rosten
Added example code, fixed a bug which allowed Reaper to reach unintended locations with Shadow Step
local function OnAttack(inst, attacker, target)
	if target:HasTag("dragonfly") then
		target.components.combat:GetAttacked(attacker, 200, inst)--200 is the bonus damage + the normal weapon damage
	end
end

inst.components.weapon:SetOnAttack(OnAttack) 

 

7 hours ago, Rosten said:

Try adding your function to the weapon's onattack with weapon:SetOnAttack like

local function fireflykiller(target)
    local damage = 10 -- Default damage

    if target:HasTag("dragonfly") then
        damage = 30000
    end
    
    inst.components.weapon:SetDamage(damage)
end      

inst.components.weapon:SetOnAttack(fireflykiller)

(Disclaimer: I'm kind of a terrible coder, this code probably wouldn't work, or at least, would impact performance a bit since it runs the function everytime it attacks anything)

 

6 hours ago, Aquaterion said:

local function OnAttack(inst, attacker, target)
	if target:HasTag("dragonfly") then
		target.components.combat:GetAttacked(attacker, 200, inst)--200 is the bonus damage + the normal weapon damage
	end
end

inst.components.weapon:SetOnAttack(OnAttack) 

 

Thanks you both, it worked :D. Didn't see any performance issues @Rosten. Do you or @Aquaterion knows why exactly my code wouldn't work? I'm learning lua now so I want to understand all that I can.

Thanks :D

2 hours ago, Zardexrlz said:

 

Thanks you both, it worked :D. Didn't see any performance issues @Rosten. Do you or @Aquaterion knows why exactly my code wouldn't work? I'm learning lua now so I want to understand all that I can.

Thanks :D

Reason being that your function is never called. It's just kind of there in the code. Adding it to OnAttack just makes it so that it runs whenever you hit something with your weapon.

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