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

Mindcrack melee Turtle.

Started by fairdude29, 30 December 2013 - 11:34 AM
fairdude29 #1
Posted 30 December 2013 - 12:34 PM
Please help me , Pro's!
I got myself into a problem!
Here's the code:

while true do
while turtle.detectDown() == true do
turtle.attack()
while turtle.attack() == true do
print("Enemy engaged|Initializing alarm!")
rs.setOutput("back", true)
while turtle.attack() == false do
print("Disable alarm:")
local input = read("*")
if input == "yes" then
rs.setOutput("back", false)
shell.run("protect")
else
term.clear()
term.setCursorPos(1,1)
print("Terminal: #923 Password invalid!")
print("Try again the password:")
end
end
end
end
end

The problem is:
Whenever the melee hits anybody , he doesn't do the
while turtle.attack() == false do.
He just continues the , == true do , one.!
Can somebody help me with that?
CometWolf #2
Posted 30 December 2013 - 02:25 PM
See my comments

[/b]
[b]while true do
  while turtle.detectDown() == true do -- this will make the program loop while there is a block beneath the turtle, im assuming you think this is for entities and not blocks?
	turtle.attack() -- this
	while turtle.attack() == true do -- and this, is a seperate attack, if this one fails, the detectDown loop runs again
	  print("Enemy engaged|Initializing alarm!")
	  rs.setOutput("back", true)
	  while turtle.attack() == false do --this is also a seperate attack.
		print("Disable alarm:")
		local input = read("*")
		if input == "yes" then
		  rs.setOutput("back", false)
   	   shell.run("protect")
		else
		  term.clear()
		  term.setCursorPos(1,1)
		  print("Terminal: #923 Password invalid!")
		  print("Try again the password:")
		end
	  end
	end
  end
end

Basically everytime you write turtle.attack(), the turtle will attack again, even if it's while turtle.attack() == true do, it will attack everytime the function is called regardless. This means it won't return the same value every time.

The first loop runs while there's a block beneath, then when there's a sucessfull attack, he moves onto the next loop. Here he will attack again, if it's unsucessfull your password part will run, otherwise he will attempt the attack == true attack again, if that fails we're back to the detectDown loop. Same things gonna happen when the password input fails, it will attack again.

a better solution for this code would be as follows


while true do
  if turtle.attack() then -- if we're working with a TRUE or FALSE we don't need any equal signs or anything, it will accept anything not FALSE or nil
	local pass
	while pass ~= "yes" do
	  --password part here, just pass the result to the pass variable
	end
  end
end
Note that your turtle will stop attacking while it's waiting for user input regardless.
Edited on 30 December 2013 - 01:27 PM
gollark8 #3
Posted 30 December 2013 - 02:47 PM
You could also try this (on iPad can't test code)

while true do
  while turtle.detectDown() do 
        local isAttacking = turtle.attack()
        while isAttacking  do
          print("Enemy engaged|Initializing alarm!")
          rs.setOutput("back", true)
          while not isAttacking do
                print("Disable alarm:")
                local input = read("*")
                if input == "yes" then
                  rs.setOutput("back", false)
           shell.run("protect")
                else
                  term.clear()
                  term.setCursorPos(1,1)
                  print("Terminal: #923 Password invalid!")
                  print("Try again the password:")
                end
          end
        end
  end
end
Edited on 30 December 2013 - 01:47 PM
CometWolf #4
Posted 30 December 2013 - 02:54 PM
You could also try this (on iPad can't test code)
That would result in a too long without yeilding error, as the loop while sAttacking would just loop over again forever, never entering the while not isAttacking loop.
a quick fix would be the following


while true do
  while turtle.detectDown() do
		local isAttacking = turtle.attack()
		while isAttacking  do
		  print("Enemy engaged|Initializing alarm!")
		  rs.setOutput("back", true)
		  isAttacking = turtle.attack()
		  while not isAttacking do
				print("Disable alarm:")
				local input = read("*")
				if input == "yes" then
				  rs.setOutput("back", false)
		   shell.run("protect")
				else
				  term.clear()
				  term.setCursorPos(1,1)
				  print("Terminal: #923 Password invalid!")
				  print("Try again the password:")
				end
		  end
		end
  end
end

But then i guess we're pretty much back to where we started…
Bomb Bloke #5
Posted 30 December 2013 - 05:43 PM
Would it not be easier to just have one "while" loop, which uses an "if" statement to check if a player is under it, and another to see if the password can be entered correctly? Seems to me it should only attack if the password is entered incorrectly - I don't see the need for more then one loop throughout the entire script.

I'm not exactly sure what the desired behaviour is, however.