A simple computercraft query...

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here

Malkeus Diasporan

New Member
Jul 29, 2019
56
0
0
Its pretty sad, but I can't get this simple cc program to work. Any help would be appreciated. The intention is to output a redstone signal to a redpower filter positioned behind the turtle after every 10th successful hit. It quits with an error (boolean expected) on line 10, which is (redstone.setOutput(back, false))
I'm sure I'm doing something completely stupid, please point it out for me. :)
This is the code:
local hit = 0
while true do
if turtle.attack() then
hit = hit +1
end
if hit >= 10 then
hit = 0
redstone.setOutput(back, true)
end
redstone.setOutput(back, false)
end
 

Malkeus Diasporan

New Member
Jul 29, 2019
56
0
0
That fixed its first complaint, tyvm, and just as i expected I am really bad at this. Now the program will attack once, then lock up expecting a boolean string on line 3.

edit:
i rewrote this pos, and now it does half of what i want, with no errors. It just attacks and never does the restone output :/
local hit = 0
while true do
hit = hit + 1
turtle.attack()
if hit >= 10 then
redstone.setOutput(''back'', true)
redstone.setOutput(''back'', false)
end
end
 

McMutton

New Member
Jul 29, 2019
18
0
0
It probably does the Redstone Signal, but way to fast to let RP2 register it. Put a sleep(0.5) between the state changes of the redpower output (sleep for half a second)

Edit: FavoriteFox was way faster than me. :)
 

Abdiel

New Member
Jul 29, 2019
1,062
0
0
First, use [ code ] forum tags to properly format your code here. It makes it much more readable:

Code:
local hit = 0
while true do
  hit = hit + 1
  turtle.attack()
  if hit >= 10 then
    redstone.setOutput(''back'', true)
    redstone.setOutput(''back'', false)
  end
end

Second, you need at least a one tick delay between setting redstone on and off, otherwise the game won't register it.

Third, you are never decrementing the hit variable in your main loop. This way, it would emit a redstone signal after every hit past the 10th. I assume you wanted to do something like the following:

Code:
local hit = 0
while true do
  hit = hit + 1
  turtle.attack()
  if hit >= 10 then
    redstone.setOutput(''back'', true)
    sleep(0.1)
    redstone.setOutput(''back'', false)
    hit = 0
  end
end
 

Malkeus Diasporan

New Member
Jul 29, 2019
56
0
0
oh wow, thanks FavoriteFox, it works perfectly now :)[DOUBLEPOST=1360841741][/DOUBLEPOST]Ah, i didn't have that in the second version Abdiel :) Is there any way to get my program to count the number of hits its getting as I originally intended? I want to have this pull out the stack after every 10 kills to minimize the amount of items in the pipes at any one time. My pc lags bad enough with a couple dozen pigs in the kill chamber.
 

silenos

New Member
Jul 29, 2019
42
0
0
First, you really need to add some sleep commands into that infinite loop otherwise it could very well crash your game ;)
Second, I'm pretty sure the redstone output has to stay on for at least 1 tick (1/20th of a second) to get recognised by the filter (again sleep() )
Third, you don't reset your hit variable after the 10th hit, so the if statement will stay always true after the 10th hit

Edit: ah too slow :)
 

Abdiel

New Member
Jul 29, 2019
1,062
0
0
oh wow, thanks FavoriteFox, it works perfectly now :)[DOUBLEPOST=1360841741][/DOUBLEPOST]Ah, i didn't have that in the second version Abdiel :) Is there any way to get my program to count the number of hits its getting as I originally intended? I want to have this pull out the stack after every 10 kills to minimize the amount of items in the pipes at any one time. My pc lags bad enough with a couple dozen pigs in the kill chamber.

Right, then you want your condition to be something like
Code:
if hit % 10 == 0 then
The % sign stands for modulus, or remainder after integer division. In layman's terms, every time hit is divisible by 10, the condition will be true and your turtle will send a signal.
 

Malkeus Diasporan

New Member
Jul 29, 2019
56
0
0
thank you everyone for your help, this has been driving me nuts for the last 2 hours. I'm at the detail stage now, couldn't have done it without you :)