Jump to content




Help with rednet

networking

4 replies to this topic

#1 GLKing19

  • Members
  • 4 posts

Posted 30 April 2015 - 03: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


#2 Square789

  • Members
  • 39 posts
  • LocationUniverse:C:/MilkyWay/Sol/Earth/Europe/Germany

Posted 30 April 2015 - 06: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 [code] and [/code ]
(Second without the space at the end.)

Edited by Square789, 30 April 2015 - 07:01 PM.


#3 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 30 April 2015 - 07: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())

#4 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 30 April 2015 - 10: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


#5 GLKing19

  • Members
  • 4 posts

Posted 01 May 2015 - 06:46 AM

Ok, thank you very much for the advice and help.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users