To show the methodology of a non-programmer learning to write LUA programs. The hope is that this journal will serve to help others, help myself, and perhaps entertain.
Updating may be slow as I have both a full time job and a full time family but the idea is that with the journal being so public it will push me to update more regularly.
Project 1 - My First Real Program
Goal -
When done I hope to have a program that will allow me to give a turtle variable height, length, and width measurements and it will clear the area defined by those inputs and return to its origin.
Phase 1 -
The goal for phase one will be to get the turtle to clear an area above itself as defined by the input of the user and then return to ground.
I will post any code that I write along with what I think is going on with the code before I test, what I expect to happen when I run it, what actually happens, and anything I learn or any question that I have.
So first code and test:
Spoiler
print("How far up?")
Arg = (...) -- Gets up distance from user
if Arg ~= nil then -- Does this insure the input is a number?
Arg = tonumber -- converts the input to a LUA number
u = 1
while u = Arg do -- this is going to be our loop
if not turtle.detectUp() then -- should make the turtle look for a block above and if false do the next line
turtle.up()
end
u = u + 1 -- this makes the loop go from 1 to user input
else
print ("Well that didn't work") -- this happens only if Arg is not a number I think
end
So it should print on the screen
How far up?
Then let me put in a number and the turtle should go up that amount.
I wonder what happens if turtle.detectUp() returns true? A simple halt? or will it do that thing where it waits for that to be true of for the terminal to be halted??
I'm not sure I fully understand Arg, does it have to be Arg? Could I substitute Aup? I'll try that later. I get the u = u + 1 loop thing but its still weird ( if I did while u = Arg would it only move up once? or would it fail out at the first loop? I'll have to try that.
time to run this code…be right back =D
Ok so I wrote the code in notepad++ and saved it as tup.lua so when I go into my turtle it shows the file as tup.lua, how can I just have it show the name with no extension? the other files in there are just listed as file. Ok saved as file type all files with no extension and it worked…. that's so odd =/
ran program tup2
result:
bios:338: [string"tup2"] :14: 'end'
expected (to close 'while' at line 8)
So something is wrong on line 8?
added an end statement, ah ok I didn't end all three conditions this should fix it
Spoiler
print("How far up?")
Arg = (...) -- Gets up distance from user
if Arg ~= nil then -- Does this insure the input is a number?
Arg = tonumber -- converts the input to a LUA number
u = 1
while <= Arg do -- this is going to be our loop
if not turtle.detectUp() then -- should make the turtle look for a block above and if false do the next line
turtle.up()
end
end
u = u + 1 -- this makes the loop go from 1 to user input
else
print ("Well that didn't work") -- this happens only if Arg is not a number I think
end
How far up?
Well that didn't work
—————————-
Sometime later after some sleep.
So I'm using the example from the turtle.forward page off the wiki as a base… is Arg a lua thing or was it chosen for clarity? Changed all instances of to tUP and ran the code from the wiki, it worked so Arg isn't a lua thing its just an argument. The (…) must just say "look at what the user wrote and set that to Arg or tUP or whatever"
So here is the code so far
Spoiler
Arg=(...) -- Gets up distance from user
if Arg ~= nil then -- Does this insure the input is a number?
Arg = tonumber(Arg) -- converts the input to a LUA number
a = 1
while a <= Arg do -- this is going to be our loop
if not turtle.detectUp() then -- should make the turtle look for a block above and if false do the next line
turtle.up()
end
end
a = a + 1 -- this makes the loop go from 1 to user input
else
print ("Well that didn't work") -- this happens only if Arg is not a number I think
end
Ahha, I didn't encapsulate the iterater(?is that a real thing?) within the loop that had the a <= Arg statement, because of that a never incremented up and was always less than Arg creating an infinite loop, lets fix it and see what happens—- be right back.
It worked!!!!!
Well that's good but it's not exactly what I want, so tonight if I get any free time I'll look into doing it in a manner such that the user is prompted to enter the 'Up' amount and also try and get the turtle to go back down….and maybe if I'm real ambitious have it report back that it went up x and returned.
When I get home from work we'll try out any code changes and let you know what happens.
Todays lessons:
be more careful wrapping code blocks, you could have lost a turtle were it not for a ceiling =/
Arg (…) isn't lua for argument is the following, well it is but you don't have to type Arg, you can type what ever you want.
the tonumber , tostring function (argument?, command?) converts some input to number or string or whatever.
I will work on make this more visual (adding screenshots and maybe some video)
cheers
edit - also need to figure out how to do the spoiler thing with code