-
Posted by lexara talionis on Sun, 23 Jan 2011 13:45:52
I've been working on an addon that puts up a small frame describing the mechanics of a boss encounter when you target that boss. This portion of the addon works flawlessly already, but I would also like a button that sends the description to a chat channel so that anyone with the addon can explain the boss quickly to those who've never done the encounter. This is where I am having some difficulty due to the fact that the description is often above 255 characters.
I basically just need some way to capture everything from the first letter to the last space before the 255th character, then start at that same space to continue with the next capture until the last space before an additional 255 characters, etc. I've been looking at the string utility functions in chapter six of your book and I can't figure out which one I would use to accomplish this or if I'm just going about it completely the wrong way. Any help you can give will be much appreciated.
Just in case, here's the button I'm talking about.
function MobExplainSendMessage_OnClick(self, button) local chatType if GetNumRaidMembers() >= 1 then chatType = "RAID"; elseif GetNumPartyMembers() >= 1 then chatType = "PARTY"; else chatType = "SAY"; end if strlen(mobEntry) > 255 then --Split the string at the last space before the 255th character, wash, rinse, repeat. else SendChatMessage(mobEntry, chatType); end end
-
Posted by jnwhiteh on Sun, 23 Jan 2011 19:54:11
I'd suggest the following:
function MobExplainSendMessage_OnClick(self, button) local chatType if GetNumRaidMembers() >= 1 then chatType = "RAID"; elseif GetNumPartyMembers() >= 1 then chatType = "PARTY"; else chatType = "SAY"; end -- Do this for every 255 char chunk (would be better if this didn't -- break on word boundaries). for i = 1, #mobEntry, 255 do local message = mobEntry:sub(i, i + 254) SendChatMessage(mobEntry, chatType) end end
-
Posted by lexara talionis on Mon, 24 Jan 2011 09:48:03
Thanks for the quick reply. There's a minor error in the code you gave.
for i = 1, #mobEntry, 255 do local message = mobEntry:sub(i, i + 254) SendChatMessage(mobEntry, chatType) end
In the third line SendChatMessage(mobEntry, chatType) should've been SendChatMessage(message, chatType).
This definitely got me on the right track, but I really wanted it to separate the text at spaces and eventually worked this out:
function MobExplainSendMessage_OnClick(self, button) local chatType, message, pattern, position; if GetNumRaidMembers() >= 1 then chatType = "RAID"; elseif GetNumPartyMembers() >= 1 then chatType = "PARTY"; else chatType = "SAY"; end position = 1; for i = 1, #mobEntry, 255 do message = mobEntry:sub(position, position + 254); if #message < 255 then pattern = ".+"; else pattern = "(.+)%s"; end for capture in message:gmatch(pattern) do SendChatMessage(capture, chatType); position = position + #capture + 1; end end end
So far it's working great! Thanks for the help and the spectacular book. I've had it for a couple years now and have only had to ask for help twice with it as my guide.
-
Posted by jnwhiteh on Mon, 24 Jan 2011 13:16:54
Great, glad you were able to get it working from my dry-coded example :P