• Print

Author Topic: Chat Triggers  (Read 10731 times)

0 Members and 1 Guest are viewing this topic.

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Chat Triggers
« on: July 21, 2013, 01:40:14 am »
So since I'm pretty new to Lua (I know like... the really, really basic stuff), I would like to know if it's possible to be able to make a phrase said in chat trigger something like tsay.

For example, if player 1 says "Hurr durr". I would like that phrase to trigger ULib.tsay(?) to say: "I LIKE TO HERP DERP".
If this is possible, I would be happy to get some documentation of some sort to guide me to set up this code myself (I have searched the forums a bit, but I'm not sure if I used the right keywords, and if I did, I didn't find anything).

Thanks in advance.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Chat Triggers
« Reply #1 on: July 21, 2013, 02:00:49 am »
You'll want to hook the PlayerSay base hook. Be very careful though, doing this wrong could potentially break everything else that uses it, including all of ULX's chat commands.

http://gmodwiki.net/Lua/Hooks/Base/PlayerSay

a quick example of how you'd do your example would be:

Code: [Select]
function HurDerp( ply, text, _ )

if ( string.sub( string.lower(text), 1, 9 ) == "hurr durr" ) then
ULib.tsay( ply, "I LIKE TO HERP DERP")
end

end
hook.Add( "PlayerSay", "HurDerp_Hook", HurDerp )
« Last Edit: July 21, 2013, 05:40:09 pm by MrPresident »

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Chat Triggers
« Reply #2 on: July 21, 2013, 04:25:57 am »
Thanks, MrPresident.

