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

Multiple User Input Variables For Rednet Code

Started by kaioo1312, 28 October 2013 - 05:35 AM
kaioo1312 #1
Posted 28 October 2013 - 06:35 AM
I found how to get users input to a rednet. api and i have a problem, i need to user inputs insted of one (id and message). This is the code that jay5674 wrote
Spoiler

write("Enter Message: ")
input = read()
rednet.open(side)
rednet.send(id, input)
rednet.close(side)
if anyone could either tell me or point me in the right direction it would be much apprechiated :)/>.

EDIT: i just tried this and it failed :angry:/>
Spoiler

function partA()
shell.run("clear")
print("ID:")
inputA=read()
  if inputA==("/") then --To move to next part when done
partB()
else
A=inputA
end
end

function partB()
shell.run("clear")
print("message:")
inputB=read()
if inputB==("/") then
rednetCode()
else
print("Enter /")
end
end

function rednetCode()
rednet.close("top")
rednet.open("top")
rednet.send(inputA,inputB)
end
inputA()

P.S sorry for bad spelling.
Bomb Bloke #2
Posted 28 October 2013 - 07:40 AM
Defining functions is similar to defining variables: You can't try and add two variables together before defining them, and likewise you can't try to call functions before defining them. partA() hence can't reliably call partB() because partB() won't've been defined yet. Functions, like variables, should be declared as local where possible.

Also, functions won't execute at all unless called. partA() wants to call partB(), and partB() wants to call rednetCode(), but nothing ever calls partA().

Though if your code only ever calls a function once, then usually that target code doesn't need to be in a function at all. Consider ditching all the functions you use here, at least until you get the basics of input + rednet working: they only serve to muddy the waters.

The computer ID will need to be treated as a number, not text. Try tonumber(read()) - this returns nil if it fails (eg something other then a number was typed), so checking to see if that's what inputA is set to after getting input will let you know if you need to ask again. A simple "while" loop can be used for this:

local inputA
while not inputA do  -- If a variable is set to nil, it's treated as false.
  print("Please type the target computer ID:")
  inputA = tonumber(read())
end
print("You typed "..inputA..".")

I'm not strictly sure why you'd be interested in getting users to type forward slashes.
kaioo1312 #3
Posted 28 October 2013 - 07:59 AM
Defining functions is similar to defining variables: You can't try and add two variables together before defining them, and likewise you can't try to call functions before defining them. partA() hence can't reliably call partB() because partB() won't've been defined yet. Functions, like variables, should be declared as local where possible.

Also, functions won't execute at all unless called. partA() wants to call partB(), and partB() wants to call rednetCode(), but nothing ever calls partA().

Though if your code only ever calls a function once, then usually that target code doesn't need to be in a function at all. Consider ditching all the functions you use here, at least until you get the basics of input + rednet working: they only serve to muddy the waters.

The computer ID will need to be treated as a number, not text. Try tonumber(read()) - this returns nil if it fails (eg something other then a number was typed), so checking to see if that's what inputA is set to after getting input will let you know if you need to ask again. A simple "while" loop can be used for this:

local inputA
while not inputA do  -- If a variable is set to nil, it's treated as false.
  print("Please type the target computer ID:")
  inputA = tonumber(read())
end
print("You typed "..inputA..".")

I'm not strictly sure why you'd be interested in getting users to type forward slashes.

A. thanks
B. There was suppost to be a (and now is) a inputA() call
C. ok but what about the message part (inputB())
Bomb Bloke #4
Posted 28 October 2013 - 08:09 AM
C. ok but what about the message part (inputB())
I would loop until something's been typed. Wouldn't much care what.

local inputB = ""
while inputB == "" do
  print("Please type the message:")
  inputB = read()
end
print("You typed \""..inputB.."\".")

Bear in mind that if you stick with the functions, then you'll need to either use global (as opposed to local) variables or (preferably) pass the variables between said functions.
kaioo1312 #5
Posted 28 October 2013 - 08:13 AM
thanks
EDIT: is it possible to contain all of this code including locals in one function?
Bomb Bloke #6
Posted 28 October 2013 - 04:30 PM
It's possible to have all that code, including locals, with no functions.