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

How to have multiple lines in chat program

Started by vyse, 24 December 2014 - 08:40 AM
vyse #1
Posted 24 December 2014 - 09:40 AM
hello, so i have been working on a chat program between two computers so players far away may talk to each other very simply, but the problem i have been running into is the fact that i can only have one line for messages so for example if someone has hi in first line and someone else talks it replaces that line with the new text when i want it to go down to a new line. so if you may know a way to fix that problem it would be awesome, i have tried get cursorpos() and then (1,y+1) and it still replaces the frist line. any ideas?


while true do
  paintutils.drawLine(1,17,52,17,colors.lightGray)
  term.setCursorPos(1,17)
  write("Type:")
  chat = read()
  term.setCursorPos(1.3)
local x, y = term.getCursorPos()
   if chat ~= nil then
   print(userID .. ":" .. chat)
	 if chat == exit then
	 print("bye")
	 sleep(0.1)
break
	 end
	end
end

there is more but this is the most important part ^
Edited on 24 December 2014 - 08:59 AM
The_Cat #2
Posted 24 December 2014 - 09:42 AM
Would it be possible if you uploaded your code to pastbin or something?
vyse #3
Posted 24 December 2014 - 09:54 AM
i can copy it if you wish
Cycomantis #4
Posted 24 December 2014 - 10:45 AM
The following line has a typo, replace the . with a ,

term.setCursorPos(1.3)


There are probably several ways of accomplished what you are trying to do. You do not want to use iterations such as (1,y+1) as eventually you will be printing off of the screen. You could do something a long the lines of capturing the messages in a table then print the messages from the table to the screen only showing the most recent entries and only enough to fill the display portion of the screen.

Something like

local tMsgs = {}
local w,h = term.getSize()

if #tMsgs > h then
    local startPos = #tMsgs - h
    for x=startPos, #tMsgs do
        print(tMsgs[x])
    end
else
    for x=1, #tMsgs do
        print(tMsgs[x])
    end
end
Edited on 24 December 2014 - 09:49 AM
The_Cat #5
Posted 24 December 2014 - 10:49 AM
Hi, so these are the changes i made. I have commented the code, if it didn't help just say. I wont be offended :D/>


term.clear()--#Clears anything thing unwanted before going in the while true loop
y = 2 -- #This is y pos (you should probably rename this to like yTextPos)
while true do
term.setCursorPos(1,1)
term.clearLine() --# Clears a single line, if this isnt here it will keep what was said previously in the chat
write("Type:")
chat = read()
term.setCursorPos(1,y) -- #The y pos is there so it can add 1 on
if chat ~= nil then
  print(">" .. chat)
  y = y + 1 --#Adds one onto to y pos so next line that is said will be 1 down
  if chat == "exit" then --#I assume you want to type "exit" to quit the program so you need "exit" in quotes for this to work
   print("bye")
   sleep(0.1)
   break
  end
end
end


Cycomantis is right, you can probably use my code with his to achieve what you want. :)/>
Edited on 24 December 2014 - 09:52 AM
vyse #6
Posted 24 December 2014 - 06:02 PM
Thank you for the replies, yea probably some errors i was really tired, also i have all the functions on the top of the page.
ok i will try and mix match the 2 codes to see what works the best for me thanks so much.
vyse #7
Posted 24 December 2014 - 07:00 PM
THe_Cat a problem with your code is that when the ("Type:") is at top and just puts the message u said below it yes thats sort of right but the ("Type:") is actually on the bottom and it displays the message on the top line and goes down from there.
The_Cat #8
Posted 24 December 2014 - 07:36 PM
Oh ok i get what you want, i will try to sort it out.
vyse #9
Posted 24 December 2014 - 07:52 PM
Oh never mind cat i got it working thank you, but now i need to make sure it doesn't go to far.
The_Cat #10
Posted 24 December 2014 - 08:29 PM
That shouldn't be to difficult you just need to use a if statement really.