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

[problem][solved] Instant messaging problem

Started by Awesomesause200, 19 May 2013 - 10:54 AM
Awesomesause200 #1
Posted 19 May 2013 - 12:54 PM
I used the following code:
local w,h = term.getSize()
local sides = {"top","bottom","left","right","front","back"}
local Version = "Block talk v.1"
local name = "nil"
local ChatMessages = { }
local function printCentered(str,ypos)
  term.setCursorPos(w/2-#str/2,ypos)
  term.write(str)
end
local function printRight(str,ypos)
  term.setCursorPos(w-#str,ypos)
  term.write(str)
end
local function discover()
  for i = 1,#sides do
	if peripheral.isPresent(sides[i]) then
	  if peripheral.getType(sides[i]) == "modem" then
		rednet.open(sides[i])
	  end
	end
  end
end

discover()
while true do
  term.clear()
  printCentered("-Welcome to "..Version.."-",1)
  printCentered(string.rep("-",w),2)
  
  e = {os.pullEvent()}
	if e[1] == "rednet_message" then
	  table.insert(ChatMessages,e[3])
	end
  
  local y = 3
  for i = 1,#ChatMessages do
	local x = 1
	term.setCursorPos(x,y)
	print(ChatMessages[i])
	local y = y + 1
  end
end

What I am trying to do is when the computer recieves a message it will add it to the table"ChatMessages" and then it will printout all of the stuff in the table

When I broadcast a message from a computer right next to it,It fails to print the message out and the screen remains blank

EDIT:

local w,h = term.getSize()
local sides = {"top","bottom","left","right","front","back"}
local Version = "Block talk v.1"
local name = "nil"
local ChatMessages = { }
local function printCentered(str,ypos)
  term.setCursorPos(w/2-#str/2,ypos)
  term.write(str)
end
local function printRight(str,ypos)
  term.setCursorPos(w-#str,ypos)
  term.write(str)
end
local function discover()
  for i = 1,#sides do
	if peripheral.isPresent(sides[i]) then
	  if peripheral.getType(sides[i]) == "modem" then
		rednet.open(sides[i])
	  end
	end
  end
end
discover()
while true do
  term.clear()
  printCentered("-Welcome to "..Version.."-",1)
  printCentered(string.rep("-",w),2)
	  
  local y = 3
  for i = 1,#ChatMessages do
	local x = 1
	term.setCursorPos(x,y)
	print(ChatMessages[i])
	local y = y + 1
  end

  e = {os.pullEvent()}
	if e[1] == "rednet_message" then
	  table.insert(ChatMessages,e[3])
	end

end

now the problem is when i send a message it overwrites the old text and doesnt go down a line

the way to fix it is by editing the for loop like this:
Spoiler

local w,h = term.getSize()
local sides = {"top","bottom","left","right","front","back"}

local Version = "Block talk v.1"

local name = "nil"
local ChatMessages = { }

local function printCentered(str,ypos)
  term.setCursorPos(w/2-#str/2,ypos)
  term.write(str)
end

local function printRight(str,ypos)
  term.setCursorPos(w-#str,ypos)
  term.write(str)
end

local function discover()
  for i = 1,#sides do
    if peripheral.isPresent(sides[i]) then
	  if peripheral.getType(sides[i]) == "modem" then
	    rednet.open(sides[i])
	  end
    end
  end
end

discover()
while true do
  term.clear()
  printCentered("-Welcome to "..Version.."-",1)
  printCentered(string.rep("-",w),2)
 
  local x = 1  
  local y = 3
  term.setCursorPos(x,y)
  for i = 1,#ChatMessages do
    print(ChatMessages[i])
  end
 
  e = {os.pullEvent()}
    if e[1] == "rednet_message" then
	  table.insert(ChatMessages,e[3])
    end
    
end
Grim Reaper #2
Posted 19 May 2013 - 04:55 PM
The problem with your code is that the event for wireless messages is no longer "rednet_message". The proper event name is now "modem_message".
So, in your program, then blocK:

if e[1] == "rednet_message" then
  table.insert (ChatMessages, e[3])
end

should be:

if e[1] == "modem_message" then
  table.insert (Chatmessages, e[3])
end
LBPHacker #3
Posted 19 May 2013 - 04:56 PM
Thanks to rednet.run, rednet_message still works. I would be very, very surprised if this was the solution.
Awesomesause200 #4
Posted 19 May 2013 - 07:13 PM
The problem with your code is that the event for wireless messages is no longer "rednet_message". The proper event name is now "modem_message".
So, in your program, then blocK:

if e[1] == "rednet_message" then
  table.insert (ChatMessages, e[3])
end

should be:

if e[1] == "modem_message" then
  table.insert (Chatmessages, e[3])
end

Turns out this isnt the solution x.x

the problem I think is the table.insert line isnt working
thanks for the help anyways
theoriginalbit #5
Posted 20 May 2013 - 12:58 AM
I just tested this in CCEmu (since CCDesk is having rednet problems for me) and it works perfectly fine http://puu.sh/2XacD.png … do you have modems on BOTH computers? and are you opening the modem on the sending computer?

You do have one problem that I noticed, line 34, remove the local and your messages will start printing correctly. However I would make this suggestion for the printing of the GUI


term.clear()
--# prepare to draw the messages first (this way if the terminal scrolls we can still print our header at the top
term.setCursorPos(1,3)
--# make the entire table of messages into a single string where each message is separated by a new line
local messages = table.concat(ChatMessages, '\n')
write(messages)
--# now draw the header
term.setCursorPos(1,1)
term.clearLine()
printCentered("-Welcome to "..Version.."-",1)
printCentered(string.rep("-",w),2)

Also I'm not sure if you noticed, but your horizontal line is off by one, to fix this I suggest this for your printCentered function (I'm going to split out the calculations so I can better explain them)

local function printCentered(str, ypos)
  --# first we calculate half of the screen given the width of the screen and the string, then we round it down
  local xpos = math.floor((w-#str)/2)
  --# now we add an offset for even length strings so they look like they are in the center (they look centered when to the right of center, not left)
  --# this performs a modulo (%) on the length of the string with 2 to see if it's even length
  xpos = xpos + (#str % 2 == 0 and 1 or 0)
  term.setCursorPos(xpos, ypos)
  term.write(str)
end

EDIT: Oh also instead of defining the table of sides, you can do a for loop like this

for _, side in pairs( rs.getSides() ) do
  -- check for you modems and such here with the side variable
end
rs.getSides returns a table of the computers sides