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

Why Won't The Print Command Work Here?

Started by warlycanz, 03 September 2013 - 12:54 PM
warlycanz #1
Posted 03 September 2013 - 02:54 PM

if turtle.forward() and
peripheral.isPresent ("front") then
  if peripheral.call("front", "turnOn") then
  print("on")
  elseif peripheral.call("front", "shutdown") then
  print("off")
  end
end

turtle.back ()
No errors appear and all of the rest of the code functions properly, the idea is the turtle moves forward turns on a computer then returns to its original position, the eventual plan is to have the turtle move around my base turning on farming/mining turtles etc.
I realise that the print section isnt neccesary but I was running it with variables originally and nothing put between line 3-5 or 5-7 register.
Lyqyd #2
Posted 03 September 2013 - 04:14 PM
Split into new topic.

I'm not sure those peripheral calls even return anything. What do they return if you run them from the Lua prompt (the turnOn and shutdown calls)?
warlycanz #3
Posted 03 September 2013 - 04:18 PM
there is a computer a block away the calls turn on and off the computer. i tried using a peripheral wrap but that shot back errors at me this however worked
Lyqyd #4
Posted 03 September 2013 - 05:11 PM
You're not understanding the nature of my question. Move the turtle so it is touching the other computer, go into the Lua prompt, and type in peripheral.call("front", "turnOn") and see if anything prints out. I'll bet nothing does, which would mean your if conditions not evaluate to nil, so both of your print statements will never be reached.
warlycanz #5
Posted 05 September 2013 - 11:53 PM
so can I just use the peripheral.call for the front part? something like

if peripheral.call ("front") then
  c.turnOn()
end
LBPHacker #6
Posted 06 September 2013 - 12:20 AM
Almost. In programming, you assign values to variables. Now look at "c" in your code. Nothing is assigned to it (Lua calls that a nil value). If you try to run that, you'll get "attempt to index ? (a nil value)" error. But it almost works like that. Fortunately, there is a method in the peripheral API which returns exactly the value you need and you can put into c so c.turnOn will work. It's called peripheral.wrap:
local cmp = peripheral.wrap("front")
cmp.turnOn()
You see? now that we actually assigned something to cmp, we can use it.

But want to execute only one peripheral method; using peripheral.call in this case is easier (Lyqyd's code two comments above).
warlycanz #7
Posted 06 September 2013 - 12:42 AM
i had it that way round originally, it errored out on me because it was looking for the computer in front of it before moving, it works fine with the turtle remaining stationary beside the computer. can a variable be created mid code?
LordIkol #8
Posted 06 September 2013 - 06:58 AM

peripheral.call(string side, string method, ...) any
Calls a method on a peripheral. The arguments (apart from side and method) and the return values depend on the method being called. If no peripheral is connected, returns nil.

As you see above the Wiki is saying that the return Value of peripheral.call is depending on the method called.
I suggest if you put a "return true" at the end of your turnOn and shutdown code, it will be returned by peripheral.call also.

will check that when im home.

Greets
Loki
LBPHacker #9
Posted 06 September 2013 - 09:03 AM
-snip-
This might be just me being tired and not interpreting English properly, but are you suggesting to rewrite the turnOn and shutdown methods?
theoriginalbit #10
Posted 06 September 2013 - 09:19 AM
-snip-
There is no turnOn or shutdown code, they are methods on the objects themselves, they are in the Java-side code.


-snip-
You are not doing what Lyqyd keeps asking you to do…. He keeps telling you to do this:

Place computer and place the turtle DIRECTLY beside it, like so. He then tells you to open the Lua Prompt and do the peripheral calls, like so. If the call returns a value it will then proceed to print in the prompt, however these ones do not return any values. Comparison of no return values, and return values.
warlycanz #11
Posted 07 September 2013 - 02:50 PM
oh right yea i did that, what happened was exactly what he expected. My continued asking is to find a way to change my code to get it to function as wanted
immibis #12
Posted 07 September 2013 - 09:50 PM
There's no peripheral call that will let you tell if a computer is on or off.
MudkipTheEpic #13
Posted 09 September 2013 - 08:29 PM
Quoted from a locked suggestions topic:

MudkipTheEpic said:
Back to the OP, if you need to know if a computer is on or not, use this:

peripheral.call(side,"getID")

It returns nil if the computer is off, and the ID if it's on.