• Print

Author Topic: Say (or Chat) Sound Script doesn't work correctly  (Read 6694 times)

0 Members and 1 Guest are viewing this topic.

Offline pote_

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
Say (or Chat) Sound Script doesn't work correctly
« on: May 04, 2016, 05:11:13 am »
Hi. I want to get  chat sounds in my own gmod server.Then i found this topic and code here

Code: [Select]
USay = {}

USay.WordList = {
["hi"] = "sound/hi.wav";
}

function USay.ChatFunction( ply, text )
if ply:IsValid() then
for k,v in pairs(USay.WordList) do
if string.find( text, k ) then
game.ConsoleCommand("ulx playsound " ..USay.WordList[k].. "\n")
return text
end
end
end
end
hook.Add("PlayerSay", "USay.ChatFunction_Hook", USay.ChatFunction)
/index.php/topic,3762.0.html />
I installed correctly but some sounds won't be played correctly. For example when I type "apple", somehow "a.mp3" be played even if "apple.mp3" exists.
I wrote "usay.lua" correctly. I guess the script doing this. But I have no idea to solve this...
Someone help me please.
Thank you and sorry for my broken english

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Say (or Chat) Sound Script doesn't work correctly
« Reply #1 on: May 04, 2016, 05:24:07 am »
The code you have is using string.find( haystack, needle ) with text as the haystack and k as the needle. Basically it's going through each of your sounds and checking if the chat message contains the name of the sound. Because the string "a" is found in "apple", if the for loop happens to get to a.mp3 before apple.mp3 then it will play a.mp3.

Another example:
if you have three sounds, bubble.mp3, gum.mp3, and bubblegum.mp3, and someone types "bubblegum" in chat, depending on the order the sounds are reached by the for loop any of the three sounds could be played.

If you just want to play a sound when ONLY that word is played (e.g. if you want to play apple.mp3 you type only "apple") then change the line
Code: Lua
  1. if string.find( text, k ) then
to
Code: Lua
  1. if text == k then

If you instead want to play all the sounds found in a message, there isn't any easy way I can think of to combat this problem. Your best bet is to not have any single-letter (or really just short named) sounds, because those will often be found within other messages.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline pote_

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
Re: Say (or Chat) Sound Script doesn't work correctly
« Reply #2 on: May 04, 2016, 05:46:07 am »
Thank you. It works fine! :)
I really appreciate!

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Say (or Chat) Sound Script doesn't work correctly
« Reply #3 on: May 04, 2016, 08:55:20 am »
Glad I could help!
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Re: Say (or Chat) Sound Script doesn't work correctly
« Reply #4 on: May 04, 2016, 05:00:46 pm »
Expanding on this issue only slightly, there are times where you would want to use a regex operations like string.match string.find and you want to match with a table in a way that would search by priority.

How would people do that? (I may look into it my self actually.)
I cry every time I see that I am not a respected member of this community.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Say (or Chat) Sound Script doesn't work correctly
« Reply #5 on: May 05, 2016, 11:39:31 am »
Expanding on this issue only slightly, there are times where you would want to use a regex operations like string.match string.find and you want to match with a table in a way that would search by priority.

How would people do that? (I may look into it my self actually.)

First, a clarification: Lua does not use regex but instead it's own pattern matching system. It may seem nit-picky, but its important to distinguish between them as they have relatively vast differences.

"search by priority" is a pretty loose request. What are you prioritizing? In pote_'s specific case, I think the most logical method of prioritization without getting too complicated would be to sort by length. Although you may be able to use default Lua functions such as string.gmatch to accomplish this, I personally would just create my own function something like this:

Code: Lua
  1. local function lengthSearch( text, tbl )
  2.   local maxLength = 0
  3.   local selected
  4.  
  5.   for name, v in pairs( tbl ) do
  6.     if string.find( text, name ) and string.len( text ) > maxLength then
  7.       selected = name
  8.     end
  9.   end
  10.  
  11.   return selected or false
  12. end

The one glaring issue I see with this method is that if you have two elements in tbl of the same length that are found in text then the selected one is up to the mercy of the for loop. An extension could be to check if the length is greater or equal, store any equal length elements in a table, and choose the element by another method.

If you're looking for words then you might want to throw away any elements that are found in the middle of the text, e.g. choosing "what" over "hat" when searching in "whatever". There are so many possibilities that I don't think there is one (or even a few) general algorithm(s) to solve it. Your choice of sorting really depends on the application.

My answer is more of an algorithmic solution rather than a pattern solution. If you give me more details as to what you want to prioritize, I might be able to help you with creating a Lua pattern for it. Of course, using an algorithm might be better (again, heavily relies on your specific problem).

Hope I was some help, and sorry for rambling on.
« Last Edit: May 05, 2016, 11:44:19 am by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Re: Say (or Chat) Sound Script doesn't work correctly
« Reply #6 on: May 06, 2016, 01:04:19 am »
Basically long ago I had this idea for a sort of gimp like command that instead of replacing your chat completely it would replace certain words. I ran into problems that happened like so...

Let's say I would have "we" and "week" and the string was "we work weekly". Let's say I am replacing "we" with "pudding" and "week" with "spoon".
Some times it would work correctly "pudding work spoon" but other times it would do "pudding work pudddingekly".

I don't have the exact code I used to make it but I've ran into the problem and feel that others may run into it as well. I'll post my code if I find it.
I cry every time I see that I am not a respected member of this community.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Say (or Chat) Sound Script doesn't work correctly
« Reply #7 on: May 06, 2016, 11:18:24 am »
In your case it sounds like you only want to replace whole words, so you could just check for the word surrounded by spaces ( needle = " " .. myword .. " " ).
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Say (or Chat) Sound Script doesn't work correctly
« Reply #8 on: May 06, 2016, 06:12:23 pm »
Could also check length of replacee candidate versus word length of replacer candidate table.
If weekly > 2, you know it's not we.
Slow overall, but presume you'd use high level checks first (we found in week), then if found, start getting down into the weed checks.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Re: Say (or Chat) Sound Script doesn't work correctly
« Reply #9 on: May 07, 2016, 12:47:37 am »
In your case it sounds like you only want to replace whole words, so you could just check for the word surrounded by spaces ( needle = " " .. myword .. " " ).

Could also check length of replacee candidate versus word length of replacer candidate table.
If weekly > 2, you know it's not we.
Slow overall, but presume you'd use high level checks first (we found in week), then if found, start getting down into the weed checks.

Thanks I will try that on the lua demo site. Weird how the results seems to be inconsistent though, never had that happen.

Also we are talking about Weeks not Weeds, silly JamminR.
I cry every time I see that I am not a respected member of this community.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Say (or Chat) Sound Script doesn't work correctly
« Reply #10 on: May 07, 2016, 07:13:03 am »
Also we are talking about Weeks not Weeds, silly JamminR.
Yes, I was being silly.
I was referring to one definition of "into the weeds", where by the high level checks could be faster, then if found, start doing more/slower checks such as my suggestion.
https://en.wiktionary.org/wiki/in_the_weeds (definition 1)
It just so happened 'we' could be found in the weeds. :)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Re: Say (or Chat) Sound Script doesn't work correctly
« Reply #11 on: May 07, 2016, 07:00:13 pm »
Yes, I was being silly.
I was referring to one definition of "into the weeds", where by the high level checks could be faster, then if found, start doing more/slower checks such as my suggestion.
https://en.wiktionary.org/wiki/in_the_weeds (definition 1)
It just so happened 'we' could be found in the weeds. :)

Ah sorry I don't really know many idioms.
I cry every time I see that I am not a respected member of this community.

  • Print