Jump to content




Turtle.attack() Rednet Toggle Possible?


  • You cannot reply to this topic
27 replies to this topic

#1 Alcheya

  • Members
  • 24 posts

Posted 30 September 2013 - 01:22 AM

I can get wireless turtles to start attacking, but I can't get them to stop once it's started.. I can also just get wait for the signal to attack once, but then they go back to waiting.. but is there a way to "break" the attack loop or even toggle on and off? I did it before with a redstone check, but with the setup I am using now, putting a rednet wire underneath wouldn't be feasible and I'd really like to keep the visible wiring to a minimum.

I just need them to attack, nothing else, but I'd like the option to turn it all off with the press of a button so that it can be in "grinder-only" to collect mob essence at will. Under the turtles are hoppers that keep them empty.

Posted Image



Any help appreciated, thanks.

#2 plazter

  • Members
  • 134 posts

Posted 30 September 2013 - 02:52 AM

You sure can, i will get back to you once im at a computer:)

#3 plazter

  • Members
  • 134 posts

Posted 30 September 2013 - 02:54 AM

Can you post your code? Ill just take a look asap:)

#4 Alcheya

  • Members
  • 24 posts

Posted 30 September 2013 - 02:59 AM

The code for the turtle attack is nothing special, just opens up the modem, turtle waits for "toggleTurtle" then either does "turtle.attack()" and waits for the command again or does "turtle.attack()" in a loop if I add while true do to the beginning. The button on the monitor just sends "toggleTurtle". The only way to stop it from going on is to reset the turtle.

#5 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 30 September 2013 - 03:22 AM

Use parallel.waitForAny.

#6 plazter

  • Members
  • 134 posts

Posted 30 September 2013 - 04:02 AM

 
Rednet.open("right")
While true do
Local id, msg = rednet.receive()

 If id then
  If msg == atk then
    Turtle.attack()
ElseIf not msg == atk then
  Sleep(0.8)
End
 sleep(1)
End


Not sure it will work but im on my phone so im not able to test first:)


#7 Alcheya

  • Members
  • 24 posts

Posted 30 September 2013 - 04:38 AM

 immibis, on 30 September 2013 - 03:22 AM, said:

Use parallel.waitForAny.

I looked into this and it seems like the thing I should be looking at, but i have no idea how it works. I'm playing around with it.

 plazter, on 30 September 2013 - 04:02 AM, said:

Rednet.open("right")
While true do
Local id, msg = rednet.receive()

If id then
  If msg == atk then
	Turtle.attack()
ElseIf not msg == atk then
  Sleep(0.8)
End
sleep(1)
End



Not sure it will work but im on my phone so im not able to test first:)

This is similar to what i have going now. With this I'll have to either keep sending "atk" or add a "while true do" and end up with a never-ending loop and no way to break it but going to the turtle and resetting it.

#8 plazter

  • Members
  • 134 posts

Posted 30 September 2013 - 05:07 AM

Local latch = true

Function atk()
Turtle.attack()
End

While true do

If msg then

Latch = true
Atk()
Elseif latch = false
Sleep(1)
End

Meaby?:b anyway, can provide a link on my pastebin with a few diffrents code that you could use some parts off i think :)


#9 Alcheya

  • Members
  • 24 posts

Posted 30 September 2013 - 05:16 AM

 plazter, on 30 September 2013 - 05:07 AM, said:

Local latch = true

Function atk()
Turtle.attack()
End

While true do

If msg then

Latch = true
Atk()
Elseif latch = false
Sleep(1)
End

Meaby?:b anyway, can provide a link on my pastebin with a few diffrents code that you could use some parts off i think :)

That would be fine. I do better when I have examples to understand how things work rather than looking at random parts. Thank you.

#10 plazter

  • Members
  • 134 posts

Posted 30 September 2013 - 05:23 AM

Www.pastebin.com/u/plazter here u go :)

#11 Alcheya

  • Members
  • 24 posts

Posted 30 September 2013 - 06:30 AM

 plazter, on 30 September 2013 - 05:23 AM, said:

Www.pastebin.com/u/plazter here u go :)

Thanks, I was able to figure out half of my problem with that.. Now the other problem remains where the turtle will not respond to the messages while already in the loop. :P


I tried using parallel, but it seems to just turn off the program with no error messages.

#12 plazter

  • Members
  • 134 posts

Posted 30 September 2013 - 06:32 AM

Now could be a good time to pass on your code :b

#13 Alcheya

  • Members
  • 24 posts

