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

Int Variables as Function's Arguments

Started by TheDarknessAngel, 08 June 2013 - 11:19 AM
TheDarknessAngel #1
Posted 08 June 2013 - 01:19 PM
Hey, im new to the CC forums and i just joined because im having some troubles making one of my scripts for LAN Chat using Modems.

I cannot make the script user to enter his own message, frequency and reply frequency…

It goes to the confirm message (Printing the choosen stuff) and for some reason when i get to the transmit code, i have the "Exected Number" Error…

I made a default message as a debug (You will be able to see in the script) to check it with the default variables given, without passing trough any change..

Here's the script:

(Pastebin: TJtkFXjX )

--NOT TO COPY BELOW THIS

--Setting up

sides = {
"front",
"back",
"left",
"right",
"top",
"bottom"
}

function PrintCentered(text, y)
	local w, h = term.getSize()
	x = math.ceil(math.ceil((w / 2) - (#text / 2)), 0)+1
	term.setCursorPos(x, y)
	print(text)
end

function clearConsole()
term.setBackgroundColor(colors.lightGray)
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(1,1)
end

function Terminate()
clearConsole()
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
error("Terminated by Script")
end

--Done

local modem = peripheral.wrap('top')

--ONLY COPY BELOW THIS

--Default Variables
num1 = 1
num2 = 2
msg = Default

clearConsole()

write("Message: ")
local msg = read()
if msg == "default" then
clearConsole()
print("The message is: "..msg.." ")
print("It will be sent to the frequency: "..num1.." ")
print("The requested reply frequency is: "..num2.." ")
sleep(3)
clearConsole()
print("Sending the DEFAULT message..")
sleep(1)
modem.transmit(num1,num2,msg) -- Here its perfectly working.. With the default variables given above
clearConsole()
print("Sucefully sent the Default message. Terminating..")
sleep(2)
Terminate()
end

write("Send frequency: ")
local num1 = read()

write("Reply frequency: ")
local num2 = read()

print("The message is: "..msg.." ") -- All good in here
print("It will be sent to the frequency: "..num1.." ") -- All good in here too. It's an Int value
print("The requested reply frequency is: "..num2.." ") -- Int value too
sleep(2)
write("Write SEND to send this message: ")

local input = read()
if input == "SEND" then
clearConsole()
print("Sending the message..")
sleep(1)

modem.transmit(num1,num2,msg) -- Here, the same code, it's not working.. I cant make this to accept the read() function so i could change the frequency to whatever i want. Given error: Expected Number

else
print("Message canceled by the client.")
print("Terminating...")
sleep(2)
Terminate()
end
Edited by
Lyqyd #2
Posted 08 June 2013 - 04:34 PM
Split into new topic.
Engineer #3
Posted 08 June 2013 - 05:52 PM
read() returns a string. You can use tonumber(read()) to make it a number!
But to make sure the user inputs a number, you can do this:

local num1 = read()
while not tonumber(num1) do
   num1 = read()
end
This will ask until it can make a number out of it.

edit: Use the one below me, Im tired.. Should know this basic stuff.. Im literally table flipping right now
Orwell #4
Posted 08 June 2013 - 07:06 PM
read() returns a string. You can use tonumber(read()) to make it a number!
But to make sure the user inputs a number, you can do this:

local num1 = read()
while not tonumber(num1) do
   num1 = read()
end
This will ask until it can make a number out of it.
Or shorter:

repeat
  num1 = tonumber(read())
until num1
Declaring num1 would add an extra line, but luckily he already declared an assigned it earlier on. This also actually makes num1 a number. In your snippet num1 is still a string, you only know it represents a number.
Bomb Bloke #5
Posted 08 June 2013 - 09:03 PM
Just throwing it out there, this may not be relevant to the situation at hand, but bear in mind that the rednet API is a wrapper for the modem API. When you tell the rednet API to send to a given computer ID, it has the modem API transmit on whatever channel the computer ID is. When you tell a rednet API to receive something, it has the modem API listen on whatever port the computer's ID is and port 65535 (for broadcasts).

For this reason, if you think other users might be transmitting anything nearby with either API, it's probably good practise to only use port numbers that match computer ID numbers that you own. If everyone does this then there should be no signal collisions.