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

Virtual Computer Freezes

Started by Sirharry0077, 02 April 2013 - 11:18 AM
Sirharry0077 #1
Posted 02 April 2013 - 01:18 PM
I am trying to make a chat room type program, but when it reaches the point where it checks for os events it freezes. I need to figure out why this happens. The code below is just part of a much larger code, but it contains all of the chat room code.

Pastebin
oeed #2
Posted 02 April 2013 - 01:50 PM

while true do						  --Point where computer freezes
					    sleep(1)
					    numberMsg = #messageTable
					    event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
					    if event == "key" then
							    if modemSide == 28 then		    -- Enter Key
									    table.insert(messageTable, msg)
									    modem.transmit(channel, channel, msg)
									    msg = username..": "
							    elseif modemSide == 14 then	    -- Backspace
									    msg = string.sub(msg, 1, string.len(msg) - 1)
							    end
					    elseif event == "char" then
							    msg = msg..modemSide
					    elseif event == "modem_message" then
							    table.insert(messageTable, message)
					    end
...

This is happening because os.pullEvent waits until an event is fired. In my opinion, the best way to do this is to restructure your program. You could use coroutines but I'm not familiar with them in Lua.
Sirharry0077 #3
Posted 02 April 2013 - 01:56 PM

while true do						  --Point where computer freezes
						sleep(1)
						numberMsg = #messageTable
						event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
						if event == "key" then
								if modemSide == 28 then			-- Enter Key
										table.insert(messageTable, msg)
										modem.transmit(channel, channel, msg)
										msg = username..": "
								elseif modemSide == 14 then		-- Backspace
										msg = string.sub(msg, 1, string.len(msg) - 1)
								end
						elseif event == "char" then
								msg = msg..modemSide
						elseif event == "modem_message" then
								table.insert(messageTable, message)
						end
...

This is happening because os.pullEvent waits until an event is fired. In my opinion, the best way to do this is to restructure your program. You could use coroutines but I'm not familiar with them in Lua.

One event is key press but even that just doesn't do anything.