Jump to content

[Scripting] play sound if tag


Recommended Posts

I already have a custom sound bank added to a character, but is there a way to play a specific sound from it? A sound that isn't used elsewhere?

 

Also, is there a way to check if a inst tag is enabled or not?

 

My goal is something like this:

 

if inst:HasTag("tag") then

    inst:PlaySound("charactername/characters/charactername/newsound")

end

Link to comment
Share on other sites

@Woodside235, you need to make sure there is a SoundEmitter on the character or item and to play the sound it would be:

if inst:HasTag("TAG") then	inst.SoundEmitter:PlaySound("charactername/characters/charactername/newsound")end 

 

 

I'm not sure if that is the address of the actual sound file, but it's what you gave so that is why I used it.

Link to comment
Share on other sites

@Woodside235, you need to make sure there is a SoundEmitter on the character or item and to play the sound it would be:

if inst:HasTag("TAG") then	inst.SoundEmitter:PlaySound("charactername/characters/charactername/newsound")end 

 

 

I'm not sure if that is the address of the actual sound file, but it's what you gave so that is why I used it.

 

Let's say the sound "newsound" is with the usual character sounds like hurt and talk_LP, would all I have to do is use that address?

 

Also, how do I initialize a sound emiiter?

Link to comment
Share on other sites

Let's say the sound "newsound" is with the usual character sounds like hurt and talk_LP, would all I have to do is use that address?

 

Also, how do I initialize a sound emiiter?

 

Considering the character can already play sounds, it is.

 

You need to tell the game when to do that sound though, you could use ListenForEvent() or onsomething().

Link to comment
Share on other sites

Considering the character can already play sounds, it is.

 

You need to tell the game when to do that sound though, you could use ListenForEvent() or onsomething().

 

It isn't working

local master_postinit = function(inst)		-- Stats	inst.components.health:SetMaxHealth(150)	inst.components.hunger:SetMax(200)	inst.components.sanity:SetMax(75)		-- Chance of dodge hit	local oldonhitfn = inst.components.combat.onhitfn		inst.components.combat.onhitfn = function(inst, attacker, damage)		inst:AddTag("alwaysblock")		inst.SoundEmitter:PlaySound("woodside/characters/woodside/dodge", "dodge")				--[[		if math.random() > (-0.333333) then			inst:AddTag("block")		else			inst:RemoveTag("block")		end		]]--				if oldonhitfn then			oldonhitfn(inst, attacker, damage)		end	end	end

The sound playing line is this:

inst.SoundEmitter:PlaySound("woodside/characters/woodside/dodge", "dodge")

 

Basically, for now, it should be playing this sound every time I'm hit, but it's not.

Link to comment
Share on other sites

It doesn't even play default sounds. I have no idea what I'm doing wrong.

 

Huh, it appears that adding the alwaysblock tag prevents the onhitfn from being called, thus I never notice the sound while testing. Any ideas on how to have a random block/dodge attack?

Link to comment
Share on other sites

@Woodside235, alter the Attack action.

 

ACTIONS.ATTACK.fn = function(act)if act.target.prefab == "prefab of character" then   if CHECK DODGE RATE HERE then      DO THINGS WHICH ARE ASSOCIATED WITH AN ACTUAL DODGE      return false   endend    act.doer.components.combat:DoAttack(act.target)    return trueend 

 

 

This should get you on the right track.

Link to comment
Share on other sites

Do you mean that I should alter the attack action of every single monster? That seems a bit much to just implement a random dodge trait for a character.

 

That is part of programming, where the most simplest things can be very complicated. Also, all monsters call the attack action, therefor altering that function will alter all monsters attacks. I.E. you only need to alter a single function to alter all monsters.

Link to comment
Share on other sites

That is part of programming, where the most simplest things can be very complicated. Also, all monsters call the attack action, therefor altering that function will alter all monsters attacks. I.E. you only need to alter a single function to alter all monsters.

 

