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

LRC Program not working

Started by Kadecamz, 13 September 2012 - 06:10 PM
Kadecamz #1
Posted 13 September 2012 - 08:10 PM
Its not that I get an error code, its just that once you enter the name it pauses.

Server code

---------------------------------------------------
--CONFIG
allowedID = { 2, 5 }
--END OF CONFIG
rednet.open("top")
term.clear()
term.setCursorPos(1,1)
sleep(0)
print(os.getComputerID())
while true do
id, msg = rednet.receive()
if id == allowedID then
rednet.broadcast(id.." said "..msg)
else
sleep(0)
end
end

Client code

serverID = 4

term.clear()
term.setCursorPos(1,1)
sleep(0)
print("Enter your name")
name = read()
rednet.open("top")
rednet.broadcast(name.." has entered the server as ID "..os.getComputerID())
while true do
id, msg = rednet.receive()
if id == "serverID" then
print(msg)
parallel.waitForAny("key")
message = read()
rednet.broadcast(message)
end
end

MysticT #2
Posted 13 September 2012 - 08:21 PM
Server:

---------------------------------------------------
--CONFIG
allowedID = { 2, 5 }
--END OF CONFIG
rednet.open("top")
term.clear()
term.setCursorPos(1,1)
sleep(0) -- ???
print(os.getComputerID())
while true do
id, msg = rednet.receive()
if id == allowedID then -- this will never be true (number ~= table)
rednet.broadcast(id.." said "..msg)
else
sleep(0) -- ???
end
end

Client:

serverID = 4

term.clear()
term.setCursorPos(1,1)
sleep(0) -- ???
print("Enter your name")
name = read()
rednet.open("top")
rednet.broadcast(name.." has entered the server as ID "..os.getComputerID())
while true do
id, msg = rednet.receive()
if id == "serverID" then -- you don't have to put quotes on the variable name
print(msg)
parallel.waitForAny("key") -- did you meant os.pullEvent("key")?
message = read()
rednet.broadcast(message)
end
end
The problem is mainly in the comparisons, since you compare different types of values. In the server, you need to check every value in the table with the given id, something like this:

local function isValidId(id)
  for _,v in ipairs(allowedID) do
    if v == id then
	  return true
    end
  end
  return false
end

local id, msg = rednet.receive()
if isValidId(id) then
  print("Valid ID!")
end

Also, why do you add all those sleep(0)?
Kadecamz #3
Posted 13 September 2012 - 08:24 PM
The sleep(0) is because if you don't add it after term.clear and term.setCursorPos() it set the cursor one section under what you set as the term strings.


And no, I did not mean os.pullEvent("key")
That will pause it until a key is pressed and I do not want that.
Cranium #4
Posted 13 September 2012 - 08:24 PM
rednet.receive() holds until it receives a rednet message. rednet IDs are sent as integers, which are just numbers, not "strings". And I think you are wanting os.pullEvent() instead of parallel.waitForAny…..
Not sure what you are trying to accomplish with your code, but some of these things just don't fit.
MysticT #5
Posted 13 September 2012 - 08:27 PM
The sleep(0) is because if you don't add it after term.clear and term.setCursorPos() it set the cursor one section under what you set as the term strings.
No it won't. They are useless there.

And no, I did not mean os.pullEvent("key")
That will pause it until a key is pressed and I do not want that.
Then that's an error. Try learning how parallel works. It takes functions as arguments, not strings.
Kadecamz #6
Posted 13 September 2012 - 08:31 PM
Ah!
So I should make the receiving part a function and make the os.pullEvent("key") a function too!
And then do parallel.waitForAny(functionhere,functionhere)
MysticT #7
Posted 13 September 2012 - 08:37 PM
You have to make a function to send and one to receive, and then parallel will do it's magic :)/>/>
Example:

local function send()
  while true do
    local msg = read()
    rednet.broadcast(msg)
  end
end

local function receive()
  while true do
    local id, msg = rednet.receive()
    print(id, ": ", msg)
  end
end

parallel.waitForAny(send, receive)
And yes, the loops are necesary, otherwise it will run until you get or send a message and then stop.
Also, the example doesn't check the cursor position or anything, so it will print the received messages where you're writing.
Cranium #8
Posted 13 September 2012 - 08:38 PM
Ah!
So I should make the receiving part a function and make the os.pullEvent("key") a function too!
And then do parallel.waitForAny(functionhere,functionhere)
Yes, that is the ideal way for the parallel API to work for you.