Now, I have a few other questions. I found your ULib.tsayColor, how would I implement that into this? Just replace ULib.tsay with ULib.tsayColor, then do
Code: Lua
  1. (ply, Color(200,100,50,Opacity?), "I", Color(150, 255, 0, Opacity??), "LIKE", Color(etc), "TO",

And so on?

Also, where would I have to place this, would I need to add it into one of the ULib/ULX directories?

Sorry for a lot of questions, I'm just willing to learn. Thanks for the help I've received so far, hopefully you will help me further!
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Chat Triggers
« Reply #3 on: July 21, 2013, 06:57:49 am »
Now, I have a few other questions. I found your ULib.tsayColor, how would I implement that into this? Just replace ULib.tsay with ULib.tsayColor, then do
Code: Lua
  1. (ply, Color(200,100,50,Opacity?), "I", Color(150, 255, 0, Opacity??), "LIKE", Color(etc), "TO",

And so on?
Unsure how you found Ulib.tsayColor, but, all of the ULib functions are documented here - http://ulyssesmod.net/docs/files/ULib_readme-txt.html
You would indeed have to place the color before each text, but, I think we made it simpler than having to use the "Color()" command.
Experiment. Try a table. ply, {r#g#,b#}, "text1", {r#g#,b#}, "text2"
If that doesn't work, try your way. Pay attention to the errors it may give.

Also, where would I have to place this, would I need to add it into one of the ULib/ULX directories?
ULX Addon 101
Make your own folder in gmod/addons/<my_addon_foldername>
Make an addon.txt within that folder (look at another addon to see what's in it...then name your info in it.)
Within <myaddonfoldername>, make a lua/ulib/modules/<my_file_name>.lua (replace ulib with ulx if you plan on ever using ulx functions, for now, you're only using ulib.tsayColor)
Within that lua file, place your code.
Now, whenever you load your server and it has ulib on it, it will load that file as a module.
This way, it won't be as easily affected by any Ulib(or ulx) file/folder structure changes we ever make.

Sorry for a lot of questions, I'm just willing to learn.
No need to apologize for wanting to learn from well-thought out and researched questions
« Last Edit: July 21, 2013, 06:59:31 am by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Chat Triggers
« Reply #4 on: July 21, 2013, 08:19:25 am »
Thanks for the reply, Jammin. I forgot to mention that I was actually browsing your documentation a bit, and that's how I found tsayColor. Although, I wasn't entirely sure how it all worked, that's why I was asking.

I'll try the things you both mentioned to me. Thanks a lot for the help, I really appreciate it.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Chat Triggers
« Reply #5 on: July 21, 2013, 09:10:45 am »
So I copied what MrPresident originally said, and modified it a bit to my liking (Note that I did used normal "tsay" on purpose).
I added that to the folder setup you told me to, and my local server loaded it fine. Although, whenever I said "Hurr durr", it didn't trigger anything. Not even a lua error in console.
Yes, I did make an addon.txt (I actually copied the one from ULX and just changed it) and yes, I put the lua file in here: addons/Hurrdurr/ulib/modules/lua/Hurrdurr.lua.

What did I do wrong? My code is below, just in case, although I don't think it's that.

Code: Lua
  1. function HurrDurr ( ply, text, _ )
  2.         if ( string.sub ( string.lower(text), 1, 9 ) == "Hurr durr" ) then
  3.                 ULib.tsay( ply, "I LIKE TO HERP DERP" )
  4.         end
  5. end
  6.  
  7. hook.Add ("PlayerSay", "HurrDurr", HurrDurr)
  8.  
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Chat Triggers
« Reply #6 on: July 21, 2013, 11:16:27 am »
addons/Hurrdurr/lua/ulib/modules/Hurrdurr.lua should be your path. Not addons/Hurrdurr/ulib/modules/lua/Hurrdurr.lua
:)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Chat Triggers
« Reply #7 on: July 21, 2013, 11:57:23 am »
In fact, that was a mistype on my part, sorry. The directory is in fact: addons\Hurrdurr\lua\ulib\modules\Hurrdurr.lua

Also, it is hosted on a SRCDS server. Just letting you know.
« Last Edit: July 21, 2013, 11:59:52 am by Decicus »
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Chat Triggers
« Reply #8 on: July 21, 2013, 12:16:46 pm »
Change that capital H in the code after ==, the "Hurr durr" to a lowercase.
The code example Mr P provided turns everything in chat string to lower case. (as shown in your/his code hurr durr will never equal "Hurr durr".)
Additionally, the '9' given in his example makes it so that 'hurr durr' must be the first thing said.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Chat Triggers
« Reply #9 on: July 21, 2013, 01:29:29 pm »
Is the capital ruining the code, or will I have to type exactly "Hurr durr" in chat with the capitalized H? Because I've tried typing it with and without capital H, nothing.
« Last Edit: July 21, 2013, 02:11:23 pm by Decicus »
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Chat Triggers
« Reply #10 on: July 21, 2013, 05:39:50 pm »
The code "strong.lower" converts everything you type into lower case to make the string comparison more compatible. I made a mistake when I put "Hurr durr" in my example.


for example:

local test = "ThIs Is All KiNdS of CaPitAlIsM"

test = string.lower(test)

print(test)

the result would be "this is all kinds of capitalism"


The idea is that it would recognize whenever a player typed "hurr durr" no matter how they capitalize it.

I fixed my example code.

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Chat Triggers
« Reply #11 on: July 22, 2013, 04:09:53 am »
Now it worked completely fine with both tsay and tsayColor. Now one last question, just to be sure. I looked up "string.sub", while I was reading about it, I didn't find the information I wanted (there's a possibility that I just misunderstood it), but I'd like to know if you have to specify when the exact StartPos/EndPos is. What if I want it to recognize if "hurr durr" was said somewhere in the chat? I mean: If "hurr durr" was included in a message, and that would still trigger "I LIKE TO HERP DERP". Is this possible? I haven't found anything about it yet, although, as I said I might have missed it.

Thanks for all the help so far! It's all appreciated, this question is probably my last one, after that I think I'll be able to piece my own code together.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Chat Triggers
« Reply #12 on: July 22, 2013, 10:51:01 am »
string.sub does require at least a start point and returns a string derivative of the string you pass it.

To do what you want, take a look at string.find

An example of this would be...

Code: [Select]
function HurDerp( ply, text, _ )

if ( string.find( string.lower(text), "hurr durr" ) then
ULib.tsay( ply, "I LIKE TO HERP DERP")
end

end
hook.Add( "PlayerSay", "HurDerp_Hook", HurDerp )

string.find returns the start and stop position of a string within another string. But simple checking it as a boolean will work because it will return nil if it doesn't find the string within the target string.

Please ask any questions you have. We (I especially) enjoy helping other people learn lua.

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Chat Triggers
« Reply #13 on: July 22, 2013, 11:08:53 am »
Ah, thank you, it makes sense to me now.

I said that was probably my last question, sort of lied, because now I have another question. I have a script that triggers when someone joins (it's sort of not connected to this, but it sort of is in a way).

So whenever a specific person joins, it triggers a playsound that works fine, but it should also print a message. The issue I'm having, is that I want it to grab the person's username (the one that's joining) while using ULib.tsayColor to print a colored message. I've tried adding ply:Nick() into it, but it doesn't work. Is there a way I can do it without having to manually add the username to each player in the group, and let it grab the username from the one they're currently using?
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Chat Triggers
« Reply #14 on: July 22, 2013, 04:09:33 pm »
Sure that ply is the variable in your code for the player object?
<player_object>:Name() may work for you too.
Really difficult to know what's wrong without a code example.
Can depend on where the code is running too (server side or client side)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

  • Print