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

problem with events

Started by Brian87, 18 March 2014 - 08:26 PM
Brian87 #1
Posted 18 March 2014 - 09:26 PM
Hello!
I'm trying to make the computer sleep for a second, and while it sleeps, it should check if it's getting a redstone signal on top of it, if there is no signal on top, it should run "startup". It just doesn't work :(/> Here is my code:


local function wait()
  local timer = os.startTimer(1)
  while true do
    local event = os.pullEvent("timer")
    if event == "timer" then
	  break
    elseif not rs.getInput("top") then
	    shell.run("startup")
    end
  end
end
while true do
side = {"right", "left", "front", "back"}
local t = math.random(#side)
  for i = 1, t do
    local x = math.random(1, #side)
   local random_side = side[x]
   rs.setOutput(random_side, true)
    table.remove(side, x)
  end
  wait()
  side = {"right", "left", "front", "back"}
  for i = 1, #side do
   rs.setOutput(side[i], false)
  end
  wait()
end
CometWolf #2
Posted 18 March 2014 - 09:35 PM

	local event = os.pullEvent("timer")
	if event == "timer" then
		  break
	elseif not rs.getInput("top") then
			shell.run("startup")
	end
Yeah, this won't work. Your computer will always yield(freeze) when you use os.pullEvent, until it gets an event of the desired type. You'll have to use the redstone event aswell, which fires when the computer detects a change in the connected redstone. However, it does not specify where, so you'll still have to use the getInput function aswell.
Edited on 18 March 2014 - 08:35 PM
wieselkatze #3
Posted 18 March 2014 - 09:37 PM
In your loop you're just waiting for the event "timer". So when this event triggers, only "break" will be executed. The other part won't ever run.

You could just do sth. like this for your loop:

while true do
local event = os.pullEvent()
  if event == "timer" then
    break
  elseif event == "redstone" then
    if rs.getInput("top") then
    shell.run("startup")
    end
  end
end
Brian87 #4
Posted 18 March 2014 - 10:22 PM
Thank you, that worked :D/> One more question, do I have to boot the computers manually every time the chunk gets unloaded? I've had some problems with this before: the chunk gets unloaded and I have to click on every computer to boot them up.
theoriginalbit #5
Posted 18 March 2014 - 11:59 PM
if should also be noted that when checking the timer event you should also check the returned ID against the one you queued, you cannot always guarantee that your program is running in an environment where only your program is creating things like timers. code example:

local timer = os.startTimer(1)
while true do
  local event, param = os.pullEventRaw()
  if event == "timer" and param == timer then
    break
  end
end

also make note of the use of os.pullEventRaw, this is to prevent users from being able to hold CTRL+T and terminate your program :)/>


while true do
side = {"right", "left", "front", "back"}
local t = math.random(#side)
  for i = 1, t do
	local x = math.random(1, #side)
   local random_side = side[x]
   rs.setOutput(random_side, true)
	table.remove(side, x)
  end
  wait()
  side = {"right", "left", "front", "back"}
  for i = 1, #side do
   rs.setOutput(side[i], false)
  end
  wait()
end
what is the purpose of this code compared to your question? it seems to me that it will randomly turn on between 1 and 4 random redstone sides and then turn them off again.
Bomb Bloke #6
Posted 19 March 2014 - 12:32 AM
For what it's worth, this script appears to be an evolution of this one. He's basically making a disco ball and switching it from rednet control to redstone.

Thank you, that worked :D/> One more question, do I have to boot the computers manually every time the chunk gets unloaded? I've had some problems with this before: the chunk gets unloaded and I have to click on every computer to boot them up.
You "shouldn't" have to do this, but I gather some older versions of ComputerCraft have had it as a bug. For some people. Some users have reported it with packs I've used without problems.

Some of the 1.6pre releases do seem to have this sort of behaviour.
Brian87 #7
Posted 19 March 2014 - 06:40 AM
Okay, thanks for all the answers :D/>