ComputerCraft Code Problem

  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

Cephrus

New Member
Jul 29, 2019
12
0
0
Alright, so I'm setting up a steam boiler in my minecraft world, and I am terribly lazy when it comes to crafting sometimes. So instead of crafting an aqueous accumulator, I used 72 liquiducts to carry water from my main water feed in my base (please don't ask why). I was concerned it might not get enough water, so (still) i didn't create an aqueous accumulator, but instead started to work on a fail-safe mechanism, so the boiler doesn't explode.

The fail-safe mechanism involves cutting the fuel supply, so to do this i remove the liquiduct connecting the fuel tanks to the boiler, and I use a turtle to do that.

In experiment, the code always crashes with "too long without yielding," and I do a sleep for 5 seconds.

The code is here: http://pastebin.com/cHPVuWHB
 

Cephrus

New Member
Jul 29, 2019
12
0
0
Thanks.

The second portion of the code (where it replaces the liquiduct) doesn't seem to be executing. Any ideas?
 

BlackFire

New Member
Jul 29, 2019
344
0
0
Thanks.

The second portion of the code (where it replaces the liquiduct) doesn't seem to be executing. Any ideas?
It never will be because v = 2 and always will because you initialized it inside your "while" loop. Move that to the very top and it should work.
 

VikeStep

New Member
Jul 29, 2019
1,117
0
0
i know what is happening, look at the comments in the code
Code:
while true do
  local l = peripheral.wrap("right")
  l.setFreq(3)
  local v = 2
 
    if l.get() then
      if v == 2 then
        v = 1 --becomes 1 here
        turtle.dig()     
        sleep(5)
      end
    else -- the only time v = 1 is if l.get() == true, but if it is not then at the start of the loop v resets back to 2
      if v == 1 then -- v will never be 1 here since you reset it to 2 at the start of the loop
        turtle.place()
        turtle.up()
        turtle.attack()
        turtle.down()
        v = 2
      end
    end
end
 

Cephrus

New Member
Jul 29, 2019
12
0
0
So how would I define the variable "v" if i don't set it?

Sorry, I'm absolutely horrible at lua lol.

Nevermind I fixed the problem by making "v" a global variable.
 

BlackFire

New Member
Jul 29, 2019
344
0
0
you do initialize but outside your while loop like:

Code:
local v = 2 -- Initialize here!!!
while true do
  local l = peripheral.wrap("right")
  l.setFreq(3)
  -- local v = 2 , not here
 
    if l.get() then
      if v == 2 then
        v = 1
        turtle.dig()       
        sleep(5)
      end
    else
      if v == 1 then
        turtle.place()
        turtle.up()
        turtle.attack()
        turtle.down()
        v = 2
      end
    end
end
 

a.lie

New Member
Jul 29, 2019
8
0
0
why are you initializing the peripheral for every cycle? (hint: veery bad practice)
also ...

Code:
if l.get() and v == 2 then
  -- do stuff here
else
  -- do more stuff
end

second step: Booleans
Code:
local v = true
 
if l.get() and v then
-- ...
end
(of course rewrite where needed)
 

TheSandwichMakr

New Member
Jul 29, 2019
582
0
0
So, basically what you want to do is alternate between 2 blocks of code whenever l.get() returns true?

Try this
Code:
local l = peripheral.wrap("right")
l.setFreq(3)
local v = true
while true do
  if l.get() then
    if v then
      turtle.dig()
      v = false
    else
      turtle.place()
      turtle.up()
      turtle.attack()
      turtle.down()
      v = true
    end
  end
  sleep(5)
end
Otherwise could you further explain what you're trying to do?