Jump to content

Adding custom sound effects to weapons


Recommended Posts

You can add a function to the spear's weapon component, but it only triggers when hitting something, not when slashing in the air.

 

local function OnAttack(inst)

    inst.SoundEmitter:PlaySound("your/sound/path")

end

 

inst.components.weapon.onattack = OnAttack

 

from SGwilson.lua (player stategraph):

            if weapon then
                inst.AnimState:PlayAnimation("atk")
                if weapon:HasTag("icestaff") then
                    inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_icestaff")
                elseif weapon:HasTag("shadow") then
                    inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_nightsword")
                elseif weapon:HasTag("firestaff") then
                    inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_firestaff")
                else
                    inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_weapon")
                end
            elseif otherequipped and (otherequipped:HasTag("light") or otherequipped:HasTag("nopunch")) then
                inst.AnimState:PlayAnimation("atk")
                inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_weapon")
            else
                inst.sg.statemem.slow = true
                inst.AnimState:PlayAnimation("punch")
                inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_whoosh")
            end

 

That's hard-coded, changing it would be complicated and cause problems with similiar mods. But the good news is: That code triggers from an event "doattack", which you can listen for without problem.

 

local function DoSound(owner)

--stuff

end

 

local function onequip(inst, owner) --you should have that function already

    --[...]

    inst:ListenForEvent("doattack",DoSound,owner) --listen for that event, then do that function, but listen for this guy and not for me

end

 

local function onunequip(inst, owner) --you should have that function already

    --[...]

    inst:RemoveEventCallback("doattack",DoSound,owner) --undo the listener

end

Link to comment
Share on other sites

@theceruleanflash Adding to what Mobbstar said, you can try changing the stategraph too if you want.

local function SGWilsonPostInit(sg)	local old_AttackOnEnter = st.states.attack.onenter	st.states.attack.onenter = function(inst)			local weapon = inst.components.combat:GetWeapon()		if (weapon			and weapon.prefab == "your_prefab") then -- You can also use tags			inst.SoundEmitter:PlaySound("sound_path")		end				return old_AttackOnEnter(inst)	endendAddStategraphPostInit("wilson", SGWilsonPostInit)
Link to comment
Share on other sites

local function OnAttack(inst)

    inst.SoundEmitter:PlaySound("your/sound/path")

end

 

inst.components.weapon.onattack = OnAttack

 

Is this separate from the code you listed below? If so, I would like to use this; I don't mind just having it make a sound when it hits. One more question: should this type of sound file be .fsb, and what directory should you suggest I place it in?

 

Thanks for the reply

Link to comment
Share on other sites

Is this separate from the code you listed below? If so, I would like to use this; I don't mind just having it make a sound when it hits. One more question: should this type of sound file be .fsb, and what directory should you suggest I place it in?

 

Thanks for the reply

 

Yes, those for lines of code are the whole first method. You should place it (or at least the last line of it) under the line that adds the weapon component.

 

The sound path can be seen in Fmod (the programm you presumably used to make the sound file). If you are using game sounds, check the related prefabs/widgets/etc. or use the fmod event player to open the built sound file.

 

By the way, you load sound files with the "assets" table. Add those two lines:

    Asset("SOUNDPACKAGE", "sound/mysound.fev"),

    Asset("SOUND", "sound/mysound_bank00.fsb")

Link to comment
Share on other sites

Hey, thanks for the help so far, but I've been having some trouble. When I used

local function OnAttack(inst)    inst.SoundEmitter:PlaySound("your/sound/path")end inst.components.weapon.onattack = OnAttack

it had crashed the game for me. In my log.txt, it says

variable 'inst' is not declared

 

This is probably a really easy fix and I'm just bad at this, but any help is appreciated

 

Link to comment
Share on other sites

Nevermind, I had fixed the crash, but I can't seem to get the sound to work. Should the assets be in the modmain or the item lua file? (I was thinking this because for custom character sounds it's in the modmain)

 

I don't think the asset loading matters, because the game loads all assets anyways (unless you use simplex' memspike fix).

 

Post your code. Did you build the sound properly?

Link to comment
Share on other sites

local assets={	Asset("ANIM", "anim/redlightsaber.zip"),	Asset("ANIM", "anim/swap_redlightsaber.zip"),	Asset("ATLAS", "images/inventoryimages/redlightsaber.xml"),		Asset("SOUNDPACKAGE", "sound/lightsaber.fev"),    Asset("SOUND", "sound/lightsaber.fsb"),	}local function onfinished(inst)    inst:Remove()endlocal function onequip(inst, owner)    owner.AnimState:OverrideSymbol("swap_object", "swap_redlightsaber", "swap_nightmaresword")    owner.AnimState:Show("ARM_carry")    owner.AnimState:Hide("ARM_normal")endlocal function onunequip(inst, owner)    owner.AnimState:Hide("ARM_carry")    owner.AnimState:Show("ARM_normal")end  local function fn(Sim)	local inst = CreateEntity()	local trans = inst.entity:AddTransform()	local anim = inst.entity:AddAnimState()	local sound = inst.entity:AddSoundEmitter()    MakeInventoryPhysics(inst)        inst.AnimState:SetBank("nightmaresword")    inst.AnimState:SetBuild("redlightsaber")    inst.AnimState:PlayAnimation("idle")        inst:AddTag("sharp")		inst:AddComponent("finiteuses")    inst.components.finiteuses:SetMaxUses(TUNING.NIGHTSWORD_USES*1.5)    inst.components.finiteuses:SetUses(TUNING.NIGHTSWORD_USES*1.5)    	inst:AddComponent("weapon")	inst.components.weapon:SetDamage(TUNING.SPEAR_DAMAGE * 2)            inst:AddComponent("inspectable")        inst:AddComponent("inventoryitem")	inst.components.inventoryitem.atlasname = "images/inventoryimages/redlightsaber.xml"        inst:AddComponent("equippable")    inst.components.equippable:SetOnEquip( onequip )    inst.components.equippable:SetOnUnequip( onunequip )        return instendlocal function OnAttack(inst)	inst.components.weapon.onattack = OnAttack    inst.SoundEmitter:PlaySound("lightsaber/lightsaber/attack") endreturn Prefab( "common/inventory/redlightsaber", fn, assets)

Thhis is my prefab for my weapon

Link to comment
Share on other sites

Thhis is my prefab for my weapomn

 

 

You placed a line wrong:

 

  
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( onequip )
    inst.components.equippable:SetOnUnequip( onunequip )
   

    inst.components.weapon.onattack = OnAttack
 
    return inst
end
 
local function OnAttack(inst)
    inst.SoundEmitter:PlaySound("lightsaber/lightsaber/attack")
 
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...