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

New guy questions

Started by Brekkjern, 09 March 2012 - 07:54 PM
Brekkjern #1
Posted 09 March 2012 - 08:54 PM
Blah blah, first post. Hello!

I have been learning lua the last 48 hours and I am working on an elevator. Now, there are a few things I am wondering about regarding the bundled cables.
  1. When you set a colour using "rs.setBundledOutput("right",colours.magenta)", will only that colour be true and everything else be false even though they were set to true earlier?
  2. If so, how do I turn on and off specific cables without setting the entire bundle to false except for the cable I want.
  3. Is there any way to listen for specific redstone events without blockingthe computer?
I am sure to come up with more questions so this thread will live long and hopefully prosper. Thank you for all the help you can get me.
Liraal #2
Posted 09 March 2012 - 09:06 PM
1. It'll be the only color true.
2. Try sth like:

local side="back"
local color=colors.magenta
local tmp=rs.getBundledOutput(side)
if colors.test(tmp, color) then tmp=colors.subtract(tmp, color) else tmp=colors.combine(tmp, color) end
rs.setBundledOutput(side, tmp)
3. There is, but it's not simple. IT requires either a specially designed code, or advanced LUA.
Casper7526 #3
Posted 09 March 2012 - 09:59 PM

x = 0
while true do
x = x + 1
print ("I've spammed you ".. x .. " times")
os.queueEvent("fake")
event, param = os.pullEvent()
   if event == "redstone" then 
   print "REDSTONE CHANGED"
   break
   end
end
Advert #4
Posted 10 March 2012 - 02:17 AM
1. It'll be the only color true.
2. Try sth like:

local side="back"
local color=colors.magenta
local tmp=rs.getBundledOutput(side)
if colors.test(tmp, color) then tmp=colors.subtract(tmp, color) else tmp=colors.combine(tmp, color) end
rs.setBundledOutput(side, tmp)
3. There is, but it's not simple. IT requires either a specially designed code, or advanced LUA.

I wouldn't exactly call it advanced; you just need to call a few functions. (Also, Lua, not LUA!)

Here's a helper function that will toggle any amount of colors (you can give them in string form (lightBlue, etc), or in number form (1 or colors.white, etc):
http://pastebin.com/rjwtAYz4
I've tried to make it give you useful error messages, but I've not tested it that much; and specifying the same colors multiple times will cause it to toggle them that many times (e.g toggleBundleColor("left", 1, 1, 1, 1) would not change the output of the white color at all).
Brekkjern #5
Posted 10 March 2012 - 04:56 AM
We got there with the first example by adjusting it a bit:


-- Function for toggling a signal on and off with a specific delay and not turning off any other signal.
function fireSignal( wire, delay )
  if not delay then delay = 2 end
  local bundle = rs.getBundledOutput(LEVEL)
  rs.setBundledOutput(LEVEL, colours.combine(bundle, wire)) -- Add wire to the bundle
  sleep(delay*TICK)
  rs.setBundledOutput(LEVEL, colours.subtract(bundle, wire)) -- Sub wire on the bundle
end

Thank you all for your help. We are getting close to making a functional program now. Just a few hundred more lines left :mellow:/>/>
Advert #6
Posted 10 March 2012 - 06:12 AM
We got there with the first example by adjusting it a bit:


-- Function for toggling a signal on and off with a specific delay and not turning off any other signal.
function fireSignal( wire, delay )
  if not delay then delay = 2 end
  local bundle = rs.getBundledOutput(LEVEL)
  rs.setBundledOutput(LEVEL, colours.combine(bundle, wire)) -- Add wire to the bundle
  sleep(delay*TICK)
  rs.setBundledOutput(LEVEL, colours.subtract(bundle, wire)) -- Sub wire on the bundle
end

Thank you all for your help. We are getting close to making a functional program now. Just a few hundred more lines left :mellow:/>/>

If you're using an os.pullEvent loop, I would strongly recommend not using sleep().

While your program is sleep()ing, any events fired will be ignored!

A solution to this would be to keep track of what is being toggled using a table, and using os.startTimer() to start a timer event to toggle your redstones, but it could get messy.

In pseudo-ish code:

local toggles = {}

function toggleBundle(side, color)
...toggle color in bundle
end

function timerEvent(timer)
if toggles[timer] then
  toggleBundle(toggles[timer[1]], toggles[timer[2]])
  toggles[timer] = nil
end
end

function fireBundle(side, color, delay)
if side/color is already toggled then -- Would need a reverse lookup to make this simple (perhaps a table with: "side:color" as the key, with the timer table as value)
  return false
end
toggleBundle(side, color)
local timerT = os.startTimer(delay)
toggles[timerT] = {side, color}
return timerT
end

while true do
... event loop here ...
if sEvent == "timer" then
  timerEvent(p1)
end
...