Jump to content

How to make custom dialogue on spell cast


Recommended Posts

I have a sword with the spell teleport I want to make it so that on casting the spell on the mobs to say a phrase like "begone". How to do? And is it possible for me to make it so that each spell cast, a few points of hunger is reduced?

Link to comment
Share on other sites

@lordfool

 

Call this to make your character say something:

local cry = "begone"local duration = 2-- Where inst is the player instanceinst.components.talker:Say{Line(cry, duration)}

-

 

Can call this to reduce hunger

local hunger_cost = 5-- where inst is the player instanceinst.components.hunger:DoDelta(hunger_cost)

-

 

Do them both when you cast the spell, probably using OnAttack or OnCast or something of the sort.

Link to comment
Share on other sites

@Blueberrys

I cant seem to get it to work, I dont know if I put it in the right place. Heres my weapon lua

 

local assets =

{
-- Animation files used for the item.
Asset("ANIM", "anim/katana.zip"),
Asset("ANIM", "anim/swap_katana.zip"),
 
-- Inventory image and atlas file used for the item.
    Asset("ATLAS", "images/inventoryimages/katana.xml"),
    Asset("IMAGE", "images/inventoryimages/katana.tex"),
}
 
-------------------Ruin Hat Function --------------Forcefield-------------------
 
    local function ruinshat_proc(inst, owner)
        inst:AddTag("forcefield")
        inst.components.armor:SetAbsorption(TUNING.FULL_ABSORPTION)
        local fx = SpawnPrefab("forcefieldfx")
        fx.entity:SetParent(owner.entity)
        fx.Transform:SetPosition(0, 0.2, 0)
        local fx_hitanim = function()
            fx.AnimState:PlayAnimation("hit")
            fx.AnimState:PushAnimation("idle_loop")
        end
        fx:ListenForEvent("blocked", fx_hitanim, owner)
 
        inst.active = true
 
        owner:DoTaskInTime(--[[Duration]] TUNING.ARMOR_RUINSHAT_DURATION, function()
            fx:RemoveEventCallback("blocked", fx_hitanim, owner)
            fx.kill_fx(fx)
            if inst:IsValid() then
                inst:RemoveTag("forcefield")
inst.components.armor.ontakedamage = nil
                inst.components.armor:SetAbsorption(TUNING.ARMOR_RUINSHAT_ABSORPTION)
                owner:DoTaskInTime(--[[Cooldown]] TUNING.ARMOR_RUINSHAT_COOLDOWN, function() inst.active = false end)
            end
        end)
    end
 
local function tryproc(inst, owner)
    if not inst.active and math.random() < --[[ Chance to proc ]] TUNING.ARMOR_RUINSHAT_PROC_CHANCE then
    ruinshat_proc(inst, owner)
    end
end
 
 
 
-------------------Ruin Hat Function End ---------------------------------
 
local function induceinsanity(val, owner)
    if owner.components.sanity ~= nil and owner.prefab ~= "doge" then
        owner.components.sanity:SetInducedInsanity(val)
    end
 
end
 
local function onequip(inst, owner) 
    owner.AnimState:OverrideSymbol("swap_object", -- Symbol to override.
    "swap_katana", -- Animation bank we will use to overwrite the symbol.
    "katana") -- Symbol to overwrite it with.
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
inst.procfn = function() tryproc(inst, owner) end
    owner:ListenForEvent("attacked", inst.procfn)
 
    induceinsanity(true, owner)
 
   --allow monster leadership
    if owner.prefab ~= "webber" then
        --Webber already has this one
        owner:AddTag("spiderwhisperer")
    end
    owner:AddTag("houndwhisperer")
end
 
local function onunequip(inst, owner) 
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal") 
induceinsanity(nil, owner)
 
--unallow monster leadership
    if owner.prefab ~= "webber" then
        --wiw
        owner:RemoveTag("spiderwhisperer")
    end
    owner:RemoveTag("houndwhisperer")
 
    local owner = inst.components.inventoryitem and inst.components.inventoryitem.owner
    if owner and owner.components.leader then
        --remove all monster followers
        if owner.prefab ~= "webber" then
            --(wiw)
            owner.components.leader:RemoveFollowersByTag("spider")
        end
        owner.components.leader:RemoveFollowersByTag("hound")
        --remove the companion tag for hounds
        for k,v in pairs(owner.components.leader.followers) do
            if k.components.combat and k.components.follower then
                k:RemoveTag("companion")
            end
        end
    end
 
end
 
-------------------- Purple Staff Function -----------------teleport-----------------
 
