This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
deletedededed's profile picture

Vertical "Chat" scrolling

Started by deletedededed, 21 November 2014 - 02:25 AM
deletedededed #1
Posted 21 November 2014 - 03:25 AM
I've been working on a chat system for my faction "Oblivion" as said in previous posts. So far I've produced a display/input system using two computers per user. My problem is that once the monitor fills up there is no convenient way to clear the old text from the first line without removing the more recent messages! ex. http://gyazo.com/3f5...3bac46e9523b7cc

Input (To send a message to other users)
rednet.open("top")
var = read()
rednet.broadcast(var)
shell.run("Sender1")

Monitor output (shows messages from yourself and all other users)
rednet.open("top")
m = peripheral.wrap("right")
password = 42
while true do
event, id, message, distance = os.pullEvent("rednet_message")
if password == 42 then
local cX,cY = m.getCursorPos()
m.setCursorPos(1,cY+1)
m.write(message)
if cY >= 25 then ---should clear first 3 lines if there are more then 25 (Max)----------
term.scroll(3)[/color] ---doesn't respond/doesn't give output------------
end
end
sleep(0.1)
end

The commented code is what I'm having issues with


Just for reference, disk drives are disabled on the server (dupe glitches from older versions) so I wanted to keep this code as simple as possible to enter upwards of 10 times.
Edited on 21 November 2014 - 02:34 AM
valithor #2
Posted 21 November 2014 - 03:55 AM
I am not certain of this, but you would probably want to use m.scroll(3) instead of term.scroll(3). Term is still refering to the computer itself, while m is the monitor. Because of this you are telling the computer terminal itself to scroll the text on the computer not the monitor.
Edited on 21 November 2014 - 02:55 AM
Dragon53535 #3
Posted 21 November 2014 - 04:11 AM
SpoilerMy first thought is to fill a table full of all previous chats and when that reaches 25 chats, delete the first three.


local m = peripheral.wrap("right")
rednet.open("top")
local password = 42
local tbl = {} --#Initialize an empty table
local mX,mY = m.getSize() --# Get the max size, so we know not to go down a line.
local firstMessage = true
while true do
  event, id, message, distance = os.pullEvent("rednet_message")
  if password == 42 then
	local cX,cY = m.getCursorPos()
	  if #tbl >= mY then --#In case you use m.setTextScale()
		table.remove(tbl,1)
		table.remove(tbl,1)
		table.remove(tbl,1) --#Remove the first 3 indexes. Table.remove automatically shifts them. I initially had it as 1,2,3. Fixed
		m.clear()
		for a = 1, #tbl do --#Starting at one and ending at the amount of entries in tbl
		  m.setCursorPos(1,a)
		  m.write(tbl[a])
		end
		  m.setCursorPos(1,cY-3)
	  elseif not firstMessage then --#So it doesn't skip a line at the beginning...
		m.setCursorPos(1,cY+1) --#If the check above didn't clear.
	  end
	  table.insert(tbl,message)
	  m.write(message)
	  firstMessage = false
	end
  sleep(0.1)
end

Just to clarify why I posted this. To do two things, one, show you how you can do it without using m.scroll() (Use scroll, probably more efficient anyways) and because Valithor replied while I was making this and I didn't want to just erase it.
After reading and testing valithor's answer. I can say that his is correct, and easier to use.
Edited on 21 November 2014 - 04:38 AM
deletedededed #4
Posted 21 November 2014 - 08:26 PM
Here is my "Edited" code, It seems now that the monitor wont respond to new output. Anything typed into the Clients simply doesn't affect the server. I did some testing and found the issue but I am not knowledgeable enough to know how to fix it. (problem lines are marked with –Problem)

rednet.open("top")
m = peripheral.wrap("right")

while true do
event, id, message, distance = os.pullEvent("rednet_message")
local cX,cY = m.getCursorPos()
m.setCursorPos(1,cY+1)
m.write(message)
if cY == 25 then --Problem
m.scroll(1) --Problem
end

sleep(0.1)
end

EDIT: With more testing I found that the issue is still scrolling the monitor, The server still runs with the added changes
Edited on 21 November 2014 - 07:29 PM
valithor #5
Posted 21 November 2014 - 10:17 PM
Here is my "Edited" code, It seems now that the monitor wont respond to new output. Anything typed into the Clients simply doesn't affect the server. I did some testing and found the issue but I am not knowledgeable enough to know how to fix it. (problem lines are marked with –Problem)

rednet.open("top")
m = peripheral.wrap("right")

while true do
event, id, message, distance = os.pullEvent("rednet_message")
local cX,cY = m.getCursorPos()
m.setCursorPos(1,cY+1)
m.write(message)
if cY == 25 then --Problem
m.scroll(1) --Problem
end

sleep(0.1)
end

EDIT: With more testing I found that the issue is still scrolling the monitor, The server still runs with the added changes

I suspect that it is technically working, but you are just writing stuff off of the screen. The y coords for writing is likely greater than the size of the monitor.

Here is how I would fix it.

rednet.open("top")
m = peripheral.wrap("right")

while true do
event, id, message, distance = os.pullEvent("rednet_message")
local cX,cY = m.getCursorPos()
m.setCursorPos(1,cY+1)
m.write(message)
if cY == 25 then --Problem
m.scroll(1) --Problem
m.setCursorPos(24) -- The scroll will clear the 25th line, so you need to set this to the 24th line since you increase the line selected by one a few lines up
end

sleep(0.1)
end
Edited on 21 November 2014 - 09:23 PM
deletedededed #6
Posted 22 November 2014 - 01:03 AM
I've updated my Server to match this format and it seems I am having the issue yet again. The clients are Broadcasting the information, the server is receiving it, however it wont display on the monitors. Yes, I have checked through my code multiple times and copied exactly what changes needed to be made. I don't think the issue is necessary the scrolling its something to do with how order is interpreted into the monitor
Dragon53535 #7
Posted 22 November 2014 - 01:21 AM
Perhaps put m.write after your if statement
deletedededed #8
Posted 22 November 2014 - 02:53 AM
That would just create a missing variable loop. Nothing would ever count text from the first "While true do" statement meaning that the "if cY == 25" would never receive any lines. It would never set of so text would never be wrote. Its like a redundant code where the output triggers the input

Edit: added some quotes
Edited on 22 November 2014 - 01:53 AM
Dragon53535 #9
Posted 22 November 2014 - 03:12 AM

rednet.open("top")
m = peripheral.wrap("right")
while true do
event, id, message, distance = os.pullEvent("rednet_message")
local cX,cY = m.getCursorPos()
m.setCursorPos(1,cY+1)
m.write(message) --#Move this
if cY == 25 then
m.scroll(1)
m.setCursorPos(24)
end
--#here
sleep(0.1)
end