Making a sound work with an if statement

Posted by Paul Balfe on Mon 15 May 2023 12:38 AM — 10 posts, 23,689 views.

#0
Hi I'm learning how to script in lua, a soundpack for a mud I play. I have this trigger that's not working. Here's the trigger.


<triggers>
  <trigger
   enabled="y"
   group="misc"
   match="* leading into *"
   send_to="12"
   sequence="100"
  >
  <send>if "%1"=="unlock" then
playsound ("misc/doorunlock/"..math.random(1,3)..".ogg")
end</send>
  </trigger>
</triggers>


Here's the text I'm trying to make different sounds work when encountering: open close unlock lock opens unlocks locks closes.


You unlock a hunter green oak door leading into a rustic white log cabin on the north side of the street bearing the house number "1142".


Thank you.
Amended on Mon 15 May 2023 09:24 AM by Nick Gammon
USA Global Moderator #1
With that match pattern and that text, "%1" will not be "unlock". It will be "You unlock a hunter green oak door".

Try turning on the regexp option on your trigger and then changing the match pattern to "^You (open|close|unlock|lock) .*"
Amended on Mon 15 May 2023 06:07 AM by Fiendish
#2
I tried what you said and it's still not working.
I want to make the unlock sound play when mush sees the unlock word in the match trigger. As well as lock open close unlocks locks opens closes.
What about the text after the .*"? Do I put leading into * after it?
I'm new in mush client.
Thanks.
USA Global Moderator #3
Upon closer inspection I realize I missed several details, so here's a more complete answer.

Ok, you're new so first let me give you a copy/paste version to start from just to make sure...


<triggers>
  <trigger
   enabled="y"
   group="misc"
   match="^You (open|close|unlock|lock) .*"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
mapping = {
  ["open"] = "misc/dooropen/"..math.random(1,3)..".wav",
  ["close"] = "misc/doorclose/"..math.random(1,3)..".wav",
  ["unlock"] = "misc/doorunlock/"..math.random(1,3)..".wav",
  ["lock"] = "misc/doorlock/"..math.random(1,3)..".wav"
}

print("TRIGGER MATCHED. SOUND FILE: " .. mapping["%1"])</send>
  </trigger>
</triggers>


This will match on "You open/close/etc ..." messages (when YOU interact with a door) and print a message showing that the trigger matched and what sound file would play.

Once you see that working, you'll want to:

1. convert your .ogg files to 16-bit, 22.05 KHz, PCM (ie. uncompressed), Mono or Stereo WAV files, because MUSHclient does not support OGG format.

2. change the print to instead use either https://www.gammon.com.au/scripts/doc.php?function=Sound or https://www.gammon.com.au/scripts/doc.php?function=PlaySound

3. make a second trigger for a different pattern for when someone else interacts with the door, because the message would be different.
Amended on Tue 16 May 2023 01:08 AM by Fiendish
Australia Forum Administrator #4
Template:pasting
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.
#5
The sound is still not playing when I put in your code for the trigger we're working with the door.
I want the client to play the sound when it sees open close lock unlock in this text:
You unlock a hunter green oak door leading into a rustic white log cabin on the north side of the street bearing the house number "1142".
And btw I just made a mp3 file of a sound and put it in mush and it played just fine as well as ogg.
Thanks.
Australia Forum Administrator #6
Please show exactly how you have amended Fiendish's trigger example.

Template:copying
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.
#7
Hi
Here's the trigger.
<triggers>
<trigger
enabled="y"
group="misc"
match="&quot;^You (open|close|unlock|lock) .*&quot;"
regexp="y"
send_to="12"
sequence="101"
>
<send>
mapping = {
["open"] = playsound "misc/dooropen/"..math.random(1,6)..".ogg",
["close"] = playsound "misc/doorclose/"..math.random(1,10)..".ogg",
["unlock"] = playsound "misc/doorunlock/"..math.random(1,3)..".ogg",
["lock"] = playsound "misc/doorlock/"..math.random(1,6)..".ogg"
}

print("TRIGGER MATCHED. SOUND FILE: " .. mapping["%1"])</send>
</trigger>
</triggers>
Thanks.
USA Global Moderator #8
MUSHclient does not support OGG or MP3 files. You have to convert them to WAV like I described.

PlaySound is also case-sensitive. You cannot freely alter the capitalization of functions in Lua.

PlaySound also requires more than just one input argument. If you want to use only one input argument, you need to use the Sound function instead.

Also I said to change the print, not the mapping. The proper modification (only after you see the print statements appear from the version I originally posted) would be:


<triggers>
  <trigger
   enabled="y"
   group="misc"
   match="^You (open|close|unlock|lock) .*"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
mapping = {
  ["open"] = "misc/dooropen/"..math.random(1,3)..".wav",
  ["close"] = "misc/doorclose/"..math.random(1,3)..".wav",
  ["unlock"] = "misc/doorunlock/"..math.random(1,3)..".wav",
  ["lock"] = "misc/doorlock/"..math.random(1,3)..".wav"
}

Sound(mapping["%1"])
</send>
  </trigger>
</triggers>
Amended on Tue 16 May 2023 09:53 PM by Fiendish
#9
Fiendish said:


mapping = {
  ["open"] = "misc/dooropen/"..math.random(1,3)..".wav",
  ["close"] = "misc/doorclose/"..math.random(1,3)..".wav",
  ["unlock"] = "misc/doorunlock/"..math.random(1,3)..".wav",
  ["lock"] = "misc/doorlock/"..math.random(1,3)..".wav"
}



I am curious if the files are indeed named "1.ogg", "2.ogg", and "3.ogg", too. Obviously they still need conversion to .wav, as stated. Another thing to note is if the pathing is actually accurate. Is the 'misc' folder indeed in the 'sounds' directory?

Of course, if more sounds are added, the math.random would need to be updated, but in this situation, I'd imagine a function that selects a random filename from 'utils.readdir' would be more beneficial. Just some food for thought.