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

Newb In Lua, Need Help

Started by Okashu, 26 February 2012 - 02:05 PM
Okashu #1
Posted 26 February 2012 - 03:05 PM
I saw this mod on reddit and found it interesting. To learn a bit coding I decided to imrpove "lumberjack turtle" code from the video. Here is what I have:

function drzewo()
repeat
if turtle.detect()==true then
turtle.dig()
turtle.up()
elseif turtle.detectUp()==true then
turtle.digUp()
turtle.up()
elseif turtle.detectUp()==false and turtle.detect()==false and turtle.detectDown()==false then
turtle.down()
end
until turtle.detect()==false and turtle.detectDown()==true and turtle.detectUp==false
do return end
end
while 1==1 do
while turtle.detect()==false do
x=math.random(6)
if x==1 or x==2 or x==3 then
turtle.forward()
elseif x==4 then
turtle.turnLeft()
elseif x==5 then
turtle.turnRight()
elseif x==6 then
turtle.turnRight()
turtle.turnRight()
end
end
while turtle.detect()==true do
drzewo()
end
end

I want turtle to chop down the tree then walk randomly (couldn't think of better system for finding trees), and chop down tree when he meets one

My problem is when turtle meet an obstacle, it freezes. I'm guessing I used "return" wrong in function drzewo(), but I don't know how to use it correctly. I am only starting out with programming and make many mistakes, so probably it's stupid mistake.
Casper7526 #2
Posted 26 February 2012 - 03:07 PM
while 1==1 do
while turtle.detect()==false do
x=math.random(6)
if x==1 or x==2 or x==3 then
turtle.forward()
elseif x==4 then
turtle.turnLeft()
elseif x==5 then
turtle.turnRight()
elseif x==6 then
turtle.turnRight()
turtle.turnRight()
end
end

Thats an infinite loop

break — to get out of loops
return —- to get out of functions (or the program itself if your not in a program)
Okashu #3
Posted 26 February 2012 - 03:12 PM
First "end" ends "if x==1 or x==2 or x==3 then" and second "end" ends "while turtle.detect()==false do". I want "while 1==1 do" to be an infinite loop, because it ends after I tell him to chop down the tree. If I want him to break out of the loop (stop walking and chopping trees) I will reboot him manually
Liraal #4
Posted 26 February 2012 - 03:55 PM
replace while 1==1 with while true, just for better look, but yor mistake is that do return end should actually be return true.

BTW, are you from Poland by chance?
Okashu #5
Posted 26 February 2012 - 05:14 PM
Hm, still does not work. Turtle chops down tree and sits there.

Yes, I'm polish. Did drzewo tell you? ^^
Advert #6
Posted 26 February 2012 - 05:54 PM
replace while 1==1 with while true, just for better look, but yor mistake is that do return end should actually be return true.

BTW, are you from Poland by chance?
You don't need to return true; you can just do return.

Here are some pointers:
1) You don't need to check if something is true in a condition; if it's not nil or false, then the condition will pass (even for 0, or ""!).

2) You can use not to pass conditions when the input is false:


if turtle.detect()==true then
-- This can become:
if turtle.detect() then

-- Likewise, your checks if something is false can also be shortened by the not keyword:
while turtle.detect()==false do
-- Can become:
while not turtle.detect() do

Also, if you don't mind, could you repaste the code you currently have?
Okashu #7
Posted 26 February 2012 - 06:21 PM
Feel free to post correct version so I can check how it should look

EDIT: whoops, read your post wrong. I haven't changed it much, just "do return end" became "return"
Liraal #8
Posted 26 February 2012 - 06:37 PM
you can put only 'return' but 'return true' looks and behaves better ;)/>/>

and yes, it was drzewo that told me it. my apps are 100% English though :)/>/>
Okashu #9
Posted 26 February 2012 - 07:32 PM
Here is my improved code which doesn't change anything but with Advert's advices

function drzewo()
repeat
if turtle.detect() then
turtle.dig()
turtle.up()
elseif turtle.detectUp() then
turtle.digUp()
turtle.up()
elseif not turtle.detectUp() and not turtle.detect() and not turtle.detectDown() then
turtle.down()
end
until not turtle.detect() and turtle.detectDown() and not turtle.detectUp
return
end


while true do
while not turtle.detect() do
x=math.random(6)
if x==1 or x==2 or x==3 then
turtle.forward()
elseif x==4 then
turtle.turnLeft()
elseif x==5 then
turtle.turnRight()
elseif x==6 then
turtle.turnRight()
turtle.turnRight()
end
end
while turtle.detect() do
drzewo()
end
end