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

Program Will Not Work [Lua]

Started by Cardinalstar16, 05 January 2013 - 02:48 PM
Cardinalstar16 #1
Posted 05 January 2013 - 03:48 PM
My Program will not work I have read the posts to read before post and I can't find out whats wrong at all I will put my code here ,and hopefully someone will answer my question.
PS. This is a Code sent between 2 Computers a Turtle (Which is the Receiver) and a Computer (Which is the sender).


The Turtles Code

rednet.open("right") – This I know is Right
while true do
local dir = rednet.receive()
if dir == (Left) then
turtle.turnLeft() else
if dir == (Right) then
turtle.turnRight) else
print("Me no Work")
end
end
end


The Computers code
rednet.open("left") – I know This is Right Also
print("What would you like your turtle ot do Sir")
local input= read("*") – I would also like to know how to make it so I can see what I type there
rednet.broadcast("input")

So Please tell me whats the Problem Thanks

Cardinalstar16
Leo Verto #2
Posted 05 January 2013 - 03:56 PM
Turtle code:

rednet.open("right")
while true do
  local id, msg = rednet.receive()
  if msg == "Left" then
	turtle.turnLeft()
  elseif msg == "Right" then
	turtle.turnRight()
  else
	print("Me no Work")
  end
end

Computer code:

rednet.open("left")
print("What would you like your turtle to do Sir")
local input = read()
rednet.broadcast(input)
ChunLing #3
Posted 05 January 2013 - 06:03 PM
To explicate a bit, rednet.receive() has three returns, and you want the second. That means that you need to specify an identifier for the variable that is to receive the first (or take all the returns in a table, but we're not doing that).

Thus "local id,msg = rednet.receive()"

Second and fourth, the rednet message text stored in msg is a string, rather than nil (we hope). The identifiers Left and Right, not corresponding to previously initialized variables, are associated with nil. So comparing to them doesn't work, we need to compare to strings, like "Left" and "Right".

Thus "msg == "Left" "and "msg == "Right" "

Third, if then elseif then end reduces unnecessary scope changes. This almost doesn't matter for this case, but the if then elseif then end structure is preferred in that case over nested if then end structures in general when the effect is similar.

Thus "elseif".

Fifth and sixth are pretty self-explanatory so we'll move on to seventh. "input" is a literal string, if you broadcast that then you'll be broadcasting "input", not the string contained in the variable input.

Thus…um…hmmm. Well, "rednet.broadcast(input)".
Fizzgigg #4
Posted 05 January 2013 - 07:16 PM
I think that "dir" is reserved as it stands for another thing. Try changing this to something else like variable or test. Most any random word over three letters long.
Cardinalstar16 #5
Posted 05 January 2013 - 07:43 PM
So Chun what is the third piece of information received from the rednet.received() command. And also I need to make sure I'm checking for strings not what ever I was checking for. This program is just a test so I can figure out the Rednet Api and I want to perfect it be for I move on to mineing programs. There is one more thing I want to know if I have a turtles place down another turtle will it run a start-up program.O also I need to take one end out from the end and put it be for the print command.
Luanub #6
Posted 05 January 2013 - 07:46 PM
The third param returned by rednet.receive() is the distance that the message was sent.

The turtle will not automatically turn on and run startup when placed, but you can connect to it like a peripheral with the turtle that placed it and turn it on. You can either pre-load the startup up or place a disk drive next to the turtle with the file on a disk.
brett122798 #7
Posted 05 January 2013 - 07:49 PM
Am I only one seeing this..? But here..


rednet.open("right") -- This I know is Right
while true do
local dir = rednet.receive()
if dir == (Left) then
turtle.turnLeft() else
if dir == (Right) then
turtle.turnRight) else	 -- Error on this line
print("Me no Work")
end
end
end
ChunLing #8
Posted 06 January 2013 - 03:20 AM
I didn't see it. Did anyone else see it?
remiX #9
Posted 06 January 2013 - 04:41 AM
The Computers code
rednet.broadcast("input")

Remove the quotation marks from there btw, you're sending a variable which is 'input' so no quotation marks must be placed there. If you were want to send the word input then you must put it in quotation marks.

I didn't see it. Did anyone else see it?