I'm guessing this new function goes at the bottom of modmain.lua, correct?

Using act, how do I play a sound from the player?

 

act.target.inst.SoundEmitter:PlaySound()

?

Link to comment
Share on other sites

@Kzisor
 

I wanna say thanks for all of your help so far. I've never made a mod before and it's annoying trying to work with someone else's code.

 

Anyway, I'm not dodging any hits at all with this.

 

 

modmain.lua

ACTIONS.ATTACK.fn = function(act)	if act.target.prefab == "woodside" then	   if 1 > 0 then -- 1 > 0, always true for now.			act.target.components.talker:Say("asdf") -- just to see if this if statement gets reached at all			act.target.SoundEmitter:PlaySound("woodside/characters/woodside/dodge")			return false	   end	end		if ACTIONS_ATTACK_OLD_fn then		return ACTIONS_ATTACK_OLD_fn(act)	else		act.doer.components.combat:DoAttack(act.target)		return true	endend

 

I think the issue is here:

if act.target.prefab == "woodside" then

 

Should I just throw in the name of the character there? Or do I have to have some sort of path like "scripts/prefabs/woodside"? Or am I implementing this completely wrong?

 

P.S. Your nightvision code works perfectly, thanks for that!

 

EDIT: @Kzisor I need to learn how to forum.

Link to comment
Share on other sites

@Woodside235, if you are trying to debug code, always use the print function.

 

print("ENTER TEXT HERE TO HELP YOU DEBUG.")

 

This both shows up in the log file and in the console; which will help save you time.

 

As far as the issue with "if act.target.prefab == "woodside" then" that is how you get the prefab. I think you might need to check the prefab that you are trying to get with something like: print("Prefab: "..act.target.prefab)

 

After you get hit a time or two, open the console so that you can see what prefab is being printed. If it's different than what is showing up or if it's not showing up at all that is the issue.

 

Also forgot to ask, what nightvision code?

Link to comment
Share on other sites

@Kzisor

 

ACTIONS.ATTACK.fn = function(act)	print("Prefab: "..act.target.prefab)	if act.target.prefab == "woodside" then	   if 1 > 0 then			print("hello world")			act.target.SoundEmitter:PlaySound("woodside/characters/woodside/dodge")			return false	   end	end

 

Alright so this is interesting: I'm only seeing print outs that state the target only when I attack things. There's no printout for when I GET attacked. This function is never even called if I get attacked.

 

Also, the night vision code from here: http://forums.kleientertainment.com/topic/48666-scripting-client-side-light/

 

Link to comment
Share on other sites

@Woodside235, I'm assuming since you are using DST code that you are coding this for DST. (Please note you are in the DS mod area, next time you should post in the proper section.)

 

Try something like this:

 

local function CombatPostInit(self)   self._DoAttack = self.DoAttackfunction self:DoAttack(target_override, weapon, projectile)        local targ = target_override or self.target    if targ.prefab == "woodside" then        -- Implement the dodge code here.        if cannot dodge then            self:_DoAttack(target_override, weapon, projectile)        else            return        end    else        self:_DoAttack(target_override, weapon, projectile)    end    endendAddComponentPostInit("combat", CombatPostInit) 

 

I thought creatures used the attack action, but seems they do not. I know for a fact that all attacks use the combat:DoAttack so this should work.

 

Also on a related note, the nightvision code that I posted was experimental and only functioned in half capacity. It was a working diagram to show that it wouldn't actually work like is wanted. Whenever the server host obtains nightvision all players including non-nightvision characters become immune to charlie.

 

Link to comment
Share on other sites

@Kzisor

 

I was going to start making threads in the DST forum, but since this was already here I was using it.

 

It works! Finally! Thanks for your help, again.

 

Also, the night vision may glitchy, but for the purpose of playing with friends where the host won't be using this character, it works.

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