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

Works as a single script but not as a function ?

Started by Rawrzor, 03 August 2014 - 08:18 PM
Rawrzor #1
Posted 03 August 2014 - 10:18 PM
So after finally getting my planting turtle to plant the entire field. A friend hopped by to look at it and jumped infront of the turtle thus mixing up it's entire planned out route. very dissapointing to say the least xD.

Now i have been trying to make a small sort of detect system that keeps up forward movement. Tested it as a single script in which it worked excellent. If i stand infront of it it waits till it can move forward and if i put a block there it digs it away.



while turtle.forward() ~= true do
			 if turtle.detect() ~= true
					 if turtle.dig() ~= true then

							  print("bedrock in the way")

					 end

			 end

			if turtle.attack == true then

						   print("player or monster in the way")

		 end
end


Now when i implement this as a funtion into the code however like this it seems to completely ignore it. I have the feeling i'm overlooking something. Tried all day to find out way this happens without simply copy pasting codes (since i do want to understand what i'm doing atleast if it works :)/>. Could somebody explain why this is happening ?




local function forward()


		   while turtle.forward() ~= true do
							 if turtle.detect() == true then  
										  if turtle.dig() ~= true then

												  print("bedrock in the way")

										   end

							  end

							  if turtle.attack == true then

												  print("player or monster in the way")

							   end
			   end

end

----- ending local forward

local function refuel()

				 r = turtle.getFuelLevel()

								  if r < 60 then

											 turtle.select(1)
											 turtle.refuel(2)

								   end
end

----- ending local refuel

local function melonturtle()

		 refuel()

			   for i = 1,14,1 do

						forward()
						sleep(1)
						turtle.turnLeft()
						turtle.dig()


								 for i = 1,2,1 do

								 turtle.turnRight()

								  end

						turtle.dig()
						turtle.turnLeft()

			  end  

			 for i = 1,2,1 do

					   turtle.turnRight()

			  end

			  for i = 1,14,1 do

						   forward()

			   end

			   for i = 1,2,1 do

						 turtle.turnLeft()

				end
end


----- ending local melonturtle

rednet.open("right")

while true do

				term.clear()
				 term.setCursorPos(1,1)

				print("Waiting for commands from advanced pc!")

				 x,y = rednet.receive()

				 if x == 278 and y == "go" then

							melonturtle()


				  end		  
  end

Edited on 03 August 2014 - 10:26 PM
KingofGamesYami #2
Posted 04 August 2014 - 12:31 AM

local function forward()
  local moved = true
  while not turtle.forward() and moved do --#not inverts boolean variables, and you do not have to compare boolean in statements.  "and moved" makes sure this will terminate if bedrock is in the way.
    moved = false --#turtle has failed to move forward, set moved to false.
    while turtle.attack() or turtle.dig() do --#attacks &amp; digs until nothing is in the way.  If bedrock is in the way, it will stop attacking and digging.
	 moved = true --#if loop hits something, it will change moved to true.
    end
  end
  return moved
end
Edited on 03 August 2014 - 10:37 PM
ValorCat #3
Posted 04 August 2014 - 12:46 AM
At the moment I cannot test this on a turtle, however I notice that in forward() you reference the variable turtle.attack, rather than its result (you want turtle.attack()). Also, you might want to add a sleep(1) at the end of the while loop (still in forward()) to avoid a 'too long without yielding' error. Other than these, I can't see why forward() wouldn't run.

