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

Working On Chat Program - Freezing [Updated Code - Still Same Bug]

Started by bbgun09, 28 August 2013 - 08:55 PM
bbgun09 #1
Posted 28 August 2013 - 10:55 PM
I'm trying to make a irc-like chat program, but this code just freezes the computer- it doesn't even clear the screen.

edit: updated code - still freezing

Here is the code:
local event,id,msg = os.pullEvent()

local history = {}

local function recieve()
  while true do
	if event == "rednet_message" then
	  table.insert(history, "User "..id.." > "..msg)
	elseif event == "key" and id == 41 then
	  term.setCursorPos(1,1)
	  term.clear()
	  break
	end
  sleep(0)
  end
end

local function broadcast()
  while true do
	term.setCursorPos(1,19)
	rednet.broadcast(io.read)
	if event == "key" and id == 41 then
	  break
	end
  sleep(0)
  end
end

local function printHistory()
  while true do
	for i=#history-17,#history do
	  term.setCursorPos(1,i)
	  print(history[i])
	end
	if event == "key" and id == 41 then
	  break
	end
  sleep(0)
  end
end

term.clear()
table.insert(history, "Logged in - Hyphen to exit")
parallel.waitForAny(recieve,broadcast,printHistory)

If you could help me that would be awesome! Thanks.

Edit: Typo in the code… didn't stop the freezing though. Neither did opening the side (though it should at least tell me that was wrong..?). If you press hyphen weird stuff happens, code sometimes clears and stuff like that. Though the program never stops when hyphen is pressed.

Edit2: The program does crash when the rednet port is not open AND hyphon is pressed (it gives the no port open error message), also it doesn't say "Logged in - Hyphon to exit" until hyphen is pressed. Not really sure what's going on here… It's weird…

Edit3: Ignore edits one and two, I updated the code to fix the way I was using parallel api, it's still broken though.
Lyqyd #2
Posted 29 August 2013 - 01:23 AM
Split into new topic.

You're using parallel wrong. You're not supposed to call the functions you want it to execute, so your parallel call should look like this:


parallel.waitForAny(recieve,broadcast,printHistory)

The way you had it, it was calling just receive(), and waiting for it to return so it could pass the return value to the waitForAny call. It would have then done the same with broadcast and printHistory as they ended.

Also, it's spelled "hyphen". With an E.
bbgun09 #3
Posted 29 August 2013 - 07:16 AM
Split into new topic.

You're using parallel wrong. You're not supposed to call the functions you want it to execute, so your parallel call should look like this:


parallel.waitForAny(recieve,broadcast,printHistory)

The way you had it, it was calling just receive(), and waiting for it to return so it could pass the return value to the waitForAny call. It would have then done the same with broadcast and printHistory as they ended.

Also, it's spelled "hyphen". With an E.

I fixed that, still broken… It will close if you use hyphen before it recieves a rednet message… weird. Still refuses to let you type. :/
LBPHacker #4
Posted 29 August 2013 - 07:24 AM
That's not how you use coroutines properly. I see you make them yield using sleep(0), but that's not how you should do it. You should use os.pullEvent - which, as a bonus, would even fill your event variables (currently they hold nothing). You want to check the event variables after all, don't you? Get rid of the sleep(0)'s and put local event, id, msg = os.pullEvent() after every while true do.

EDIT: I realized you set the event, id, msg trio at the top of the program. The problem is, you do that once. Get rid of that line too.
Pinkishu #5
Posted 29 August 2013 - 07:34 AM
you're basically spamming rednet by broadcasting a function ID every 0.05s, i wouldn't be surprised if it gets laggy/freezes :P/>
i guess you wanted to do rednet.broadcast(io.read()) ?

Also, you're never pulling any events in your coroutines and sleep() will eat a couple events, so it never clears the screen because theres never any reason for it, because event doesnt become "key" and such
bbgun09 #6
Posted 29 August 2013 - 06:08 PM
That's not how you use coroutines properly. I see you make them yield using sleep(0), but that's not how you should do it. You should use os.pullEvent - which, as a bonus, would even fill your event variables (currently they hold nothing). You want to check the event variables after all, don't you? Get rid of the sleep(0)'s and put local event, id, msg = os.pullEvent() after every while true do.

EDIT: I realized you set the event, id, msg trio at the top of the program. The problem is, you do that once. Get rid of that line too.

Thanks! Still a bit buggy, but at least it works to an extent now! :)/>