Jump to content




Rednet code question.


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

#21 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 22 September 2013 - 02:22 AM

So, you're essentially just doing this?

for i = 1, 2 do
  for j = 1, 25 do
    turtle.back()
  end
  turtle.turnRight()
  for j = 1, 17 do
    turtle.back()
  end
  turtle.turnRight()
end

None of the turtle functions you're using take any arguments, so those numbers you're passing them are just getting ignored.

#22 brattus123

  • Members
  • 17 posts

Posted 22 September 2013 - 02:37 AM

i tried using that code and all it did was travel in a stright line 25 blocks turn in a circle for a while faced the wrong way then went 25 more blocks in a stright line then stopped.

here's the code

Turtle:
for i = 1, 2 do
for j = 1, 25 do
  turtle.back ()
end
turtle.turnRight ()
for j = 1, 17 do
turtle.turnRight ()
end turtle.turnRight ()
end


#23 BigTwisty

  • Members
  • 106 posts

Posted 22 September 2013 - 07:41 AM

Whenever you have several places in your code that are basically the same, it's a good idea to use a function. Try this:
local function backTurn(length)
  for i = 1,length do
    turtle.back()
  end
  turtle.turnRight()
end

backTurn(25)
backTurn(12)
backTurn(25)
backTurn(12)

Looking at the last 4 lines it is now easier to (a.) see what the turtle is going to do and (b.) make it do something different if you want.

It should be noted that Lyqyd's code should do he same thing if it is copied in correctly.

#24 brattus123

  • Members
  • 17 posts

Posted 23 September 2013 - 12:05 AM

with this code which is attempting to combine the rednet code with the orbit code im getting an error saying ("eof") expected on line 10 and i don't know why and also would it be possible to make this code loop until the redstone signal is applied

Turtle:
rednet.open ("left")
while true do
local event, id, msg = os.pullEvent ("rednet_message")
if id == 18 then
if msg == "nograv" then
shell.run ("offorbit")
end
end
end
else
shell.run ("orbit")


#25 BigTwisty

  • Members
  • 106 posts

Posted 23 September 2013 - 07:20 AM

View Postbrattus123, on 23 September 2013 - 12:05 AM, said:

with this code which is attempting to combine the rednet code with the orbit code im getting an error saying ("eof") expected on line 10 and i don't know why and also would it be possible to make this code loop until the redstone signal is applied

Turtle:
rednet.open ("left")
while true do
local event, id, msg = os.pullEvent ("rednet_message")
if id == 18 then
if msg == "nograv" then
shell.run ("offorbit")
end
end
end
else
shell.run ("orbit")

Proper code indentation would make this syntax error easier to spot.

rednet.open ("left")
while true do
  local event, id, msg = os.pullEvent ("rednet_message")
  if id == 18 then
    if msg == "nograv" then
      shell.run ("offorbit")
    end
  end
end
else
  shell.run ("orbit")

The last 2 lines actually belong right after the other shell.run. Right now they are outside your loop and will never run. Can you see how code indentation makes it easier to read your own code?

#26 brattus123

  • Members
  • 17 posts

Posted 23 September 2013 - 10:08 PM

ok now how would i get this program to loop until a redstone signal is applied

#27 BigTwisty

  • Members
  • 106 posts

Posted 23 September 2013 - 10:23 PM

rednet.open ("left")
repeat
  local event, id, msg = os.pullEvent ("rednet_message")
  if id == 18 then
    if msg == "nograv" then
      shell.run ("offorbit")
    else
      shell.run ("orbit")
    end
  end
until rs.getInput("left")


#28 brattus123

  • Members
  • 17 posts

Posted 23 September 2013 - 10:57 PM

instead of a line of redstone being applied directly to the turtle how would i make it stop looping with a rednet signal also i changed the rednet_message to rednet.recieve to stop the turtle from wating indefinatly and now im getting the error rednet :68: Expected number here is the code:

rednet.open ("left")
while true do
local event, id, msg = os.pullEvent (rednet.receive ("10"))
if id == 18 then
if msg == "nograv" then
shell.run ("offorbit")
shell.run ("orbit")
end
end
end


#29 BigTwisty

  • Members
  • 106 posts

Posted 24 September 2013 - 10:29 PM

View Postbrattus123, on 23 September 2013 - 10:57 PM, said:

instead of a line of redstone being applied directly to the turtle how would i make it stop looping with a rednet signal also i changed the rednet_message to rednet.recieve to stop the turtle from wating indefinatly and now im getting the error rednet :68: Expected number here is the code:

rednet.open ("left")
while true do
local event, id, msg = os.pullEvent (rednet.receive ("10"))
if id == 18 then
if msg == "nograv" then
shell.run ("offorbit")
shell.run ("orbit")
end
end
end

Look at your os.pullEvent line. Read the documentation on os.pullEvent, look at your code again, and have a nice facepalm moment.

#30 plazter

  • Members
  • 134 posts

Posted 29 September 2013 - 12:33 PM

Not sure why you want shell.run, but as they said, you forget the "os.pullEvent('rednet_message')"

Also try avoiding multi shells :) i use functions :)

#31 brattus123

  • Members
  • 17 posts

Posted 30 September 2013 - 12:14 AM

ok so need to change it back to os.pullEvent ("rednet_message") but what i need it to do is check for a rednet signal if there is one execute the offorbit program and if there is not one execute the orbit program.

#32 BigTwisty

  • Members
  • 106 posts

Posted 30 September 2013 - 12:27 PM

The answer lies in the tutorials on this very site. Check out the "If then else" tutorial here: http://www.computerc...onal_statements
and the Redstone API documentation here: http://www.computerc.../Redstone_(API). In the Redstone API, check out the rs.getInput(side) function. Figuring it out from there should be simple enough.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users