Also, you can replace:
while turtle.forward() ~= true do
with
while not turtle.forward() do
and you can completely remove
== true
.
KingofGamesYami #4
Posted 04 August 2014 - 12:50 AM
Also, you might want to add a sleep(1)
Not true. Every time turtle.forward() is called, it has the same effect. Most turtle functions yield internally.
Rawrzor #5
Posted 04 August 2014 - 09:41 AM
sorry for the late response the sandman had me yesterday at a certain point ^_^/>. Thanks for the reply. I'm having a bit of trouble understanding a part of your code though so i'm just gonne write it out how i think it reacts at the moment (i'm new to programming so bear with me please >.<)

local function forward() —- creating a new function that only runs in the local script
local moved = true —- setting moved locally to true
while not turtle.forward() and moved do —– aslong as turtle.forward() does not put out true and moved does put out true keep doing (loop1) as a side note could i do not moved aswell ?

moved = false —– setting moved to false as turtle.forward has put out false thus breaking loop1 allowing loop2 to start ?
while turtle.attack() or turtle.dig() do —— aslong as turtle.attack or turtledig gives off true (loop2)
moved = true —– set moved to true (so when it could attack or dig it picks up loop1 again ? )
end
end
return moved —- no idea why this is here i tried to read about this function as if yet it still eludes me what the return does and where it returns to or what it returns

end

Sorry if i might come across ignorant but up till now i have just being trying trial and error for learning lua and might have teached myself a few thing wrongly. Since i had the idea while acted like this.





While not turtle.forward() do			   [color=#ff0000] <----- aslong as turtle.forward is not true do						  when it's true end[/color]

  turtle.dig() or turtle.attack()		   [color=#ff0000]	<----- turtle.dig() or turtle.attack() command[/color]


end


So now it should run the while loop everytime it can not move forward. Attack dig and try to move again or not ?

editting over 9000
Edited on 04 August 2014 - 08:04 AM
KingofGamesYami #6
Posted 04 August 2014 - 03:03 PM

local function forward()
  local moved = true --#local variable declaration
  while not turtle.forward() and moved do
    moved = false  --#turtle has *not* moved, thus this should be false
    while turtle.attack() or turtle.dig() do
         moved = true  --#if blockage is cleared, it changes to true to allow another attempt
    end
    --#if the turtle did not attack or dig anything, it will not continue to attempt to move forward
  end
  return moved --#return "returns" a value.
end
--#example of how to use this function
if not forward() then --#it will return true unless it hits something unbreakable :)/>
  error( "Bedrock detected" )
end

A smaller version of the same thing, that is probably easier to understand

function forward()
  local moved = true
  while moved and not turlte.forward() do
    moved = turtle.dig() or turtle.attack() --#if either hits, moved will be true
  end
  return moved
end

The main difference? With 2 loops, the turtle will not attempt to move forward between attacking/digging.

Here's how the "or" operator works:

If the first statement is true, it will ignore the second ( since turtle.dig() and turtle.attack() return boolean (true/false), this is very nice)
if the first statement is false, it will attempt to get the second, meaning turtle.attack() will be run. If both statements are false, it will return false, else it will return true.
Rawrzor #7
Posted 04 August 2014 - 04:58 PM
Thanks allot fot the info i have been digging a bit further into all the statments etc named and i know here the knot was for me atleast. Found this:


while condition do
chunck
end

That means: since a condition is not true execute the chunck. Vice versa: if the condition isn't true anymore (false) than stop the loop.

But step by step:
  • 1. it will be checked if the condition is true
  • 2. if condition is true than execute the chunck
  • 2.1 on "end" it will be checked if the condition is true or not << thats where the knot in my brain failed. I figured while would run the script and do the checkup on that if he would run it again. Instead it checks at the end mark if it's still true or not that why you added the extra move variable.
  • 2.2 if the condition is still true the chunck will be executed again (back to 2.)
  • 2.3 if the condition is not true (false) than stop the loop
Again thanks for the help!
Edited on 04 August 2014 - 02:58 PM
TheOddByte #8
Posted 04 August 2014 - 08:23 PM
- Snip -
About that the statement will be checked at the end the one who posted it meant this

local loop = true
while loop do
    print( "foo" )
    sleep( 1 )
    loop = false
end -- it checks if loop is still equal to true here, if it isn't then it exits the loop and moves on the next piece of code

print( "bar" )