• Help with ChatPrint and Color
    13 replies, posted
I'm working on editing Flood to learn lua, and I'm trying to make some text a different color but i still haven't figured it out. What I've tried, and it does fail is : v:ChatPrint( "Just some sample text.", Color (255, 0, 0, 255)) Is there a different way to do this SUCCESFULLY? Please let me know. I appreciate all help.
Here is a major tip. Do NOT start learning lua from flood mod. Here is what you should do. Go to Garrysmod.org and search Skeleton. Download that folder. That is everything basic for a game mode provided kindly by Garry himself. Then go to [url]http://wiki.garrysmod.com/?title=Lua_Tutorial_Series[/url] and you will see a game mode section. Those guides will HELP you. Trust me.
I've already studied that, I'm just trying to learn this gamemode. I'm working on making my own gamemode too, so yea. I just need to know how to add color to text..
You have the order wrong, you set the color, then the text. [lua]v:ChatPrint(Color(255,0,0),"Moar sample text!")[/lua] So far as I know, anyway. I think you can also provide a numerically indexed table, but I'm not all too sure.
Are you sure you're not looking for this? [b][url=http://wiki.garrysmod.com/?title=Chat.AddText]Chat.AddText [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
Whitewater this is the error i got with that code. [code] ERROR: GAMEMODE:'Think' Failed: Flood\gamemode\Players.lua:313: bad argument #1 to 'ChatPrint' (string expected, got table) [/code] and CrazyQuebec, ill try. [editline]09:08PM[/editline] Both of those posts gave errors, any other ideas?
Dvondrake's example is perfect but in this case you might want to abuse the fact that it's clientside to reduce the strain on the networking. For instance you could store the strings and colors you'll need clientside and then use the server to specify which ones you should be using, instead of having to send the whole string each time. [editline]12:22AM[/editline] Sure it'd work. But optimization can't be a bad thing. :smile:
[code] ERROR: GAMEMODE:'Think' Failed: Flood\gamemode\Players.lua:10: bad argument #1 to 'String' (string expected, got nil) Error - Starting new message without ending the old one! [/code]
Well in his case it's for a gamemode so I suppose he wants to send specific strings. And even if he needs to include information about players/who won/what not you can still do a great deal clientside. [editline]12:52AM[/editline] [QUOTE=mdew355;21071461][code] ERROR: GAMEMODE:'Think' Failed: Flood\gamemode\Players.lua:10: bad argument #1 to 'String' (string expected, got nil) Error - Starting new message without ending the old one! [/code][/QUOTE] We can't help with that unless we have the code you're using.
[lua] function GAMEMODE:OnPlayerChat( pl, txt, team, dead ) if !string.find( txt, "|" ) then return self.BaseClass:OnPlayerChat( pl, txt, team, dead ) end local Lines = string.Explode( "|", txt ) local parse = {} -- Normal chatbox stuff for the status and name. if dead then table.insert( parse, Color( 255, 30, 40 ) ) table.insert( parse, "*DEAD* " ) end if team then table.insert( parse, Color( 30, 160, 40 ) ) table.insert( parse, "(TEAM) " ) end if IsValid( pl ) then table.insert( parse, pl ) else table.insert( parse, "Console" ) end table.insert( parse, Color( 255, 255, 255 ) ) table.insert( parse, ": " ) -- Predefined colors. Feel free to add your own. Just follow the table, and DON'T forget your commas! local Colors = { ["red"] = Color( 255, 0, 0 ), ["pink"] = Color( 255, 0, 255 ), ["purple"] = Color( 102, 0, 153 ), ["blue"] = Color( 0, 0, 255 ), ["cyan"] = Color( 0, 255, 255 ), ["green"] = Color( 0, 255, 0 ), ["yellow"] = Color( 255, 255, 0 ), ["gold"] = Color( 255, 255, 0 ), ["orange"] = Color( 255, 123, 0 ) } for _, v in ipairs( Lines ) do if Colors[v] then table.insert( parse, Colors[v] ) else if v != "" then table.insert( parse, v ) end end end chat.AddText( unpack( parse ) ) return true end [/lua] This is a good method. I use it for putting colors in my chatbox. My players can actually change the color of parts of their messages with this so you may need to edit it to make a new type of ChatPrint with it that uses color. EDIT: It's a client-side file. EDIT: And the players can multi-color their messages. e.g. "red|This text is red. |blue|Now it is blue :D |gold|Now it is the color of piss."
What? It's not like it's a secret function or anything.. In fact cheiftiger's version is based heavilly on how it's implemented in the base gamemode.
Sorry, you need to Log In to post a reply to this thread.