Need help with easy computer craft code

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

Wekmor

New Member
Jul 29, 2019
939
0
1
Hey guys, I just started doing some coding on a turtle. What I want the turtle to do is to break the block infront of it, select the right slot (1 or 2) and place that item (I will always have 1 item of the block that is getting mined, and 2 of the other in the turtles inventory).

I have it to break and place what is in slot 1 if there is a redstone signal applied from the left side and break and place what is in slot 2 if there is a redstone signal applied from the right side.
But it randomly stops working. So it does nothing when I press the button next to the turtle.

Here is the bit of code I wrote (remember, I never did any coding at all ;s)

Code:
function Cargo()
  turtle.select(1)
  sleep(.25)
  turtle.dig()
  sleep(.25)
  turtle.place()
end
 
function Track()
  turtle.select(1)
  sleep(.25)
  turtle.dig()
  sleep(.25)
  turtle.select(2)
  sleep(.25)
  turtle.place()
end
 
while true do
os.pullEvent("redstone")
  if redstone.getInput("right") then
    Cargo()
  end
 
os.pullEvent("redstone")
  if redstone.getInput("left") then
    Track()
  end
 
end

Two ingame pictures here:

What the turtle's work place looks like

2013-10-09_19.23.06.png


What the turtle's inventory looks like

2013-10-09_19.23.11.png

For the function Track() I have this select(1) because if slot 2 is selected and the turtle breaks a block the item goes to slot 3 rather than slot 1 where there is already this item



Can anyone explain to me why it randomly stops working? There is no error or something showing up in the turtle.






(Somehow this whole wall of text looks like it makes no sense ._.)
 

Fuzzlewhumper

New Member
Jul 29, 2019
500
0
0
Try running your program.
Then either log out or get far enough from the chunk with the turtle so that it becomes un-chunk-loaded.
If it stops running when you come back / log back in - then the issue may not be your code but how turtles work when the chunk they are in is no longer loaded.

How to fix?
Make your code be stored in a file called startup

A turtle with a file called startup will run startup and execute the code in that file.

In this way, the turtle will always be running that program even if you leave and come back.

ALSO

Your code made also be self terminating due to it running a loop and effectively not doing anything. I've heard turtles need to have a sleep(##) command in their main loop. This may not be the case any more but it doesn't hurt to add that just in case.
 

Wekmor

New Member
Jul 29, 2019
939
0
1
It's not that the program stops. For examps, if I press the left button everything works. But the right button then does not. Apparently since idk like 10 minutes the right button doesn't work at all.
Which makes even less sense to me, since it's basicly the same code as for the left button :s

I will try the sleep in the loop tho, thanks for that


Edit: Wooh! That 0.25 second sleep time after the if then statement did it! Thanks alot :)

But now the cc noob shows again. How do I make it so that I can do the right button 3 times in a row?
The way I have the code right now only works when pressing the two buttons alternately

Edit again: No it doesn't work how I want it to. The program seems to only run if there is a normal track (not the dropoff rail) infront of the turtle and I start with the left button then. So I think I'll just stop using this turtle stuff for now xD
 

xBTx

New Member
Jul 29, 2019
42
0
0
It's not that the program stops. For examps, if I press the left button everything works. But the right button then does not. Apparently since idk like 10 minutes the right button doesn't work at all.
Which makes even less sense to me, since it's basicly the same code as for the left button :s

I will try the sleep in the loop tho, thanks for that


Edit: Wooh! That 0.25 second sleep time after the if then statement did it! Thanks alot :)

But now the cc noob shows again. How do I make it so that I can do the right button 3 times in a row?
The way I have the code right now only works when pressing the two buttons alternately

Edit again: No it doesn't work how I want it to. The program seems to only run if there is a normal track (not the dropoff rail) infront of the turtle and I start with the left button then. So I think I'll just stop using this turtle stuff for now xD

I tested it, and this solves both of your problems. Change you're while loop to this:

Code:
while true do
os.pullEvent("redstone")
  if redstone.getInput("right") then
    Cargo()
elseif redstone.getInput("left") then
    Track()
  end

Edit: You can add a sleep if you want, but it works without it.
 

kittle

New Member
Jul 29, 2019
229
0
0
hrm... try this?

Code:
while true do
  os.pullEvent("redstone")
  if redstone.getInput("right") then
    Cargo()
  end
  if redstone.getInput("left") then
    Track()
  end
end

Also, you may need to wait between pressing buttons. It also may help to lay a little redstone wire so you can see exactly when the left and/or right is lit up
 

ratchet freak

Well-Known Member
Nov 11, 2012
1,198
243
79
the reason it happens is so turtles don't suck up all the CPU of a server, a wait (or a pullevent) will let the server do other things

it is essentially a forced self throttling of the lua
 

Wekmor

New Member
Jul 29, 2019
939
0
1
I tested it, and this solves both of your problems. Change you're while loop to this:

Code:
while true do
os.pullEvent("redstone")
  if redstone.getInput("right") then
    Cargo()
elseif redstone.getInput("left") then
    Track()
  end

Edit: You can add a sleep if you want, but it works without it.

That thing works wonders! Thanks so much! :)