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

Help with a tricky function

Started by MaryuZ, 12 July 2012 - 11:08 AM
MaryuZ #1
Posted 12 July 2012 - 01:08 PM
First off I'm using Tekkit so if you don't know what a filter and a detector are they're red-power-2 machines that manipulate items and send/receive Red-stone signal when doing so.
The filter when sent a Red-stone signal(impulse) pulls one item out of an adjacent inventory and sends it through a connected tube.
The detector sends a Red-stone signal(impulse) when an item goes through it.
To the code…I'm making this gambling machine that takes in items turns them into credits that you play with and then returns items based on how many credits you have.
The computer (CC computer) that controls it has 4 buttons, 2 filters and a detector connected to it.
The code is made up of 4 functions corresponding to the 4 buttons : Load, Bet, Play and Pay.
I have Bet, Play and Pay sorted out but i can't figure out Load.
How it's all connected:
Mechanics: input chest -> input filter -> detector -> storage chest -> output filter -> output chest
Electronics: Computer- bundled wire - red wire - Load button- input filter
- bundled wire - blue wire - Bet button
- bundled wire - white wire - Play button
- bundled wire - black wire - Pay button
- bundled wire - green wire - output filter
What i want Load to do:
when i press load a signal is sent to the computer via the red wire and the input filter wich sends an item on it's way to the storage chest.
when the computer receives that signal I want the computer to wait for a signal from the pink wire and, if and when it get's it to send a signal through the red wire ( only the red wire not the green wire as well) and start the loop again ( start waiting for the pink signal again).
also if any wait is greater than 2 seconds i want the function to exit
would this work?

sd="bottom" --this is a global variable for string side as all my cables are connected to the bottom
credits=0 --global variable for credits
function load()
local k=40
 while k>0 do
 if rs.testBundledInput(sd , colors.pink) then
  credits=credits+10
  rs.setBundledOutput(sd, colors.red)
  sleep(0.05)
  rs.setBundledOutput(sd, 0)
  break
  else
  k=k-1
  sleep(0.05)
  end
 end
end
Or should i use os.pullEvent() ?
MaryuZ #2
Posted 12 July 2012 - 01:12 PM
I should mention i call load like this:

while 1 do
logo(bet,credits)
if rs.testBundledInput(sd , colors.red) then
load()
elseif
rs.testBundledInput(sd , colors.blue) then
sb()
elseif
rs.testBundledInput(sd , colors.white) then
play(bet)
elseif
rs.testBundledInput(sd , colors.black) then
pay()
end
sleep(0.05)
end
MaryuZ #3
Posted 12 July 2012 - 01:35 PM
I officially hate vanilla buttons!!!
I modified the code to be more vanilla button compatible but now it won't even load one item worth of credits…the code above loaded one item worth of credits.
Here's what i did to the code….what in the name of Dan200 am I doing wrong?

function load()
local sbv=1
while sbv>0 do
local k=40
while k>0 do
if rs.testBundledInput(sd , colors.pink) then
  credits=credits+10
  rs.setBundledOutput(sd, colors.red)
  sleep(0.2)
  rs.setBundledOutput(sd, 0)
  break
  else
  k=k-1
   if k==0 then
   sbv=0
   end
  sleep(0.05)
  end
end
end
end
while 1 do
logo(bet,credits)
if rs.testBundledInput(sd , colors.red) then
sleep(0.8)
load()
elseif
rs.testBundledInput(sd , colors.blue) then
sleep(0.8)
sb()
elseif
rs.testBundledInput(sd , colors.white) then
play(bet)
elseif
rs.testBundledInput(sd , colors.black) then
sleep(0.8)
pay()
end
sleep(0.05)
end
MaryuZ #4
Posted 13 July 2012 - 02:12 PM
anyone?
I'm like really stuck here…pls help.
kazagistar #5
Posted 13 July 2012 - 07:17 PM
You should use os.pullEvent("redstone") for sure. Here is some basic documentation. You wait for a restone event, then test which of the events it was (if two colors change in the same tick on the same wire, it will still register as the same event, afaik, that is why I say "events" plural). There is some kind of strangeness going on with the pullEvent("timer") in sleep() eating up redstone messages silently.

Also, a button will sent signals out for many ticks. You have to store the previous state of each input, and only trigger whatever it is you want to happen when the state changes from off to on.