We have multiple lines!
It was both easier and more difficult than I thought. My original plan was to build a table with reference numbers and then cycle through what was displayed with fun and math. Then I discovered table.insert included references, and table.remove happily removes the lowest. Talk about a time saver!
Then I spent the next few hours running in circles between strings table string table table not a table a string in a table table no not that table table table string untangle the string of tables!
Trying to figure out where a string ended up a table ended up a string was like
Also, table.concat doesn’t like new lines, but that’s fine, I put them in the strings near the beginning instead.
Whew.
Then with a little fiddling and guessing, the table of strings played nicely with concat and
In the end, 6 functions and dozens of lines of code and debug messages became… ducttape.script!
-- initial variables
--[[
So, my thoughts on this.
Need to display multiple lines.
So build a series of text messages and glue them together?
* get the line from "reachchatlog.script" (as a message)
* put line in an array
* format array as a single opbject to be displayed?
* send object to test_http.gui for display
* wait for next line
* older lines need removed
* add to array and reformat
* send new array to "test_http.gui"
--]]
local scriptn = "ducttape.script " -- debug
local functn = "functn " -- debug
local message_table_lines = 10 -- show last 10 lines from log
local message_table = {} -- behold! A table!
function init(self)
-- self.message_table = {}
end
function on_message(self, message_id, message, sender)
local functn = "on_message "
if message_id == hash("log_message") then -- check the message name
local taped_message = message.user .. ": ".. message.body .. "\n" -- put it together. don't use {} here.
local ducttaped = taped_message -- I really wanted to use ducttaped.
-- print(scriptn .. functn .. "taped_message " .. taped_message) -- debug
table.insert(message_table, ducttaped) -- put string in table at pos n+1
-- print(scriptn .. functn .. "message stored I hope") -- debug
local table_entries = table.maxn(message_table) -- see how many entries are in the table
-- print(scriptn .. functn .. "table entries: " .. table_entries) -- debug
if table_entries >= message_table_lines then -- if there are more entires than X
table.remove(message_table, 1) -- remove first entry
-- print(scriptn .. functn .. "removed message") -- debug
else
print(scriptn .. functn .. "no messages to remove, works fine to here.") -- debug
end
if table.maxn(message_table) >=1 then -- if there is a message
message_to_send = table.concat(message_table)
msg.post("go#test_http", "new_message_table", {table = message_to_send}) -- send messge to the GUI script
-- print(scriptn .. functn .. "sent message to test_http") -- debug
else
print(scriptn .. functn .. " nil table") -- debug
end
end
end
I also went through the other scripts and commented out debug prints. 30 lines of debugging for one line of text was getting silly. (:
Of special help in today’s adventure was Egor Skriptunoff’s comment in LUA - invalid value (table) at index 1 in table for 'concat' - Stack Overflow
And some comments here Table binding (passing tables as reference) - #3 by Vik and Table binding (passing tables as reference) - #3 by Vik
And now, for tomorrow!