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

Help with rednet

Started by GLKing19, 30 April 2015 - 02:41 PM
GLKing19 #1
Posted 30 April 2015 - 04:41 PM
Hello, I would like to ask for help with my code, I am a beginner at Computercraft and have a problem with this code, whenever I run it it gives me an error
  • os.pullEvent = os.pullEventRaw
  • rednet.open("top")
  • function lancp()
  • term.clear
  • term.setCursorPos(1,1)
  • x = read()
  • if
  • x == 1
  • then
  • rednet.broadcast("1")
  • end
  • if
  • x == 2
  • then
  • rednet.broadcast("2")
  • end
  • if
  • x == 3
  • then
  • rednet.broadcast("3")
  • end
  • if
  • x == 999666
  • then
  • rednet.broadcast("999666")
  • end
  • end
  • repeat lancp() until x == nobodywillguessthisvalue
Square789 #2
Posted 30 April 2015 - 07:48 PM
What error does this program give to you?
Please post something like

error: bios:367: [string "yesthatsastring"]:123: '<name>' expected
Also, please, please use
 and 
(Second without the space at the end.)
Edited on 30 April 2015 - 06:01 PM
Creator #3
Posted 30 April 2015 - 08:08 PM
term.clear()

you don't need a channel to broadcast

channels only go to 65536

Don't put numbers in strings:

"1" ==> string
1 ==> number

x = tonumber(read())
HPWebcamAble #4
Posted 30 April 2015 - 11:56 PM
Here is your code, formatted correctly and in code tags (like Square showed)
Remember to indent (With 2 spaces is the general rule in Computer Craft)


os.pullEvent = os.pullEventRaw

rednet.open("top")

function lancp()

  term.clear   --# This is probably your error - You need ()
  term.setCursorPos(1,1)

  x = read()

  if x == 1 then
    rednet.broadcast("1")
  end

  if x == 2 then
    rednet.broadcast("2")
  end

  if x == 3 then
    rednet.broadcast("3")
  end

  if x == 999666 then
    rednet.broadcast("999666")
  end

end

repeat 
  lancp()
until x == nobodywillguessthisvalue
(NOTE: The forum sometimes messes up tabs/spaces)

Instead of a ton of if statements, use elseif:

if value then

elseif value2 then

elseif value3 then

end


Now, in this specific case, you could just do this:

x = read()

rednet.broadcast(x)
If you only want to do it for the values in your code then you would need to use a table


Like Creator said, numbers are different than Strings.
None of your if statements will ever be true, since read() always returns stuff in a string

x = tonumber(read()) --# Convert the value returned by 'read()' to a number
GLKing19 #5
Posted 01 May 2015 - 07:46 AM
Ok, thank you very much for the advice and help.