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

Can Someone Help Me In My Program

Started by johnneijzen, 17 September 2013 - 05:17 AM
johnneijzen #1
Posted 17 September 2013 - 07:17 AM
Okey I want thanks everybody how helped me and giving them Credits right
here http://www.computercraft.info/forums2/index.php?/topic/15292-john-strip-mining-turtle/. This Topic can be close now.

Source code
http://pastebin.com/emb9EnUC
campicus #2
Posted 17 September 2013 - 09:45 AM
Format your code please, with indents. That way you can easily debug (and so can we).

You have called a function while defining that function
You have also called a function before defining it


--Start
print("Hi There Welcome to Mining Turtle Program")
print("How Far Will Turtle Go")
local U = tonumber(read())
local TF = U --two variables that are exactly the same? just make this one variable
local TB = U
forwardM() -- calling your function here before defining it

--Mining
function forwardM()
  turtle.dig()
  turtle.forward()
  while true do
	TF = TF - 1
	turtle.digUp()
  end
  if TF == 0 then
	backA()
	else
	forwardM() --you are calling your function within your definition of that function
  end
end

--Warm Up For Back Program
function backA()
  turtle.turnLeft()
  turtle.turnLeft()
  back()
end

--Back Program
function back()
  turtle.forward()
  while true do
	TB = TB - 1
  end
  if TB == 0 then
	print("Turtle done")
  else
	back()
  end
end
johnneijzen #3
Posted 17 September 2013 - 10:05 AM
okey next time i make my code lot cleaner sorry i this first time doing this stuff so i mix up lot stuff.and make other code so debugging is lot easy.
LBPHacker #4
Posted 17 September 2013 - 10:07 AM
Calling it before declaring it? Yeah, that's wrong.
Calling it in itself? Possible if it's declared like
local function x(...
and not like
local x = function(...
Beware of filling stacks though. And use locals.
campicus #5
Posted 17 September 2013 - 10:12 AM
Calling it before declaring it? Yeah, that's wrong.
Calling it in itself? Possible if it's declared like
local function x(...
and not like
local x = function(...
Beware of filling stacks though. And use locals.

Are you able to call functions before declaring them? e.g.,

myFunction()

function myFunction()
  print("Would this run?")
end
LBPHacker #6
Posted 17 September 2013 - 10:13 AM
Are you able to call functions before declaring them?
Calling it before declaring it? Yeah, that's wrong.
campicus #7
Posted 17 September 2013 - 10:22 AM
Are you able to call functions before declaring them?
Calling it before declaring it? Yeah, that's wrong.

Your pronoun use is confusing me lol Is my advice wrong or is calling before declaring wrong?
johnneijzen #8
Posted 17 September 2013 - 10:51 AM
okey i fix function but now i have probleem with user input.

http://pastebin.com/5DPjqTbC.
Cranium #9
Posted 17 September 2013 - 11:06 AM
Your pronoun use is confusing me lol Is my advice wrong or is calling before declaring wrong?
You cannot call a function before declaring it. It is incorrect usage to do so.
campicus #10
Posted 17 September 2013 - 09:30 PM
-snip-

Thank you :)/>

okey i fix function but now i have probleem with user input.

http://pastebin.com/5DPjqTbC.

try something like this:

local distance = 0
-- Start
print("Hi There Welcome to Mining Turtle Program")
print("How Far Will Turtle Go")
input = io.read()
distance = tonumber(input)
TF = distance
TB = distance
forwardM()

Also, this loop will run forever:


while true do  
  TF = TF - 1
  turtle.digUp()
end

A repeat loop would be better:

repeat
  TF = TF - 1
  turtle.digUp()
until TF == 0
johnneijzen #11
Posted 18 September 2013 - 08:11 AM
-snip-

Thank you :)/>

okey i fix function but now i have probleem with user input.

http://pastebin.com/5DPjqTbC.

try something like this:

local distance = 0
-- Start
print("Hi There Welcome to Mining Turtle Program")
print("How Far Will Turtle Go")
input = io.read()
distance = tonumber(input)
TF = distance
TB = distance
forwardM()

Also, this loop will run forever:


while true do  
  TF = TF - 1
  turtle.digUp()
end

A repeat loop would be better:

repeat
  TF = TF - 1
  turtle.digUp()
until TF == 0

I have follow your code and thank you i have it working. :D/> Thanks Lot.

http://pastebin.com/5DPjqTbC
johnneijzen #12
Posted 18 September 2013 - 08:21 AM
i have one question left how to make detection that turtle moved can someone show me code.

because i want: when turtle.forward() is true then do TF = TF - 1. so distance wont get messup
kreezxil #13
Posted 18 September 2013 - 09:31 PM
Lines 24-28 can be reduced to
backA()
because TF will equal 0 after the repeat loop is finished and because of that the
forwardM()
will never get executed. If you want it to get executed you will have to rework the logic of the forwardM() function.

As far as knowing for sure if you turtle has moved, you should read http://computercraft...ki/Turtle_(API) so that your eyes may be opened wide. But yes you can detect if the turtle moved forward. Replace lines 20-21 with this code

if turtle.forward() then
	TF = TF -1
end