Page 1 of 1

Creating message mutator

Posted: Fri Apr 05, 2019 7:10 pm
by piazon79
Hello,

I am trying to create a simple mutator that takes a wav file and plays the sound byte when a user types a certain word in the chat. For example when a player types "hello" in chat it plays a sound byte.

I'm importing my wav file like this:
#exec AUDIO IMPORT FILE="Sounds\hi_0.wav" NAME="hi_0" GROUP="Taunts"

But i can't figure out how to actually get it to play when the user types "hi" or "hello". I'm new to unreal script and I have searched all over the internet for assistance but nothing that I can find that relates to this. I know this is possible, I have the MOZMessages mutator but I'm trying to create my own sounds.

Any help is greatly appreciated. Thanks so much!

Re: Creating message mutator

Posted: Sat Apr 06, 2019 5:21 am
by JackGriffin
Post over at House of Fool's forum: https://hofgamingclan.com/forums/
The head guy there is a voice pack fanatic and they use 'zounds' type sound mods on their servers. I wouldn't be surprised if they weren't written by him.

Re: Creating message mutator

Posted: Sat Apr 06, 2019 2:05 pm
by PrinceOfFunky
piazon79 wrote:Hello,

I am trying to create a simple mutator that takes a wav file and plays the sound byte when a user types a certain word in the chat. For example when a player types "hello" in chat it plays a sound byte.

I'm importing my wav file like this:
#exec AUDIO IMPORT FILE="Sounds\hi_0.wav" NAME="hi_0" GROUP="Taunts"

But i can't figure out how to actually get it to play when the user types "hi" or "hello". I'm new to unreal script and I have searched all over the internet for assistance but nothing that I can find that relates to this. I know this is possible, I have the MOZMessages mutator but I'm trying to create my own sounds.

Any help is greatly appreciated. Thanks so much!
You just have to create a mutator class that listens to messages by registering it as a MessageMutator, GameInfo.uc contains this function "RegisterMessageMutator(Mutator M)", call it from the mutator in PostBeginPlay() using "self" as parameter. Then, everytime a player chats, one or more of these callbacks will be called: "MutatorTeamMessage(...)"/"MutatorBroadcastMessage(...)"/"MutatorBroadcastLocalizedMessage(...)", which are functions in Mutator.uc that you just need to redefine (also remember to add super.NameOfTheFunction(...).
Since you have the hi_0 file, do a check on the string and if the string contains "hello" (you can check it by using InStr) use a "for" on Level.PawnList, check if the pawn is a PlayerPawn and then play the sound as playerPawn.ClientPlaySound(...) (which is a function in PlayerPawn.uc).
Maybe there's a way to broadcast a sound but I'm not sure how.

Re: Creating message mutator

Posted: Sat Apr 06, 2019 3:35 pm
by piazon79
Thanks for the suggestions! I'll give this a try, may take me a while to figure it out since im newer to unreal script but i really appreciate the help!

Re: Creating message mutator

Posted: Sat Apr 06, 2019 4:31 pm
by PrinceOfFunky
piazon79 wrote:Thanks for the suggestions! I'll give this a try, may take me a while to figure it out since im newer to unreal script but i really appreciate the help!
Something like this:

Code: Select all

class CustomMutator extends Mutator;

event PostBeginPlay() {
	Level.Game.RegisterMessageMutator(self);
}

function bool MutatorBroadcastMessage(Actor Sender, Pawn Receiver, out coerce string Msg, optional bool bBeep, out optional name Type) {
	//Should check if (Sender.isA('PlayerPawn')) ?
	if (inStr(Msg, "hello") != -1) {
		for (pawn = level.pawnList; (pawn != None) && (pawn.isA('PlayerPawn')); pawn = pawn.nextPawn) {
			pawn.ClientPlaySound(SOUND_GOES_HERE);
		}
	}
}
I actually don't remember if a sound must be used like textures do (Texture'Package.Group.Name') or in a different way.

Re: Creating message mutator

Posted: Sat Apr 06, 2019 9:16 pm
by Barbie
PrinceOfFunky wrote:

Code: Select all

for (pawn = level.pawnList; (pawn != None) && (pawn.isA('PlayerPawn')); pawn = pawn.nextPawn) {
	pawn.ClientPlaySound(SOUND_GOES_HERE);
	}
Because second argument of the for-loop is the loop condition and not every pawn in level.pawnList is a PlayerPawn, that loop will not iterate over all items in the list necessarily.

Better use something like this:

Code: Select all

for (pawn = level.pawnList; pawn != None; pawn = pawn.nextPawn)
	if (PlayerPawn(pawn) != None)
		PlayerPawn(pawn).ClientPlaySound(SOUND_GOES_HERE);

Re: Creating message mutator

Posted: Sat Apr 06, 2019 9:41 pm
by piazon79
Awesome! I'll try this out. Questions though, where does it check for what the playing is typing in order to play the sound? Like typing "hello" in chat will play that sound?

Re: Creating message mutator

Posted: Sat Apr 06, 2019 11:42 pm
by Barbie
piazon79 wrote:where does it check for what the playing is typing in order to play the sound?

Code: Select all

if (inStr(Msg, "hello") != -1)
piazon79 wrote:Like typing "hello" in chat will play that sound?
Close - typing it is not enough, the player has to send the text (press Enter) also. Also notice that "inStr" compares case sensitive .

Re: Creating message mutator

Posted: Sun Apr 07, 2019 12:22 am
by PrinceOfFunky
Barbie wrote:
PrinceOfFunky wrote:

Code: Select all

for (pawn = level.pawnList; (pawn != None) && (pawn.isA('PlayerPawn')); pawn = pawn.nextPawn) {
	pawn.ClientPlaySound(SOUND_GOES_HERE);
	}
Because second argument of the for-loop is the loop condition and not every pawn in level.pawnList is a PlayerPawn, that loop will not iterate over all items in the list necessarily.

Better use something like this:

Code: Select all

for (pawn = level.pawnList; pawn != None; pawn = pawn.nextPawn)
	if (PlayerPawn(pawn) != None)
		PlayerPawn(pawn).ClientPlaySound(SOUND_GOES_HERE);
You're right, it's been a long time that I don't code in unrealscript anymore :what: .