Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal

standinonstilts

Members
  • Content Count

    3
  • Joined

  • Last visited

Posts posted by standinonstilts

  1. 18 hours ago, payonel said:
    
    local thread = require("thread")
    local event = require("event")
    
    local t = thread.create(function()
    	while true do
    		local pack = table.pack(event.pull("modem_message"))
    		print(table.unpack(pack))
    	end
    end)
    
    while true do
    	local command = io.read()
    	if command == "quit" then
    		break
    	end
    end
    
    t:kill()

     

    my code was very close to yours. the only difference was that instead of creating a function inside the thread.create(), I created a seperate function named handleMessage() and called it as: 

    local t1 = thread.create(handleMessage()) 

    whenever I do this, I cannot type input, it's like the thread is blocking the io.read; but when you define the function inside thread.create() it allows me to read and print perfectly fine. Is there a reason for this or is it a bug?

     

    my code:

     

    local event = require("event")
    local thread = require("thread")
    local modem = component.modem

    modem.open(77)

    function handleMessage()
      while true do
        local pack = table.pack(event.pull("modem_message"))
        print(table.unpack(pack))
      end
    end

    local t1 = thread.create(handleMessage()) 

    while true do
      local command = io.read()
      print(command)
    end

    t1:kill()

  2. I want to create a server that pulls the modem_message event and prints to the console, but I also want the computer to be able to send messages (based on io.read()) while waiting for a message. I tried using threads, but I think I am using them wrong. As far as I know there is no thread.sleep() so I'm not sure how to suspend a thread for a short period of time so that you can still type messages, but still have that thread automatically continue. How would I go about doing this?

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.