local function getrandomposition(caster)
    local ground = TheWorld
    local centers = {}
    for i, node in ipairs(ground.topology.nodes) do
        if ground.Map:IsPassableAtPoint(node.x, 0, node.y) then
            table.insert(centers, {x = node.x, z = node.y})
        end
    end
    if #centers > 0 then
        local pos = centers[math.random(#centers)]
        return Point(pos.x, 0, pos.z)
    else
        return caster:GetPosition()
    end
end
 
local function teleport_thread(inst, caster, teletarget, loctarget)
 
local cry = "BEGONE"
local duration = 2 
-- Where inst is the player instance
inst.components.talker:Say{Line(cry, duration)}
 
local hunger_cost = 10
--where inst is the player instance
inst.components.hunger:DoDelta(10)
 
local ground = TheWorld
 
    local t_loc = nil
    if loctarget then
        t_loc = loctarget:GetPosition()
    else
        t_loc = getrandomposition(caster)
    end
 
    local teleportee = teletarget
    local pt = teleportee:GetPosition()
    if teleportee.components.locomotor then
        teleportee.components.locomotor:StopMoving()
    end
 
 
 
    if ground.topology.level_type == "cave" then
        TheCamera:Shake("FULL", 0.3, 0.02, .5, 40)
        ground.components.quaker:MiniQuake(3, 5, 1.5, teleportee)     
        return
    end
 
    if teleportee.components.health then
        teleportee.components.health:SetInvincible(true)
    end
 
    --#v2c hacky way to prevent lightning from igniting us
    local preventburning = teleportee.components.burnable ~= nil and not teleportee.components.burnable.burning
    if preventburning then
        teleportee.components.burnable.burning = true
    end
    --ground:PushEvent("ms_sendlightningstrike", pt)
    if preventburning then
        teleportee.components.burnable.burning = false
    end
 
    teleportee:Hide()
 
---   if caster and caster.components.sanity then
---        caster.components.sanity:DoDelta(-TUNING.SANITY_HUGE)
---    end
 
    --ground:PushEvent("ms_forceprecipitation", true)
 
    local isplayer = teleportee:HasTag("player")
    if isplayer then
        teleportee.components.playercontroller:Enable(false)
        teleportee:ScreenFade(false, 2)
        Sleep(3)
    end
 
    if teleportee.Physics ~= nil then
        teleportee.Physics:Teleport(t_loc.x, 0, t_loc.z)
    else
        teleportee.Transform:SetPosition(t_loc.x, 0, t_loc.z)
    end
 
    if isplayer then
        teleportee:SnapCamera()
        teleportee:ScreenFade(true, 1)
        Sleep(1)
        teleportee.components.playercontroller:Enable(true)
    end
    if loctarget and loctarget.onteleto then
        loctarget.onteleto(loctarget)
    end
 
    --#v2c hacky way to prevent lightning from igniting us
    preventburning = teleportee.components.burnable ~= nil and not teleportee.components.burnable.burning
    if preventburning then
        teleportee.components.burnable.burning = true
    end
   -- ground:PushEvent("ms_sendlightningstrike", t_loc)
    if preventburning then
        teleportee.components.burnable.burning = false
    end
 
    teleportee:Show()
    if teleportee.components.health then
        teleportee.components.health:SetInvincible(false)
    end
 
    if isplayer then
     --   teleportee.sg:GoToState("wakeup")
        teleportee.SoundEmitter:PlaySound("dontstarve/common/staffteleport")
    end
end
 
local function teleport_targets_sort_fn(a, b)
    return a.distance < b.distance
end
 
local function teleport_func(inst, target)
    print(inst, target)
    local mindistance = 1
    local caster = inst.components.inventoryitem.owner
    local tar = target or caster
    if not caster then caster = tar end
    local pt = tar:GetPosition()
    local ents = TheSim:FindEntities(pt.x,pt.y,pt.z, 9000, {"telebase"})
 
    if #ents <= 0 then
        --There's no bases, active or inactive. Teleport randomly.
        inst.task = inst:StartThread(function() teleport_thread(inst, caster, tar) end)
        return
    end
 
    local targets = {}
    for k,v in pairs(ents) do
        local v_pt = v:GetPosition()
        if distsq(pt, v_pt) >= mindistance * mindistance then
            table.insert(targets, {base = v, distance = distsq(pt, v_pt)}) 
        end
    end
 
    table.sort(targets, teleport_targets_sort_fn)
    for i = 1, #targets do
        local teletarget = targets
        if teletarget.base and teletarget.base.canteleto(teletarget.base) then
            inst.task = inst:StartThread(function() teleport_thread(inst, caster, tar, teletarget.base) end)
            return
        end
    end
 
    inst.task = inst:StartThread(function() teleport_thread(inst, caster, tar) end)
end
 
-------------------Purple Staff Function End ---------------------------------
 
-- 1 doge 2 rule them all
 
local function onattack(inst, owner, target)
 
    --for targets that can be followers
    if target.components.follower then
        --all befriendable creatures
        if (target:HasTag("hound") and  owner:HasTag("houndwhisperer")) then
 
            --make all current followers and the target non aggressive
            for k,v in pairs(owner.components.leader.followers) do
                if k.components.combat and k.components.follower then
                    k.components.combat:SetTarget(nil)
                end
            end
            target.components.combat:SetTarget(nil)
 
            --make the target a follower (with a special case for hounds)
            owner.components.leader:AddFollower(target)
            if target:HasTag("hound") then
                target:AddTag("companion")
            end
 
            --add loyalty time - specific to the Authority Symbol
            target.components.follower:AddLoyaltyTime(200)
 
            --play the friend sound
            inst.SoundEmitter:PlaySound("dontstarve/common/makeFriend")
 
            --entities say something to confirm friendship
            if target:HasTag("hound") then
                owner.components.talker:Say("Be a good doge!")
            end
 
            --A few instants later, remake everyone non-agressive.
                --This can be useful if you have a follower that already initiated its attack animation,
                --and hitting the target after the moment you "persuade" it,
                --which would remake the target aggressive and lead to a fight among your friends.
                --This isn't a full-proof solution though, it's rather a very shy way of solving the problem.
            inst:DoTaskInTime( 0.2, function()
                for k,v in pairs(owner.components.leader.followers) do
                    if k.components.combat and k.components.follower then
                        k.components.combat:SetTarget(nil)
                    end
                end
                target.components.combat:SetTarget(nil)
            end )
        end
    end
end
 
--end doge ruler
 
 
 
local function init()
local inst = CreateEntity()
 
inst.entity:AddTransform()
inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()
 
    MakeInventoryPhysics(inst)
 
    inst.AnimState:SetBank("katana")
    inst.AnimState:SetBuild("katana")
    inst.AnimState:PlayAnimation("idle")
 
inst:AddTag("sharp")
 
 
 
    if not TheWorld.ismastersim then
        return inst
    end
 
    inst.entity:SetPristine()
 
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(TUNING.KATANA_DAMAGE)
 
    inst:AddComponent("blinkstaff")
 
---------------------------- Purple Staff Stuff -------------------------
inst.fxcolour = {104/255,40/255,121/255}
    inst:AddComponent("spellcaster")
    inst.components.spellcaster:SetSpellFn(teleport_func)
    inst.components.spellcaster.canuseontargets = true
    inst.components.spellcaster.canusefrominventory = true
    inst.components.spellcaster.canonlyuseonlocomotors = true
---------------------------- Purple Staff Stuff End ---------------------
 
inst:AddComponent("armor")
    inst.components.armor:InitCondition(1e+99, 0.01)
 
 
    inst:AddComponent("inspectable")
    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/katana.xml"
    inst.components.inventoryitem.imagename = "katana"
    
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)
 
 
    MakeHauntableLaunch(inst)
    
    return inst
end
 
return Prefab("common/inventory/katana", init, assets)

 

Link to comment
Share on other sites

@Blueberrys,

Yea it gives me a

[00:00:45]: 110622 - katana(LIMBO)

[00:00:45]: 110622 - katana(LIMBO) 110011 - doge 110011 - doge     when I cast the spell.
If I leave the code as is it shows me this error.

 

COROUTINE 110622 SCRIPT CRASH:

[string "../mods/loge/scripts/prefabs/katana.lua"]:130: attempt to index field 'talker' (a nil value)
LUA ERROR stack traceback:
        ../mods/dogte/scripts/prefabs/katana.lua(130,1) in function 'teleport_thread'
        ../mods/dogte/scripts/prefabs/katana.lua(240,1)
[00:00:45]: 
COROUTINE 110622 SCRIPT CRASH:
[string "../mods/dogte/scripts/prefabs/katana.lua"]:130: attempt to index field 'talker' (a nil value)
LUA ERROR stack traceback:
        ../mods/dogte/scripts/prefabs/katana.lua(130,1) in function 'teleport_thread'
        ../mods/dogte/scripts/prefabs/katana.lua(240,1)
[00:00:45]: SCRIPT ERROR! Showing error screen

 

Link to comment
Share on other sites

@lordfool Hmm. I'm assuming the inst variable isn't referring to the player. Try placing the code elsewhere, or figure out what inst is referring to. Alternately, you can use GetPlayer() instead of inst. It's not very clean, but it'll do for now if you can't find another way.

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