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

Nested coroutine.yield?

Started by dasdre, 20 November 2014 - 02:39 PM
dasdre #1
Posted 20 November 2014 - 03:39 PM
I'm trying to make a floppy lock program. It should emit a redstone signal when a floppy is inserted, and disable it when it is removed. However, I can't get it to disable the signal when the floppy is removed.
Code: (relevant parts line 11-15)


-- side
local outputSide = "bottom"
-- key disk id
local keyDiskID = 1
while true do
	local rawEvent = {coroutine.yield()}
	local event = rawEvent[1]
	if event == "disk" then
		local side = rawEvent[2]
		if disk.getID(side) == keyDiskID then
			rs.setOutput(outputSide,true)
			local rawEvent = {coroutine.yield()}
			local event = rawEvent[1]
			if event == "disk_eject" then
				rs.setOutput(outputSide,false)
			end
		else
		disk.eject(side)
		end
	end
end
Dragon53535 #2
Posted 20 November 2014 - 03:57 PM
the event is never going to be disk and disk_eject

if event == "disk" then
  --#Do stuff
elseif event == "disk_eject" then
  --#Do stuff
end

Edit: read code wrong, one second

the event is never going to be disk and disk_eject

if event == "disk" then
  --#Do stuff
elseif event == "disk_eject" then
  --#Do stuff
end

Edit: read code wrong, one second

Edit2: Your problem is that it picks up the redstone change as an event and so it continues. your best bet would be


local rawEvent = {coroutine.yield("disk_eject")}
Edited on 20 November 2014 - 03:02 PM
KingofGamesYami #3
Posted 20 November 2014 - 04:00 PM
your second coroutine.yeild is probably getting a different event, and skipping the redstone shutoff entirely.