Jump to content




[problem][solved] Instant messaging problem


  • You cannot reply to this topic
4 replies to this topic

#1 Awesomesause200

  • Members
  • 18 posts

Posted 19 May 2013 - 11:54 AM

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


#2 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 19 May 2013 - 03: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


#3 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 19 May 2013 - 03:56 PM

Thanks to rednet.run, rednet_message still works. I would be very, very surprised if this was the solution.

#4 Awesomesause200

  • Members
  • 18 posts

Posted 19 May 2013 - 06:13 PM

View PostGrim Reaper, on 19 May 2013 - 03:55 PM, said:

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

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 May 2013 - 11:58 PM

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users