Jump to content

[Coding help] Can't use spell on targets


Recommended Posts

Greetings, everyone. Would like to have some assistance, please. I have a little problem: i cant use my item, (its supposed to spawn lightning at target location)on target(inst.components.spellcaster.canuseontargets = true), character says: i can't do this.

I can cast spell at point(inst.components.spellcaster.canuseonpoint = true) but then spell doesn't work properly: lightning always hits only caster.

My code is following: 

local function lSpell(target, pos)
                           TheWorld:PushEvent("ms_sendlightningstrike", target:GetPosition())
                    end

 

I tried alternative way:

Spoiler

 

      local ents = TheSim:FindEntities(pos.x, pos.y, pos.z, 10, {"freezable"}, {"player"})
    for k, v in pairs(ents) do
        lightningSpell(inst, v)

local function lightningSpell(inst, target)

--local x, y, z = target:GetPosition()
                local attacker = inst.components.inventoryitem.owner
                                        attacker.AnimState:PlayAnimation("atk_pre")
            attacker.AnimState:PushAnimation("atk", false)
            attacker.SoundEmitter:PlaySound("dontstarve/wilson/attack_whoosh")
    inst:DoTaskInTime(0.2, function() 
        attacker.components.sanity:DoDelta(-TUNING.SANITY_LARGE * 0.9 )
                                        attacker.AnimState:PlayAnimation("atk_pre")
            attacker.AnimState:PushAnimation("atk", false)
            attacker.SoundEmitter:PlaySound("dontstarve/wilson/attack_whoosh")
                    local max_tries = 4
                    for k = 1,max_tries do
                    local player2 = FindClosestPlayerToInst(inst, 10, true)
                        local pos = player2:GetPosition()
                            TheWorld:PushEvent("ms_sendlightningstrike", pos)
                    end
                            if target.prefab == "dragonfly" then
        target.components.health:DoDelta(-70)
    end
            end)

end

 

but then lightning hits every target in range. Any ideas how to make it work?

Edited by Zer000
Link to comment
Share on other sites

I believe your issue is this part.

  local ents = TheSim:FindEntities(pos.x, pos.y, pos.z, 10, {"freezable"}, {"player"})
    for k, v in pairs(ents) do
        lightningSpell(inst, v)

Right now what it's doing is saying find all the entities around the player.

For each entity(ents) cast the lightningSpell on them.

I would take a look at a ranged weapon (fire staff, ice staff, dart) to target your enemy instead.

If you're just having the player cast the spell when the player attacks it's much easier.

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