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

Hand to Hand programming (Rednet problems)

Started by gheotic, 21 May 2013 - 05:22 AM
gheotic #1
Posted 21 May 2013 - 07:22 AM
Im trying to figure out away to make a turtle that are doing a loop like:


function Attack()

while true do
local scrap, message = rednet.receive()

if message == sleep then
sleep(30)

else

turtle.attack

sleep(0.5)

ennd
end

And then have a computer that can send a signal that make it stop / start but my problem is that it can't attack while it receives a signal

so if i send it a signal there is a small chance that it wont see the signal
because it only receive 1/2 of the time, but i still want the turtle to attack effectivly.

I know that you can make a hand to hand method but then the computer also have to receive the signal.

does anyone know a clever way to do this?

Any help would be appreciated :)/>

Gheotic
panicmore #2
Posted 21 May 2013 - 08:10 AM
get it to check for a signal telling it to attack everytime it attacks and therefore when it stops receiving this it will stop the program. You can write the command to file so it carryies on through world restart
gheotic #3
Posted 21 May 2013 - 08:45 AM
When you say signal do you mean redstone signal?
panicmore #4
Posted 21 May 2013 - 09:52 AM
rednet
gheotic #5
Posted 21 May 2013 - 01:05 PM
but the problem is that if i send a signal it's there is a chance that its about to attack instead of receive so it wont get the message…
diegodan1893 #6
Posted 21 May 2013 - 01:14 PM
Don't use rednet.receive() or sleep() functions. Use events and timers to receive and wait at the same time.
gheotic #7
Posted 21 May 2013 - 02:15 PM
Hmm can you give an example?
diegodan1893 #8
Posted 21 May 2013 - 05:21 PM
Your problem, if I understood it well, is that when you call sleep function you can't receive rednet messages, but you can wait a certain time without pausing the program if you use events.


while true do
   local timer
   local bSleep = false
   event, p1, p2, p3 = os.pullEvent()
   if event == "rednet_message" then
	  if p2 == "sleep" then
		 timer = os.startTimer(30)
		 bSleep = true
	  elseif p2 == "attack" and not bSleep then
			--If the message is "attack" and the turtle is not sleeping (this is not what you wanted but it's only an example)'
			turtle.attack()
	  end
   elseif event == "timer" and p1 == timer then
	  bSleep = false
   end		
end
gheotic #9
Posted 22 May 2013 - 02:30 AM
It still dosnt loop attack :/
i dont know if i have set it up right



while true do
local timer
local bSleep = false
event, p1, p2, p3 = os.pullEvent()
if event == "rednet_message" then
if p2 == "sleep" then
timer = os.startTimer(30)
bSleep = true
elseif p2 == "attack" and not bSleep then
--If the message is "attack" and the turtle is not sleeping (this is not what you wanted but it's only an example)'
turtle.attack()
end
elseif event == "timer" and p1 == timer then
bSleep = false
end          
end

and then send a message "attack"
then it just attack
and if sleep it just sleep
dosnt look like the world
diegodan1893 #10
Posted 23 May 2013 - 01:52 PM
Please, explain better what do you want to do and what is the problem
gheotic #11
Posted 24 May 2013 - 03:14 AM
I have a long line of turtles that at the moment just run a loop
like this:
while true do
turtle.attack()
end

I want to make a main computer that sends a rednet signal that tells them all to stop

but if i do something like this

while true do
local id, message = rednet.receiv(0.5)
if message == "stop" then
sleep(30) – sleep 30 seconds before it starts again
else
turtle.attack()

end

if i did something like this, it will hit slower and would not keep up the system and there is a chance that it wont receives the message that the main computer :/

so i need a way to make it keep on the loop, and kinda just check if the Meleeturtle has got an signal.
unobtanium #12
Posted 24 May 2013 - 06:37 AM
Now it is getting interesting :P/>
That's the same i need as well. Allready did some code about it here, but it didnt turned out as it should.
diegodan1893 #13
Posted 25 May 2013 - 01:55 PM
Well that can be done with events. rednet.receive will pause the program but you can just listen for any events (including rednet messages or timers) and you will be able to receive messages without stop attacking.

EDIT: Here is the code (untested)

while true do
   local timer
   local bSleep = false
   event, p1, p2, p3 = os.pullEvent()
   if event == "rednet_message" then
	  if p2 == "sleep" then
		 timer = os.startTimer(30)
		 bSleep = true
	  end
   elseif event == "timer" and p1 == timer then
	  bSleep = false
   end
   if not bSleep then
	  turtle.attack()
	  os.startTimer(0.1)
   end		
end

I haven't tested it but I think it will work.