PDA

View Full Version : SendChatMessage() Error in Code


thejoester
19-05-2008, 12:19 AM
I am having an error doing a SendChatMessage() to send a notification to guild chat to announce a "ding". In this message I want to include my /played information so I have the following code:


to get time played in string:
if (event == "TIME_PLAYED_MSG") then
if (arg1 ~= nil) then
--Get time played
timePassed = SecondsToTime(arg1);
end
end

then to send the message I do:
SendChatMessage("Ding Notification!!! - I just reached level " .. newlvl .. " in " .. timePassed, "GUILD", nil,nil);

this is giving the followin error:

Date: 2008-05-18 15:36:10
ID: 52
Error occured in: Global
Count: 1
Message: SendChatMessage(): Invalid escape code in chat message
Debug:
[C]: ?
[C]: ?
[C]: ?
[C]: SendChatMessage_Org()
ChatMOD\inc\ChatMOD.lua:571:
ChatMOD\inc\ChatMOD.lua:554
Identity\Identity.lua:147: SendChatMessage()
DingRecorder\DingRecorder.lua:79: GuildNotify()
[string "GuildNotify("2");"]:1: in main chunk
[C]: RunScript()
..\FrameXML\ChatFrame.lua:1598: value()
..\FrameXML\ChatFrame.lua:3040: ChatEdit_ParseText()
..\FrameXML\ChatFrame.lua:2732: ChatEdit_SendText()
..\FrameXML\ChatFrame.lua:2753: ChatEdit_OnEnterPressed()
[string "*:OnEnterPressed"]:1:
[string "*:OnEnterPressed"]:1

Any Ideas?

I did a test code of

DEFAULT_CHAT_FRAME:AddMessage("timePassed: " ..timePassed)

and it shows up as "timePassed: 50 Days 10hrs" so I dont know what could be an escape code.

Tunga
19-05-2008, 01:07 AM
Do you have a backslash "\" in either of those variables? Backslash is the Lua escape character which means you have to double it.

thejoester
19-05-2008, 01:49 AM
Do you have a backslash "\" in either of those variables? Backslash is the Lua escape character which means you have to double it.

there should be no backslash character in either. The newlvl should be a number (the new level) and the timePassed should display as something like "10 days 3 hrs"

Moongaze
19-05-2008, 08:59 AM
The problem lies within the new 2.4.2 changes to Blizzard's internal strings. They now have singular/plural conditions inside the string itself.

So, calling SecondsToTime(1234567) will return the string:

"14 |4Day:Days; 6 |4Hr:Hrs"

And sending that to the chat screen will not be pretty. To get around this, you can do something really simple:

Create a button, or a text field, or a caption. ANYTHING that lets you use the "SetText()" method. Make sure it is hidden so the user doesn't know about it. Then, do this:


button:SetText(SecondsToTime(time_played_in_seconds));
local timePlayed = button:GetText();
SendChatMessage("blah blah" .. timePlayed);


Doing so will automatically format the text to be displayed to the button and have it displayed properly. Then, you get the text from the button (after it's been converted) and display it to the chat. That's the down-and-dirty was of doing it. I'm sure there is a way to use the string library to bypass the need to do this, but this is just a simple remedy for now.