Adding Message To Frame

Hola, so I am about to finish an add-on and release it, but I'm having a bit of trouble adding messages to the frame properly. At the moment it starts adding messages to the frame starting at the bottom, rather than the top.

Picture detailing what's happening. If I :SetInsertMode("TOP") it will start at the top, but it will also continue to put new messages above the previous messages which I do not want. I want to input my first message at the very top of the frame, and then continuously put more messages under that initial message as if it was a Word document. I can't use FontString because this addon will eventually go over the 4000 character limit.

Why don't you use one FontString per line, dynamically generating more as needed? It's what ScrollingMessageFrame does internally, too.

---------- Post added 2013-03-15 at 08:34 AM ----------

Looking at your picture, here's my first impression of how I'd do it:

- One FontString for the heading ("Vahalue - Tichondrius vs 2v2 Arena Teams")
- A numerically-indexed table of tables for the entries. Each sub-table holds two FontStrings. These are positioned appropriately within a container, starting at the top. You might want to consider using a ScrollFrame for the container. If you do, make sure the label is outside the container, as you don't want to scroll it.

Unfortunately I may be a little too deep to make such a fix. Here is a pastebin upload http://pastebin.com/RrCGAXm1, but it's 1800 lines and quite a mess since this is my first LUA project ever. 1580-1623 is an example of how I add messages to the frame. I'm actually also having an issue where if my minmaxvalue for the scrollbar is too high (or something) then the message is repeated as I scroll down (i think thats what happens anyways). Right now I simply want to loop through a table (which I do fine), AddMessage() to the ScrollFrame, and set the ScrollFrameBar's size properly so that the first line of text is the top, and the last line of text is the bottom of the scroll bar. If I could do something similar using FontString's I'm all for it since this is the absolute last thing I need to fix up before it is finished.

Well, the problem here is you're not really using ScrollingMessageFrame for its intended purpose.

If you'd like to adapt the code you pointed out to use my model, this is what you should change:

Turn line 1583 from an AddMessage to a SetText on the label.

Use ipairs instead of pairs on line 1584. pairs() is generic, while ipairs() is intended for numerically indexed sorted tables. The main difference is that ipairs() enforces sequence, while pairs() gives no such guarantee.

Between lines 1588 and 1589, fetch the entry frame to use (from 'container.entries', in my example). pCount can be used as an index.

Turn line 1618 from an AddMessage into a return.

Turn line 1589 from an AddMessage into a SetText on the top FontString ([1] in my example).

Turn line 1590 into a SetText on the bottom FontString ([2] in my example) using the return value we added in 4).

Adapt and add the hide-unneeded-entries code from my example after line 1592. Use (pCount+1) as a starting value.

PS: Something I wanted to point out while skimming over your code:
Multiple string concatenation is inefficient in Lua. As there is no compiler optimization, multiple concatenation causes the operations to be executed one after another, creating and discarding each intermediate string in memory. (Extreme) example:

Code:

local str = "this".." ".."is".." ".."a".." ".."long".." ".."string"

This creates the following strings in memory:

Code:

"this"
"this "
"this is"
"this is "
"this is a"
"this is a "
"this is a long"
"this is a long "
"this is a long string"

It is more efficient to use native concatenation, such as string.format (which can be called as a method on any string, e.g. ("Found something: %s"):format("efficiency")) or table.concat (e.g. table.concat({"this","is","a","long","string"}," ")).

PPS: You seem to have a dislike for scoping and using locals. Why is that? Any previous programming languages? I'm curious.

I don't dislike scoping and using locals, but yeah I didn't use them very often in this add-on. Mostly because at first I didn't understand them and resulted to keeping everything global, and never went back to correct them. I started programming last year January and most of my knowledge has come from courses (comp sci degree). This add-on is my attempt to add to my knowledge of languages.

Just make sure you give it a run over with Mikk's FindGlobals or equivalent and make sure you don't leak any of them. Regardless, for a first project, it's great, really. You don't want to see my first attempts at Lua. Trust me on that one.

Yeah I haven't learned anything about efficiency other than than Big-O. Taking a Data Structures and Programming in C++ course starting Monday, so hopefully each will touch on efficiency and I'll be able to compare. May just have to learn it on my own time, though.

I'll put up a link to the add-on wherever I end up putting it accoolnamehere. Hopefully before Monday since I'd like to be done before my new classes start.

Uploaded my add-on there if anyone who has viewed this thread is interested. I haven't been able to debug the 3v3 and 5v5 side of the database, and haven't done string.concat or anything like that, but once I know I've fixed up the majority of bugs (since I'm sure there will be some) I'll start working on efficiency slowly, or possibly just start a new project ;p.