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

Attack Turtle Error

Started by iJameStm, 17 January 2013 - 03:31 PM
iJameStm #1
Posted 17 January 2013 - 04:31 PM
Hi guys, I'm a beginner in turtle programming, and I want to make a guard turtle. Here is the program

————————-
for i = 1, 100 do
turtle.detect()
if true do
turtle.attack()
end
————————-

I get the error bios:338: [string "guard"]:3: 'then' expected

Also, am I missing something? like do I need to add anything to this?


Thanks
theoriginalbit #2
Posted 17 January 2013 - 04:39 PM
firstly it would be done like this

while true do
  if turtle.detect() then
	turtle.attack()
  end
end
however as stated on the wiki ( http://computercraft...tle=Turtle_(API) ) the detect function only detects blocks not mobs… so a solution would have to be made like the following:

1, we can just have it spam attack

while true do
  turtle.attack()
end

2, we can have it try to move, if it cant and it cant see a block there and it has fuel its a mob (however this also means your going to have to refuel the turtle every now and again )

while true do
   -- turn around
   turtle.turnLeft()
   turtle.turnLeft()
   while not turtle.forward() do
	 if turtle.detect() then
	   print("Its ok, its just a block!")
	   break -- exit this 'move forward' loop
	 elseif turtle.getFuelLevel() == 0 do
	   print("Oh it seems I'm out of fuel")
	   print("I will wait here for fuel")
	   while turtle.getFuelLevel() < 100 do
		 for i = 1, 16 do -- look in all my slots
		   turtle.select( i )
		   turtle.refuel()
		 end
	   end
	 else
	   turtle.attack()
	 end
   end
end
iJameStm #3
Posted 17 January 2013 - 04:45 PM
firstly it would be done like this

while true do
  if turtle.detect() then
	turtle.attack()
  end
end
however as stated on the wiki ( http://computercraft...tle=Turtle_(API) ) the detect function only detects blocks not mobs… so a solution would have to be made like the following:

1, we can just have it spam attack

while true do
  turtle.attack()
end

2, we can have it try to move, if it cant and it cant see a block there and it has fuel its a mob (however this also means your going to have to refuel the turtle every now and again )

while true do
   -- turn around
   turtle.turnLeft()
   turtle.turnLeft()
   while not turtle.forward() do
	 if turtle.detect() then
	   print("Its ok, its just a block!")
	   break -- exit this 'move forward' loop
	 elseif turtle.getFuelLevel() == 0 do
	   print("Oh it seems I'm out of fuel")
	   print("I will wait here for fuel")
	   while turtle.getFuelLevel() < 100 do
		 for i = 1, 16 do -- look in all my slots
		   turtle.select( i )
		   turtle.refuel()
		 end
	   end
	 else
	   turtle.attack()
	 end
   end
end
Thanks, me and my friend are applying this and seeing if it will work.
I'm sure this was a simple task, but I need some experience. Were did you learn this
stuff anyway?
theoriginalbit #4
Posted 17 January 2013 - 04:50 PM
Were did you learn this stuff anyway?
Really most of my general programming knowledge is from University… however my Lua specific knowledge was from reading this site http://www.lua.org/pil/1.html ( keep clicking next, theres more than one page) and for CC knowledge here http://computercraft...e=Category:APIs


EDIT: There will be some issues with the last one when the server shuts down or the chunk is unloaded as CC does not have persistence yet (meaning the computer/turtle does not remember what it was doing and where it was up to), so if you are using this one, and it becomes a regular problem look into Lua I/O Model to read and write to file what the turtle was doing… :)/>
iJameStm #5
Posted 17 January 2013 - 04:59 PM
Ok so I did method 2, and get the error


bios:338: [string "guard"]:9: 'then' expected

what now?

OK nvm It's working instead of


elseif turtle.getFuelLevel() == 0 do
it's

elseif turtle.getFuelLevel() == 0 then
theoriginalbit #6
Posted 17 January 2013 - 05:03 PM
oh oops sorry I had a typo on line 9… thats what I get for typing quick and in this reply box…

change it from this

elseif turtle.getFuelLevel() == 0 do
to this

elseif turtle.getFuelLevel() == 0 then

again sorry


EDIT: I ninja'd :P/>
Edited on 17 January 2013 - 04:04 PM
jewelshisen #7
Posted 17 January 2013 - 05:03 PM
Ok so I did method 2, and get the error


bios:338: [string "guard"]:9: 'then' expected

what now?

Replace do with then
iJameStm #8
Posted 17 January 2013 - 05:29 PM
Sorry to drag it on, but how would I go about applying turtle.suck ?
theoriginalbit #9
Posted 17 January 2013 - 05:35 PM
when does it need to suck? like when its attacked? if so, when it kills the mob it will pick up its items auto… otherwise you could add this just incase


turtle.attack()
turtle.suck()
Edited on 17 January 2013 - 04:45 PM
iJameStm #10
Posted 17 January 2013 - 06:02 PM
Yeah I'm using the turtle to make a blazerod farm, with turtles auto killing blazes. And they weren't picking up the blazerods..
theoriginalbit #11
Posted 17 January 2013 - 06:09 PM
they should have been… just have them suck after attacking though and you should be good… :)/>
iJameStm #12
Posted 18 January 2013 - 04:06 PM
Sorry for dragging it on, but how would I refuel it without going INTO the room (I hate blazes :P/>) and could someone give me a link on how to send commands from
my computer to a turtle? Both of them have been "rednet.open"-ed but how do I send commands so I can edit the program safely? (the program is labeled 'guard')
theoriginalbit #13
Posted 18 January 2013 - 04:18 PM
well blaze rods are a fuel http://computercraft...uel#Fuel_Values
so you could have something like this added somewhere into the code, I would suggest if the turtle cant move forward

