[Solved]New to programming, can't find the answer in the tutorials

Started by mattig89ch, Feb 12, 2020, 02:09 AM

Previous topic - Next topic

mattig89ch

hello all,

So I'm not actually sure where to look in the tutorials to figure this out.  I'm trying to create my first branch mine program.  I could just copy and paste one, already created.  But I wanted to try and make my own first.

As you can imagine, I'm running into problems.  Firstly, I've watched a few youtube vids, so I have a vague notion of how lua should look.  But I'm getting some weird errors.

My goal is to print out the turtle's current fuel level, go to the next line and ask how long the branch mine is.  The user (me in this case) types in a number, and it begins to mine.  I wanted to use a while do loop.  And simply subtract the input number by 1, every time.  Mine, move up, mine above.  All the while, checking there's something in front of it.  if I hit a ravine, or it reaches its limit, I wanted the turtle to stop and return.  here's what I wrote

local run = 0
local runcount = 0

term.write("Fuel Left" turtle.getFuelLevel /n)
term.write("Branch Length: ")
run = read()
runcount = run

while(turtle.detect() and runcount < 0 do)
     turtle.dig()
     turtle.forward()
     turtle.digUP()
     os.sleep(1.0)
     runcount = runcount - 1
end

turtle.turnLeft()
turtle.turnLeft()

while(run > 0) do
     turtle.foward()
     run = run - 1
end

I'm not 100% on the spelling, as I had to re-write this here.  This is on a public server, so I can't just copy and paste it.

I'm getting an error, saying that /n is an unexpected symbol.  When I remove it, its saying the input I'm trying to do is trying to match a number to a string.  And this is as far as I've gotten trying to figure this out.  I'm totally lost where to go from here.

Could someone point out what I'm doing wrong?  Or point me to someone trying to do something similar, so that I could learn from them?

SquidDev

To go over the two problems:

term.write("Fuel Left" turtle.getFuelLevel /n)
This is your first problematic line. I'm not quite sure how you got in this state, but that \n really shouldn't be there! You need something like the following:

term.write("Fuel Left" .. turtle.getFuelLevel())
--                     ^                     ^ () to call the function
--                     | Needed to join the string and fuel together

The second problem is a little more nuanced. Basically, read returns a string, so if you enter 23 it'll contain the value "23". When you do that comparison in the while loop, this is a problem as you're trying to compare a number to a string, and they're not the same!

The correct solution is to convert the result of read into a number:
run = tonumber(read())

Just as an aside, you can also use the pastebin program (http://www.computercraft.info/wiki/Pastebin_(program)) to upload files to the internet, which hopefully will save you from copying them out :).
GitHub | CC:Tweaked: A ComputerCraft fork | Plethora: A peripheral mod

mattig89ch

Thanks!  So...how do I get a new line?  When I searched for that question /n was the answer I found.

SquidDev

Quote from: mattig89ch on Feb 13, 2020, 01:36 PMThanks!  So...how do I get a new line?  When I searched for that question /n was the answer I found.
Almost! So you actually need "\n" (note the quotes and backslash instead of forward one). However, that doesn't work with normal term.write - you need to use write instead.

write("Fuel Left" .. turtle.getFuelLevel() .. "\n")
You can also use the print function, which does that all for you!

print("Fuel Left" .. turtle.getFuelLevel())
GitHub | CC:Tweaked: A ComputerCraft fork | Plethora: A peripheral mod

mattig89ch

Thanks for the help so far, but now I'm running into a new issue.  Its looking like the code isn't even entering into the first while loop.

local run = 0
local runcount = 0
local fuelUpdate = "Fuel Left: " .. turtle.getFuelLevel()
   
print(fuelUpdate)
print("Branch Length: ")
run = tonumber(read())
runcount = run

while turtle.detect() and runcount < 0 do
    turtle.dig()
    turtle.forward()
    turtle.digUp()
    os.sleep(1.0)
    while turtle.detect() do
        turtle.dig()
    end
    runcount = runcount - 1
end
 
turtle.turnLeft()
turtle.turnLeft()
 
while run > 0 do
    turtle.forward()
    run = run - 1
end

It just skips over the first loop, and goes to the return loop instead.

Also, thank you for the pastebin tip.  That helped a lot here.

Lupus590

I'm assuming that you are inputting a number greater than 0
Is there always something in front of the turtle?
You probably want to remove the turtle.detect from the while do part of the while loop

mattig89ch

I am putting it in front of a wall, yes.  I was going to use that, as a means to prevent the turtle from getting trapped over a ravine.

I was putting in 3, as the number, yes.

Dog

Here's why your first loop is being skipped...

while turtle.detect() and runcount < 0 do
Since runcount will theoretically be above zero as the script starts this will be bypassed.  Changing it to...

while turtle.detect() and runcount > 0 do
should fix it.

mattig89ch

Quote from: Dog on Feb 17, 2020, 12:43 AMHere's why your first loop is being skipped...

while turtle.detect() and runcount < 0 do
Since runcount will theoretically be above zero as the script starts this will be bypassed.  Changing it to...

while turtle.detect() and runcount > 0 do
should fix it.
Ha!  That fixed it!  Thanks!  Now to figure out how to add a torch placement thing in there next.  One Step at a time though.  Thanks!