Posted 30 September 2013 - 06:50 AM

 immibis, on 30 September 2013 - 03:22 AM, said:

Use parallel.waitForAny.

With this it ends the program when anything is recieved. I want it to continously loop turtle.attack when "attack" is received until it receives "dismissed" then loop back to the beginning of the program to just listening.

#14 Alcheya

  • Members
  • 24 posts

Posted 30 September 2013 - 07:00 AM

 plazter, on 30 September 2013 - 06:32 AM, said:

Now could be a good time to pass on your code :b

rednet.open("right")


function f1()
senderID, msg, dis = rednet.receive()
if msg == "attack" then
  while true do
   turtle.attack()
  end
end
end

function f2()
senderID, msg, dis = rednet.receive()
if msg == "dismissed" then
   sleep(.5)
   shell.run("fight")
  end
end

parallel.waitForAny(f1, f2)

I changed it because I thought after reading around that this might work.. regardless, still the same issue. If take out the functions and just have it listen for "attack" and "dismissed" once it gets the signal to attack, it will ignore anything else.

#15 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 30 September 2013 - 07:04 AM

I would make 2 functions with while true do loops inside and one local toggle

1) if toggle=true then attack else sleep
2) listen and set toggle depending on if "attack" or "dismiss"

Run both on same time with parallel.waitForAny. or parallel.waitForAll. - no matter cause both functions have while true do loops inside with no breaks - never end

No need to run program again cause it stores if it needs to attack or not in toggle -
2nd just listens and sets toggle depending on orders.
1st just attacks if toggled else sleeps.

#16 plazter

  • Members
  • 134 posts

Posted 30 September 2013 - 07:10 AM

Rednet.open("right")

Function rednet()
Id, sg = rednet.receive()
If msg =="atk" then 
Turtle.attack()
End

Repeat rednet() 

Untill local id , sg = rednet.receive() 
If msg == "stop" then
Sleep(1)
End


If this doesnt work i will figure it out at Home:)


#17 LordIkol

  • Members
  • 197 posts
  • LocationSwitzerland

Posted 30 September 2013 - 07:16 AM

Untested but should give you a clue about how it can work

rednet.open("right")
local running = true

local function atk()
while true do
if running == true then
turtle.attack()
print("attacking")
end
sleep(0.8)
end
end

local function switchit()
event, id, text = os.pullEvent()
if text == "on" then
running = true
elseif text =="off" then
running = false
print("Stop attack")
end
end

while true do
parallel.waitForAny(switchit,atk)
end
end

edit: corrected an error now it should work ;D

Edited by LordIkol, 30 September 2013 - 07:25 AM.


#18 Alcheya

  • Members
  • 24 posts

Posted 30 September 2013 - 07:55 AM

 LordIkol, on 30 September 2013 - 07:16 AM, said:

Untested but should give you a clue about how it can work

rednet.open("right")
local running = true

local function atk()
while true do
if running == true then
turtle.attack()
print("attacking")
end
sleep(0.8)
end
end

local function switchit()
event, id, text = os.pullEvent()
if text == "on" then
running = true
elseif text =="off" then
running = false
print("Stop attack")
end
end

while true do
parallel.waitForAny(switchit,atk)
end
end

edit: corrected an error now it should work ;D

It's working, but making me lag horribly, but it might be because I did the first one (pre-correction) and it crashed me horribly.

2013-09-30 08:46:10 [INFO] [STDOUT] ComputerCraft: Error running task.
Spoiler

Going to give it a go after I restart.

#19 jay5476

  • Members
  • 289 posts

Posted 30 September 2013 - 08:19 AM

this would be simpler
atk = false
rednet.open(sidehere)
function listen()
  _,mes,_ = rednet.receive()
  if mes == "off" then
    atk = false
  elseif mes == "on" then
    atk = true
  end
end

function attack()
  while atk do
    turtle.attack()
    sleep(0)
  end
end

parallel.waitForAny(attack,listen)


#20 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 30 September 2013 - 08:32 AM

 jay5476, on 30 September 2013 - 08:19 AM, said:

this would be simpler
atk = false
rednet.open(sidehere)
function listen()
  _,mes,_ = rednet.receive()
  if mes == "off" then
	atk = false
  elseif mes == "on" then
	atk = true
  end
end

function attack()
  while atk do
	turtle.attack()
	sleep(0)
  end
end

parallel.waitForAny(attack,listen)

But in this code function attack() would end if atk is false - and it would end parralel - and program would end :P
same with listen() - ends after one message ending whole program.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users