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

[LUA][ERROR]

Started by Kip336, 26 October 2012 - 06:50 PM
Kip336 #1
Posted 26 October 2012 - 08:50 PM
Hey guys

Just a few hours ago I got infected with computercraft after seeing a youtube video of a turtle running a full automatic treefarm. Cue 4 hours after building it myself, and I'm computerizing my entire base on a SMP server.

I have no experience with LUA at all, so I've been doing lots of googling.. unfortunately, I am stuck.

My program is supposed to send a message over rednet to another computer, to switch on or off a redstone current for the treefarm.
Code:
http://pastebin.com/FSi1xJFG

I get stuck with the error: "rednet:347: Positive number expected". I already know it has to do with lines 3 and 28.
On line 19 I simply put down the receiving computer ID, but right now im testing the script as I move along. Once its done, Id like to be able to change the computer ID in a variable up top, so if the server crashes, or the PC is removed, I only have to edit one variable, instead of scrolling through the code.

I read about the id = tonumber(read()) but I'm not sure how to implement it in this specific code.
sjele #2
Posted 26 October 2012 - 08:52 PM
You defined treefarm as a string. The id has to be a number, fixed code:

this = "15" –Thats a string
this = 15 –Thats a nunber


Controlon = "engage"
Controloff = "disengage"
TreefarmPC = 15 --Fixed line
function clearscreen()
term.clear()
term.setCursorPos(1,1)
rednet.open("top")
end
while true do
clearscreen()
print("TreeFarm Control Program")
print("Please enter engage/disengage command:")
term.setCursorPos(1,4)
command = io.read()
if command == Controlon then
write("Turning Treefarm on")
rednet.send(15, "engage")
sleep(10)
clearscreen()
break
elseif
command == Controloff then
write("Turning Treefarm off")
clearscreen()
print("Turning Treefarm off")
rednet.send(TreefarmPC, "disengage")
sleep(2)
end
end
Ditto8353 #3
Posted 26 October 2012 - 08:52 PM
Line 3: Change "15" to 15
(Remove quotes)

Edit: Ninja'd…
Ditto8353 #4
Posted 26 October 2012 - 08:54 PM
I am also curious about 'break' on line 22. What are you trying to accomplish with this?
Kip336 #5
Posted 26 October 2012 - 08:56 PM
I feel horribly embarrassed now. Thanks guys.
Kip336 #6
Posted 26 October 2012 - 08:57 PM
From what I've figured/tested/gathered; Once it sends the engage message, it moves on to "break", which terminates the program

The disengage message doesnt have the "break", and it keeps returning to the "on/off" question
Ditto8353 #7
Posted 26 October 2012 - 09:03 PM
Yes, 'break' immediately exits the 'while' loop, effectively ending the program. I just wanted to make sure that it was doing what you wanted it to do.
However, I would like to offer an alternative approach.
repeat
   clearscreen()
   print("TreeFarm Control Program")
   print("Please enter engage/disengage command:")
   term.setCursorPos(1,4)
   command = io.read()
   if command == Controlon then
      write("Turning Treefarm on")
      rednet.send(15, "engage")
      sleep(10)
      clearscreen()
   elseif
      command == Controloff then
      write("Turning Treefarm off")
      clearscreen()
      print("Turning Treefarm off")
      rednet.send(TreefarmPC, "disengage")
      sleep(2)
   end
until command == Controlon
Kip336 #8
Posted 26 October 2012 - 09:11 PM
Just tried it; and it seems to do the same and just go back to the question? Not sure if that's supposed to be like that?

I still needed to add the break to the elseif if that matters anything :D/>/>
Ditto8353 #9
Posted 26 October 2012 - 09:16 PM
Hmm… Strange… It should work the same as you explained your previous code works, but it should not require the 'break' statement.
change
command = io.read()
to
local command = io.read()
That could possibly be effecting it. However, only do this if you do not need to use that input outside of the loop.
local variables declared inside of loops are destroyed at the end of the loop.
Kip336 #10
Posted 26 October 2012 - 09:19 PM
Ill change it 'round and get back :D/>/> this is pretty much all the script is going to do. Just needs a "invalid entry" if-statement, and maybe a "cancel" to break it right away.

Nah..
giving the Engage command quits the program, disengage still loops back to the on/off question
Ditto8353 #11
Posted 26 October 2012 - 09:22 PM
Yeah, I wouldn't worry about it too much.
It's just usually best to avoid 'break' when you can. It helps with debugging and just makes code easier to read.
Imagine chasing someone through a building. It's harder to find them if the room has two exits instead of one.
Kip336 #12
Posted 26 October 2012 - 09:30 PM
Well, thanks very much for the help Ditto, the code is fully operational now:

http://pastebin.com/icX5eYUa

The receiving computer is responding fine to every command!
Next part: Once the receiving computer gets the message, he passes on a message to a big bad status monitor, saying its redstone output is on or off……….
remiX #13
Posted 26 October 2012 - 10:15 PM
Hmm quick question.. I see you made TreefarmPC = 15. But on line 20 you don't use TreefarmPC, but '15', yet on line 28 - you do use TreefarmPC.

I'd guess you just missed it out?
Kip336 #14
Posted 27 October 2012 - 03:59 AM
In the final code….yes, I did miss it out actually :D/>/> I was planning on changing this to the TreefarmPC thing, but forgot..I was too intent on finishing it up :)/>/>


Thanks!