See what?
Leo Verto #10
Posted 06 January 2013 - 05:03 AM
I didn't see it. Did anyone else see it?
Nope, I was too tired, fixed it in my code now.
ChunLing #11
Posted 06 January 2013 - 05:54 AM
But you've left the quotes in rednet.broadcast("input").
Leo Verto #12
Posted 06 January 2013 - 06:57 AM
But you've left the quotes in rednet.broadcast("input").
Yeah, I noticed that at first but forgot to change it afterwards.
Cardinalstar16 #13
Posted 06 January 2013 - 08:40 AM
Ok thanks guys I've got the recent thing down. So as of my second question if I place a disk drive next to a turtle how will it auto start up or do I have to put a disk in the drive How Will it k n ow that it needs a program to be put on the turtle man I wish if turtle placed down another tturtle then it would auto start up. My real problem is how do I make a turtle start up without opening it up.
Luanub #14
Posted 06 January 2013 - 09:52 AM
Are you going to have the turtle craft the one it is placing or will it be one that you craft?

If its one that you craft you can just load the startup file on it yourself then you do not have to worry about using a disk drive.

If the turtle is going to craft the other turtle then you're(your turtle) going to have to place a disk drive and insert a disk with the files you want to put on the new turtle.

To have one turtle turn on another here is a function that you can use.

local function turnOn()
for _,side in pairs(rs.getSides()) do  --iterates through the output of rs.getSides() which is front, left, right etc..
  if peripheral.isPresent(side) and peripheral.getType(side) == "computer" or peripheral.getType(side) == "turtle" then --check to see if the current side fron the for loop has a computer or turtle attached
	comp = peripheral.wrap(side) --sets the handle comp to the peripheral side that a computer / turtle was found on
	comp.turnOn()  --turns it on
	break  --breaks out of the loop
  end
end
end
Edited on 06 January 2013 - 01:29 PM
Cardinalstar16 #15
Posted 06 January 2013 - 11:01 AM
Are you going to have the turtle craft the one it is placing or will it be one that you craft?

If its one that you craft you can just load the startup file on it yourself then you do not have to worry about using a disk drive.

If the turtle is going to craft the other turtle then you're(your turtle) going to have to place a disk drive and insert a disk with the files you want to put on the new turtle.

To have one turtle turn on another here is a function that you can use.

local function turnOn()
for x=1, #tSides do
  if peripheral.isPresent(tSides[x]) and peripheral.getType(tSides[x]) == "computer" or peripheral.getType(tSides[x]) == "turtle" then
	comp = peripheral.wrap(tSides[x])
	comp.turnOn()
	break
  end
end
end


See what I want to learn here is how to do it on my own and not just copy someones work I would like to know how that function works so if you could explain it to me so I understand it that would be great because I know little to nothing of what you just typed.
Luanub #16
Posted 06 January 2013 - 02:30 PM
I added comments to the code in my last post.
Cardinalstar16 #17
Posted 07 January 2013 - 11:10 AM
I still don't get it i'm sorry your English is too advanced for me. I don't want to be an annoyance but i'm a beginner and I NEED to go really really slow.
Luanub #18
Posted 07 January 2013 - 12:00 PM
The main things you need to read up on are the computer peripheral functions which you can find here http://www.computercraft.info/wiki/Computer#Peripheral_Functions
The peripheral API which is what allows you to connect to another computer/turtle as a peripheral so you can use the computer peripheral functions: http://www.computercraft.info/wiki/Peripheral_%28API%29

The other part of the function is just a for loop that will go through the sides available on the computer and check for a computer or turtle. Some more information on those are:
generic for loop: http://lua.gts-stolberg.de/en/generischFor.php
the pairs function: http://www.lua.org/manual/5.1/manual.html#pdf-pairs

Here is a tutorial on loops http://www.computercraft.info/forums2/index.php?/topic/7999-loops/

Hope this helps some.
brett122798 #19
Posted 07 January 2013 - 01:46 PM
I still don't get it i'm sorry your English is too advanced for me. I don't want to be an annoyance but i'm a beginner and I NEED to go really really slow.
If you'd like our English to be simplified, translate it to your main language here.
ChunLing #20
Posted 07 January 2013 - 08:54 PM
Hilarity ensues.