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

Thread hijack question.

Started by KatraAmalia, 21 October 2013 - 03:56 PM
KatraAmalia #1
Posted 21 October 2013 - 05:56 PM
Hey, I've been playing with some rednet stuffs, and I'm having issues running a command on a turtle over rednet wireless. So let's say program "x" is on the turtle, how do i use the rednet computer in another area to execute "x"?
Lyqyd #2
Posted 21 October 2013 - 06:14 PM
Split into new topic.

You need to go read the sticky posts.
Engineer #3
Posted 21 October 2013 - 06:27 PM
First you must make sure both turtles are even in loaded chunks, and in range of each other with rednet.
From there its easy:
  • Make sure the turtles can talk to each other
  • On the turtle where x is, you need to have something like this:
  • 
    if message == "runX" then
      shell.run( "x" )
    end
    
  • And this is the win-tip: Post your current code!@#
KatraAmalia #4
Posted 21 October 2013 - 10:55 PM
Split into new topic.

You need to go read the sticky posts.

sticky posts read, will make sure i do that next time :)/>
KatraAmalia #5
Posted 21 October 2013 - 11:07 PM
First you must make sure both turtles are even in loaded chunks, and in range of each other with rednet.
From there its easy:
  • Make sure the turtles can talk to each other
  • On the turtle where x is, you need to have something like this:
  • 
      if message == "runX" then
    shell.run( "x" )
    end
  • And this is the win-tip: Post your current code!@#

current code: http://pastebin.com/VefNhLV3

it's extremely inefficient, seeing as it's my first code. I know there is a way to make it so that instead of typing all the actions out, I can just type it once as a function, but I couldn't figure it out >.<

also, where would i add the code you suggested? again, i'm sorry, i have 0 knowledge of this stuff >.<
Bomb Bloke #6
Posted 22 October 2013 - 12:37 AM
You can't really fit rednet into your current program, at least, not without re-writing it to take advantage of loops (and preferably functions as well). Engineer was suggesting that you make an alternate script that calls your "action" scripts based on rednet input.

It'd look something like:

local senderId, message, distance
rednet.open(where ever the modem is)
while true do
  senderId, message, distance = rednet.receive()
  if message == "runX" then
    shell.run( "somescript1" )
  elseif message == "runY" then
    shell.run( "somescript2" )
  elseif
    etc
  end
end

The rednet API documentation is worth a read if you haven't come across it yet.

In the short term you might instead consider using Lyqyd's nsh system to allow you to simply connect to your turtle and execute the script you've got.
KatraAmalia #7
Posted 22 October 2013 - 01:48 AM
Found a piece of code on another post, suggested by Lyqyd (thread link http://www.computercraft.info/forums2/index.php?/topic/15315-how-to-send-commands-wirelessly-and-some-monitor-help/)

"local id, message = rednet.receive()
shell.run(message)"

mixing this with my wireless turtles has managed to allow what I wanted. I also saw somewhere that if you name a program "startup", if my server restarts, it will run the command automatically. using this, ive taken lyqyd's suggestion, and turned it into a stand alone script that runs whenever the computer starts. However, when the server restarts, the rednet modem on my turtles seems to reset to the closed position. is there any way around this?
Bomb Bloke #8
Posted 22 October 2013 - 02:12 AM
Take the command you use to open your modem and stick that in the startup script.
KatraAmalia #9
Posted 22 October 2013 - 03:13 AM
how do I ensure that the turtle is in the LUA api when it boots?
theoriginalbit #10
Posted 22 October 2013 - 03:26 AM
how do I ensure that the turtle is in the LUA api when it boots?
In Lua when a false or nil value is used in a conditional statement it is resolved to false, any other value will resolve to true. As such we can do the following

if turtle then --# this is the same as if turtle ~= nil then
  print("Welcome Turtle user!")
else
  print("Welcome Computer user!")
end
The turtle API is only initialised when it is a Turtle, on a Computer the turtle API is nil.
TheOddByte #11
Posted 22 October 2013 - 03:17 PM
I just looked at your pastebin code and may I suggest using 'for' loops?

Example

for i = 1, 7 do
    turtle.dig()
    turtle.up()
end

For Loops