if turtle.getFuelLevel() < 10 then
  turtle.select( 1 )
  turtle.refuel( 8 ) -- uses 8 blaze rods to refuel resulting in a fuel level of 960
end

As for sending commands there are several ways people do it… I would suggest reading through "Programs" and "Ask a Pro" there are a few examples in these from memory… If you still cant find anything useful just ask and I can help run through it :)/>
Edited on 18 January 2013 - 03:21 PM
iJameStm #14
Posted 18 January 2013 - 06:59 PM
well blaze rods are a fuel http://computercraft...uel#Fuel_Values
so you could have something like this added somewhere into the code, I would suggest if the turtle cant move forward

if turtle.getFuelLevel() < 10 then
  turtle.select( 1 )
  turtle.refuel( 8 ) -- uses 8 blaze rods to refuel resulting in a fuel level of 960
end

As for sending commands there are several ways people do it… I would suggest reading through "Programs" and "Ask a Pro" there are a few examples in these from memory… If you still cant find anything useful just ask and I can help run through it :)/>
Yeah I can't find anything :/
theoriginalbit #15
Posted 18 January 2013 - 07:10 PM
ok so you want to be able to update your turtle with a new program even while its running? or just stop it, run a program, then continue it?

EDIT: Also is this the same program as before? or are we dealing with different existing code?
iJameStm #16
Posted 18 January 2013 - 08:09 PM
ok so you want to be able to update your turtle with a new program even while its running? or just stop it, run a program, then continue it?

EDIT: Also is this the same program as before? or are we dealing with different existing code?
I want to be able to stop it to refuel, and maybe edit it from the other side of the glass
theoriginalbit #17
Posted 18 January 2013 - 08:43 PM
When you say from the other side of the glass do you mean from another computer? or just on the turtle itself?
iJameStm #18
Posted 19 January 2013 - 02:13 PM
When you say from the other side of the glass do you mean from another computer? or just on the turtle itself?

Lol. When I said behind the glass, I meant behind a glass wall I built so I can see what is happening. I got the turtles on the computer now, so thanks anyway :)/>