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

Movenment/attack Turtle

Started by TeknoPT, 09 April 2013 - 03:48 AM
TeknoPT #1
Posted 09 April 2013 - 05:48 AM
Hey guys can you help me to do a code, when my turtle.attack(), i want to turtle.turnLeft().

But my turtle didn't do anything

while
turtle.attack()
turtle.turnLeft()
end,
function()
turtle.attack()
turtle.turnLeft()
end,
function()
turtle.attack()
turtle.turnLeft()
end,
function()
turtle.attack()
turtle.turnLeft()
end

This is my code, i new at computer craft
theoriginalbit #2
Posted 09 April 2013 - 06:13 AM
hello and welcome. for future reference I suggest you use [code][/code] tags around your code to make it more readable.

now as for your problem, I'm not too sure exactly what you want this to do, so here is a few questions to help me understand exactly, is it being used in a larger program, or on its own as like a 'mob farm' script? does that mean it is meant to attack and then turn and stop, or continue to attack once turned? if it is meant to keep attacking, does it ever have to stop?

now as an immediate fix. while loops should follow the following format.

while <condition> do
  -- your code in here will run while the condition is true
end
where <condition> should be a boolean or a function call that returns a boolean (such as most turtle functions, check the wiki for more info). Note: A boolean is a variable that is true or false.
Izodn #3
Posted 09 April 2013 - 07:54 AM
I might be far off, but I think you can accomplish what you're trying to with:


function test()
	if turtle.attack() then
		turtle.turnLeft()
	end
end
test()
test()
test()

In this example, the function "test()" is called 3 times. The function it'self checks to see if the turtle CAN attack, and if/when it does turn left afterwords.

TheOriginalBIT has a perfect example of the syntax of a while statement.

As for future programs, I'd recommend searching online for Lua tutorials and using the search function on this forum.
SuicidalSTDz #4
Posted 09 April 2013 - 09:20 AM

function test()
	if turtle.attack() then
		turtle.turnLeft()
	end
end
test()
test()
test()
Ehem:

for i = 1,3 do
 test()
end

Don't be a Direderp and call functions repeatedly, use loops. They don't bite.
Izodn #5
Posted 09 April 2013 - 09:24 AM

function test()
	if turtle.attack() then
		turtle.turnLeft()
	end
end
test()
test()
test()
Ehem:

for i = 1,3 do
test()
end

Don't be a Direderp and call functions repeatedly, use loops. They don't bite.
Good point. I usually don't use for functions, I can't seem to remember the syntax.
SuicidalSTDz #6
Posted 09 April 2013 - 09:28 AM
Good point. I usually don't use for functions, I can't seem to remember the syntax.
local table = {"lol","lol","lol"}
for k,v in pairs(table) do
print(v)
end

OR

local table = {"lol","lol","lol"}
for i = 1, #table do
print(table[i])
end

OR

local table = {"lol","lol","lol"}
for k,v in ipairs(table) do
print(v)
end

Read about for loops on the PIL or Lua Reference Manual. Great places.
VADemon #7
Posted 09 April 2013 - 12:09 PM
local table = {"lol","lol","lol"}
for k,v in ipairs(table) do
print(v)
end

Read about for loops on the PIL or Lua Reference Manual. Great places.
ipairs is deprecated in Lua 5.2. Stop using/recommending it ;)/>

1) Only turn left if turtle attacked something:
while true do
if turtle.attack() then turtle.turnLeft() end
end

2) Spin around and attack:
while true do
turtle.attack()
turtle.turnLeft()
--sleep(0.5)
end

3) Turn left if no target was found (to attack it):
while true do
if not turtle.attack() then turtle.turnLeft() end
	--sleep(0.5)
end
I prefer the last one. You also may want to add a short sleep between actions, just remove the "–"
SuicidalSTDz #8
Posted 09 April 2013 - 12:17 PM
ipairs is deprecated in Lua 5.2. Stop using/recommending it ;)/>
CC does not use Lua 5.2 , therefore I will bring forth the method as much as I damn please <_</> If CC used Lua 5.2 then I would not bring forth the method since it would be irrelevant :P/>
Dlcruz129 #9
Posted 09 April 2013 - 04:14 PM

function test()
	if turtle.attack() then
		turtle.turnLeft()
	end
end
test()
test()
test()
Ehem:

for i = 1,3 do
 test()
end

Don't be a Direderp and call functions repeatedly, use loops. They don't bite.

In this case I would call the function repeatedly, simply because the function name is short and I'm only doing it 3 times, so it's actually shorter.
SuicidalSTDz #10
Posted 09 April 2013 - 04:14 PM
No! I must use loops!
theoriginalbit #11
Posted 09 April 2013 - 05:01 PM
TheOriginalBIT has a perfect example of the syntax of a while statement.
OMG! Someone got the casing right and didn't even like copy it from my username! I LOVE YOU SIR/MADAM, which ever you are!

ipairs is deprecated in Lua 5.2. Stop using/recommending it ;)/>
It has been stated several times that CC will never upgrade to 5.2 as it will break to many things, so we will be 5.1 forever. however I do agree with your statement, don't use ipairs, there was a big argument over it a few months ago, and we ended up agreeing that there is more overhead using an iterator (generic for loop, ipairs, pairs), over using a sequential for loop (numeric for)
SuicidalSTDz #12
Posted 09 April 2013 - 05:45 PM
there was a big argument over it a few months ago, and we ended up agreeing that there is more overhead using an iterator (generic for loop, ipairs, pairs), over using a sequential for loop (numeric for)
Hmm, really.. I must've missed this 'debate' :P/>