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

Help, What do I need to do next?

Started by anthonysilby, 18 September 2015 - 07:13 AM
anthonysilby #1
Posted 18 September 2015 - 09:13 AM
My code so far, its not much as I don't know how/what I should do next


[color=#000000][font=Arial][size=4]io.write("What is your name?")[/size][/font][/color]
[color=#000000][font=Arial][size=4]name = io.read()[/size][/font][/color]
[color=#000000][font=Arial][size=4]io.write("How many Pumpkins " ..name.. "?")[/size][/font][/color]
[color=#000000][font=Arial][size=4]Block[1] = io.read()[/size][/font][/color]
[color=#000000][font=Arial][size=4]io.write("How many Dirt Blocks " ..name.. "?")[/size][/font][/color]
[color=#000000][font=Arial][size=4]Block[2] = io.read()[/size][/font][/color]
[color=#000000][font=Arial][size=4]io.write("How many Hay Bales " ..name.. "?")[/size][/font][/color]
[color=#000000][font=Arial][size=4]Block[3] = io.read()[/size][/font][/color]
[color=#000000][font=Arial][size=4]io.write("How many Snow Blocks " ..name.. "?")[/size][/font][/color]
[color=#000000][font=Arial][size=4]Block[4] = io.read()[/size][/font][/color]
[color=#000000][font=Arial][size=4]io.write("How many Sand " ..name.. "?")[/size][/font][/color]
[color=#000000][font=Arial][size=4]Block[5] = io.read()[/size][/font][/color]
[color=#000000][font=Arial][size=4]io.write("How many Gravel " ..name.. "?")[/size][/font][/color]
[color=#000000][font=Arial][size=4]Block[6] = io.read()[/size][/font][/color]
[color=#000000][font=Arial][size=4]io.write("How many Red Sand " ..name.. "?")[/size][/font][/color]
[color=#000000][font=Arial][size=4]Block[7] = io.read()[/size][/font][/color]
[color=#000000][font=Arial][size=4]io.write("How many Soul Sand " ..name.. "?")[/size][/font][/color]
[color=#000000][font=Arial][size=4]Block[8] = io.read()[/size][/font][/color]
[color=#000000][font=Arial][size=4]io.write("How many Melons " ..name.. "?")[/size][/font][/color]
[color=#000000][font=Arial][size=4]Block[9] = io.read()[/size][/font][/color]

[color=#000000][font=Arial][size=4]function towall()[/size][/font][/color]
[color=#000000][font=Arial][size=4]while turtle.detect() == false do[/size][/font][/color]
[color=#000000][font=Arial][size=4]turtle.forward()[/size][/font][/color]
[color=#000000][font=Arial][size=4]end[/size][/font][/color]
[color=#000000][font=Arial][size=4]end[/size][/font][/color]

an examplar created by a teacher at my school to show different levels of achievement:
Edited on 19 September 2015 - 09:38 AM
valithor #2
Posted 18 September 2015 - 11:26 PM

io.write("What is your name?")
name = io.read()
io.write("How many Pumpkins " ..name.. "?")
Block[1] = io.read()
io.write("How many Dirt Blocks " ..name.. "?")
Block[2] = io.read()
io.write("How many Hay Bales " ..name.. "?")
Block[3] = io.read()
io.write("How many Snow Blocks " ..name.. "?")
Block[4] = io.read()
io.write("How many Sand " ..name.. "?")
Block[5] = io.read()
io.write("How many Gravel " ..name.. "?")
Block[6] = io.read()
io.write("How many Red Sand " ..name.. "?")
Block[7] = io.read()
io.write("How many Soul Sand " ..name.. "?")
Block[8] = io.read()
io.write("How many Melons " ..name.. "?")
Block[9] = io.read()

function towall()
  while turtle.detect() == false do
    turtle.forward()
  end
end

Cleaned up the formatting stuff for you.

So… What exactly do you want the program to do?
KingofGamesYami #3
Posted 18 September 2015 - 11:41 PM
I assume he wants to accomplish the challange in the video (bottom of post; text is the same color as the background).

I'm also going to assume Block is defined, as that code would error if it was not.

The next step would be to add the coordinates of each resource - eg.

local location = {}

location.melons = 5
location.soulSand = 10

*note: I'm mostly guessing on the actual distance, since the video isn't very high quality.

After this, you'll have to move the turtle to the resource, and mine said resource until the quantity matches the amount requested.
anthonysilby #4
Posted 19 September 2015 - 11:40 AM
What do you mean by block is defined?
valithor #5
Posted 20 September 2015 - 12:15 AM
Didn't even see the video… lol

What do you mean by block is defined?

Block is a table, which, in the code provided, is being referenced on lines 4,6,8,10, etc… But is not defined in the code provided.
anthonysilby #6
Posted 20 September 2015 - 01:07 AM
How do I make a table? :unsure:/>
anthonysilby #7
Posted 20 September 2015 - 01:20 AM
array?
KingofGamesYami #8
Posted 20 September 2015 - 01:24 AM
In your code you have:


Block[ 1 ] = io.read()

…which will error with "attempt to index ? (a nil value)" unless you make a table.


local Block = {} --#creates an empty table named "Block"

Tables can be treated as arrays. In fact, the table API only includes functions helpful when it is being used as an array.
Edited on 19 September 2015 - 11:25 PM
anthonysilby #9
Posted 20 September 2015 - 02:54 AM
I've edited my code a bit


local Block = {"Pumpkins", "Dirt", "Hay", "Snow", "Sand", "Gravel", "Red Sand", "Soul Sand", "Melon"}
io.write("What is your name?")
name = io.read()
io.write("How many Pumpkins " ..name.. "?")
Block[1] = io.read()
io.write("How many Dirt Blocks " ..name.. "?")
Block[2] = io.read()
io.write("How many Hay Bales " ..name.. "?")
Block[3] = io.read()
io.write("How many Snow Blocks " ..name.. "?")
Block[4] = io.read()
io.write("How many Sand " ..name.. "?")
Block[5] = io.read()
io.write("How many Gravel " ..name.. "?")
Block[6] = io.read()
io.write("How many Red Sand " ..name.. "?")
Block[7] = io.read()
io.write("How many Soul Sand " ..name.. "?")
Block[8] = io.read()
io.write("How many Melons " ..name.. "?")
Block[9] = io.read()

will this work ok??
do I have to write local? (line 1)
what do you reccomend I do next?
what I need to do to pass:
KingofGamesYami #10
Posted 20 September 2015 - 03:05 AM
Hmm… you could make it much smaller.

You don't have to write local, but global variables are bad practice and should be avoided whenever possible. Be sure to read up on scope, as localizing variables in the wrong places (eg. inside an if statement) will result in it not being available outside of that block.



local Block = {"Pumpkins", "Dirt", "Hay", "Snow", "Sand", "Gravel", "Red Sand", "Soul Sand", "Melon"}
write("What is your name?") --#no need to use io
local name = read() --#localizing your variables is good practice!  Read up on scope :)/>
for i = 1, #Block do --#loop makes everything so much smaller
  write("How many " .. Block[ i ] .. " " ..name.. "?") --#you've already done this, I don't think I need to explain
  Block[i] = read()
end

That will get the amount they want of each thing. Next you'd need to write some code to find said items. If you have trouble on a particular task, check the wiki, and if you still have trouble, you may ask here.
anthonysilby #11
Posted 20 September 2015 - 10:22 AM
does the for loop you've made make it do

io.write("How many Pumpkins " ..name.. "?")
Block[1] = io.read()

for all the blocks?
KingofGamesYami #12
Posted 20 September 2015 - 01:51 PM
No, try running it. The first time it will be Pumpkins & Block[ 1 ]. The second time it'll be Dirth & Block[ 2 ]. etc.
anthonysilby #13
Posted 21 September 2015 - 07:08 AM
Thanks, I just downloaded Minecraft/Forge/ComputerCraft today and the code works, so far. I'm going to work on making it get the stuff now
Edited on 21 September 2015 - 05:09 AM
anthonysilby #14
Posted 21 September 2015 - 11:17 AM
any idea how I'd make it so the turtle recalls the value inputted for the block and then digs those blocks like shown in the video?
I was thinking of doing a function with a for loop that digs than goes up and digs and goes up etc. until the amount I've told it to collect has been reached.
but I dont know how I'd write it
KingofGamesYami #15
Posted 21 September 2015 - 01:20 PM
I'd use the break command - it exits a loop early,


while true do
  local input = read()
  if input == "exit" then
    break
  end
end

You'd also want to use turtle.dig and turtle.up. Honestly, I think the way they did it in the video was somewhat wasteful of the turtle's fuel, but that's not a big deal.
anthonysilby #16
Posted 21 September 2015 - 08:38 PM
this is my first time seeing a break command :o/>
anthonysilby #17
Posted 21 September 2015 - 09:36 PM
I asked the turtle to get 5 pumpkins, used this code:


function digDig()
for i = 1 , Block[1] do
turtle.dig()
turtle.up()
end
end
digDig()

and it got 5 pumpkins :D/>
anthonysilby #18
Posted 21 September 2015 - 10:58 PM

local Block = {"Jack o'Lanterns", "Pumpkins", "Dirt", "Hay", "Snow", "Sand", "Gravel", "Red Sand", "Soul Sand", "Melons"}
io.write("What is your name?")
local name = io.read()
for i = 1, #Block do
write("How many " ..Block[ i ].. " " ..name.. "?")
Block[i] = read()
end

function toWall()
while turtle.detect() == false do
turtle.forward()
end
end

function digThenDown()
for i = 1 , Block[1] do
turtle.dig()
turtle.up()
end
while turtle.detectDown() == false do
turtle.down()
end
end

function selectAndCompare()
turtle.select(1)
if turtle.compare() == true then
digThenDown()
else
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end
end

toWall()
turtle.turnRight()
toWall()
selectAndCompare()

I've made my code get the right amount of blocks, how would I make it change slot numbers after its collected the blocks and then move on to the next block in slot2?
https://www.youtube.com/watch?v=SaCHnRi6A8g&feature=youtu.be
anthonysilby #19
Posted 22 September 2015 - 08:28 PM
anyone know how I'd do that?
KingofGamesYami #20
Posted 22 September 2015 - 09:51 PM
To change the selected slot, use

turtle.select( slotNumber )

Where slotNumber is an integer between 1 and 16.

I'm not going to spoonfeed you the exact code, as this looks as if it is a homework assignment…
Edited on 22 September 2015 - 07:52 PM
anthonysilby #21
Posted 22 September 2015 - 10:52 PM
I already knew about the turtle.select() command, I was wondering if there was some way I could make a loop that automatically goes to the next slot and then digs the amount of blocks I make it.
Or will I have to repeat all those lines of code over and over?
TYKUHN2 #22
Posted 22 September 2015 - 10:55 PM
Since no one else seemed to suggest it I would recommend you go here.


Reminds me of my web design class I am taking.


Worst mistake of my life. Hope your class is much… less annoyingly easy.
KingofGamesYami #23
Posted 22 September 2015 - 11:10 PM
You seem to be looking for a for loop.

If you ever write code repeatedly, chances are you can use a loop.
Edited on 22 September 2015 - 09:10 PM
anthonysilby #24
Posted 24 September 2015 - 08:40 AM
I've added a bit to my code to make it dig the second block type, Pumpkins, and the code works by digging the Jack o'Lanterns and when it has finished that it goes looking for the next block type, Pumpkins but when it gets to the Pumpkins it just stops, Does anyone know why this would be happening with my code?


local Block = {"Jack o'Lanterns", "Pumpkins", "Dirt", "Hay", "Snow", "Sand", "Gravel", "Red Sand", "Soul Sand", "Melons"}
io.write("What is your name?")
local name = io.read()
for i = 1, #Block do
write("How many " ..Block[ i ].. " " ..name.. "?")
Block[i] = read()
end

function toWall()
while turtle.detect() == false do
turtle.forward()
end
end

function digThenDown()
for i = 1 , Block[1] do
turtle.dig()
turtle.up()
end
while turtle.detectDown() == false do
turtle.down()
end
end

function digThenDown2()
for i = 1 , Block[2] do
turtle.dig()
turtle.up()
end
while turtle.detectDown() == false do
turtle.down()
end
end

function selectAndCompare()
turtle.select(1)
if turtle.compare() == true then
digThenDown()
else
while turtle.compare() == false do
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end
end
end

function selectAndCompare2()
turtle.select(2)
if turtle.compare() == true then
digThenDown2()
else
while turtle.compare() == false do
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end
end
end

toWall()
turtle.turnRight()
toWall()
selectAndCompare()
selectAndCompare2()


besides the obvious thing of there being an error, what is the error?
Bomb Bloke #25
Posted 24 September 2015 - 11:03 AM
When selectAndCompare2() is called, if a match for the turtle's second inventory slot (presumably containing pumpkins) is detected in front of the turtle immediately, then it'll attempt to dig them up. If not, it'll attempt to move along until it finds some, but once it has you don't issue any other orders to dig them.

Remember that some blocks can fall, meaning the turtle may have to dig multiple times from the same position to eg collect a column of sand. I also get the impression you may've been intending to use turtle.digUp() / turtle.compareUp()?

You can rig your functions to accept parameters, allowing you to re-use the same block of code for slightly different tasks. Eg, this:

function selectAndCompare()
turtle.select(1)
if turtle.compare() == true then
digThenDown()
else
while turtle.compare() == false do
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end
end
end

function selectAndCompare2()
turtle.select(2)
if turtle.compare() == true then
digThenDown2()
else
while turtle.compare() == false do
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end
end
end

selectAndCompare()
selectAndCompare2()

… can be reduced to:

function selectAndCompare(slot)
	turtle.select(slot)
	if turtle.compare() == true then
		digThenDown()
	else
		while turtle.compare() == false do
			turtle.turnRight()
			turtle.forward()
			turtle.turnLeft()
		end
	end
end

selectAndCompare(1)
selectAndCompare(2)