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

Issues With Functions And Parallel

Started by Sivarias, 17 October 2013 - 09:09 AM
Sivarias #1
Posted 17 October 2013 - 11:09 AM
Hey guys! I'm trying to write a basic chat program as a proof of concept to see if I can get remote control turtles up and running

here's what I have so far

	--!chat
	--This is for simple emailing from computers
	function recieve()
	 while true do
	  event, side, channel, replychannel, message, distance=os.pullEvent("modem_message")
	  print("Message from Computer ID: "..replychannel.." from "..distance.." blocks away!")
	  print(message)
	 end
	end
	
	function send()
	 while true do
	  print("Public Message=1, Private Message=0")
	  type=read()
	  if type=="0" then
	   print("What ID would you like to send it to?")
	   ID=read()
	  else
	   ID=100
	  end
	  print("What would you like to say?")
	  sent=read()
	  modem.transmit(ID, os.getComputerID(), sent)
	 end
	end
	
	parallel.waitForAny(recieve,send)


I'm getting an error at parallel:22, !chat:23 expecting a number, but going through twice I don't see where a number doesn't show up.

This is coupled with the fact that this is the first time I've ever used functions and parallel I'm feeling a little out of my depth

any help?
Engineer #2
Posted 17 October 2013 - 11:30 AM
if ID gets set by read(), it becomes a string, but what you want is a number.
Now there a 2 options here:
  1. Use the function tonumber and leave it by that
  2. Use the function tonumber and make sure that you have a number
First, we go over what tonumber is. If you do this:

local number = tonumber( "1" )
Number contains an actual number. But what you have is a string, which we cannot do anything with, with your intent.
So tonumber makes from a string a number, if it can. Otherwise it wil return nil (Which is similair to null in other programming languages).

The problem is though, what if the user types: "This is not a number" and you tonumber that, it will return nil! We dont want that, so we use a loop:

local Number --# Initialize variable 
repeat --# start a repeat loop
  Number = tonumber( read() ) --# Let the user type their number and convert it to the number type
until Number --# if the read returns anything but numbers, Number will be nil. And this will continue when Number is valid.
And that will make sure its a number, it keeps asking until it can convert it to a number :)/>
Sivarias #3
Posted 17 October 2013 - 01:26 PM
Thanks Engineer! I'm also digging the name, because I'll give you two guesses what my major is in :-P. I'm familiar with while, for, and if statement [Matlab was fun] but a repeat loop is new to me.

What makes it any different from a while loop?
LBPHacker #4
Posted 17 October 2013 - 01:35 PM
What makes it any different from a while loop?
It checks the expression at the end of the loop, so the body of the loop is executed at least once. It continues looping if the expression evaluates false (as stated here).