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

I cannot get this rednet chat script to work

Started by bbgun09, 25 August 2013 - 03:03 PM
bbgun09 #1
Posted 25 August 2013 - 05:03 PM
Title: I cannot get this rednet chat script to work

I've been teaching myself some lua in computercraft and lately I've been messing around with rednet. I'm trying to make a simple chat script that displays messages in a sort-of irc format. Whenever it receives a rednet message it just hangs and won't respond to anything other than ctrl+s. Here's my code:

aa = " "
bb = " "
cc = " "
dd = " "
ee = " "
ff = " "
gg = " "
hh = " "
ii = " "
jj = " "
kk = " "
ll = " "
mm = " "
nn = " "
oo = " "
pp = " "
qq = " "
rr = "Logged on"

function updateChat()
  term.setCursorPos(1,1)
  aa = bb
  print(aa)
  bb = cc
  print(bb)
  cc = dd
  print(cc)
  dd = ee
  print(dd)
  ee = ff
  print(ee)
  ff = gg
  print(ff)
  gg = hh
  print(gg)
  hh = ii
  print(hh)
  ii = jj
  print(ii)
  jj = kk
  print(jj)
  kk = ll
  print(kk)
  ll = mm
  print(ll)
  mm = nn
  print(mm)
  nn = oo
  print(nn)
  oo = pp
  print(oo)
  pp = qq
  print(pp)
  qq = rr
  print(qq)
  rr = chat
  print(rr)
end

updateChat()
rednet.open("back")
term.clear()
term.setCursorPos(1,19)
print("Chat V0.1 - hyphon to quit")
event, id, chat = os.pullEvent()
a = true
while true do
  sleep(0)
  if event == "key" and id == 41 then
	term.setCursorPos(1,1)
	term.clear()
	break
  elseif event == "rednet_message" then
	sleep(0)
	updateChat()
  end
end

If you could help me that'd be awesome! Thanks.

edit: spelling :o/>
H4X0RZ #2
Posted 25 August 2013 - 08:53 PM
Why are you doing this thing with aa,bb,cc… Just use tables:

local history = {}
local currInput = ""
local lastHistory = #history
local w,h = term.getSize()

--Backup some functions
local gS = term.getSize
local sCP = term.setCursorPos
local c = term.clear

--Overriting some functions
function term.getSize()
  return w,h-1
end
function term.setCursorPos(oX,oY)
  local oY= oY
  if oY > h-1 then
    oY = h-1
  end
  sCP(oX,oY)
end
function term.clear()
  local cW,cH = term.getSize()
  for i = 1, cH do
    term.setCursorPos(1,i)
    for j = 1, cX do
      term.write(" ")
    end
  end
end

--Functions
local function addChatMessage(msg)
  table.insert(history, msg)
end
local function drawScreen()
  while true do 
    sCP(1,h)
    term.clearLine()
    sCP(1,h)
    write(currInput)
    term.setCursorPos(1,1)
    term.clear()
    if lastHistory ~= #history then
      for k,v in pairs(history) do
        print(v)
      end
      lastHistory = #history
    end
  sleep(0)
  end
end

local function handleEvents()
  while true do

    local evt, p1,p2,p3 = os.pullEvent()
    if evt == "char" then
      currInput = currInput..p1
    elseif evt == "key" then
      if p1 == keys.enter then
        --Send the message
        currInput = ""
      elseif p1 == keys.return then
        curInput = string.sub(currInput, 1, #currInput-1)
      end
     elseif evt == "rednet_message" then
       addChatMessage("<"..p2..">"..p1)
     end
    sleep(0)
    end
  end
end
parallel.waitForAny(drawScreen, handleEvents)
bbgun09 #3
Posted 25 August 2013 - 09:39 PM
Why are you doing this thing with aa,bb,cc… Just use tables:

local history = {}
local currInput = ""
local lastHistory = #history
local w,h = term.getSize()

--Backup some functions
local gS = term.getSize
local sCP = term.setCursorPos
local c = term.clear

--Overriting some functions
function term.getSize()
  return w,h-1
end
function term.setCursorPos(oX,oY)
  local oY= oY
  if oY > h-1 then
	oY = h-1
  end
  sCP(oX,oY)
end
function term.clear()
  local cW,cH = term.getSize()
  for i = 1, cH do
	term.setCursorPos(1,i)
	for j = 1, cX do
	  term.write(" ")
	end
  end
end

--Functions
local function addChatMessage(msg)
  table.insert(history, msg)
end
local function drawScreen()
  while true do
	sCP(1,h)
	term.clearLine()
	sCP(1,h)
	write(currInput)
	term.setCursorPos(1,1)
	term.clear()
	if lastHistory ~= #history then
	  for k,v in pairs(history) do
		print(v)
	  end
	  lastHistory = #history
	end
  sleep(0)
  end
end

local function handleEvents()
  while true do

	local evt, p1,p2,p3 = os.pullEvent()
	if evt == "char" then
	  currInput = currInput..p1
	elseif evt == "key" then
	  if p1 == keys.enter then
		--Send the message
		currInput = ""
	  elseif p1 == keys.return then
		curInput = string.sub(currInput, 1, #currInput-1)
	  end
	 elseif evt == "rednet_message" then
	   addChatMessage("<"..p2..">"..p1)
	 end
	sleep(0)
	end
  end
end
parallel.waitForAny(drawScreen, handleEvents)

Thanks! And I'm a noob… so that's why… :)/>