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

[Lua] Need help making a program work

Started by Axolotl, 21 January 2013 - 02:19 PM
Axolotl #1
Posted 21 January 2013 - 03:19 PM
I'm still not very good with lua so this program probably has multiple issues with it. I know that each function works as a separate program but i may have made typos. I also don't know if I have the functions set up correctly. In my first test run I got this error:
bios:338: [string "rednet"]:6: '<name>' expected

Here's my code. There are some additional things I would like to add but I'll ask about those after the errors are resolved.

function receive() --receives rednet messages, timeout is set by user
  term.clear()
  term.setCursorPos(1,1)
  print("Duration:")
  local var = tonumber(read())
  local senderId, message, distance = rednet.receive(var) --resolved issue in line 6 (extra comma)
  print("")
  write("From:")
  print("senderId")
  print("message")
end

function send() --sends custom message to specified computer
  term.clear()
  term.setCursorPos(1,1)
  print("TargetID:")
  local var = tonumber(read())
  print("Message:")
  local var1 = read()
  rednet.send(var,var1)
end

function broadcast() --broadcasts custom message
  term.clear()
  term.setCursorPos(1,1)
  print("Message:")
  local var = read()
  rednet.broadcast(var)
end

function configure() --allows user to either open or close a connection on a specified side
  while true do --resolved issue in line 32 (capital W)
	term.clear()
	term.setCursorPos(1,1)
	print("Do you want to open or close a connection?")
	local var = read()
	if var == "open" then
	  print("Which side would you like to connect?")
	  local var1 = read()
	  rednet.open(var1)
	elseif var == "close" then
	  print("Which side would you like to disconnect?")
	  local var2 = read()
	  rednet.close(var2)
	else
	  print("Invalid Response")
	  sleep(2)
	end
  end
end

function announce() --just announces
  term.clear
  term.setCursorPos(1,1)
  rednet.announce()
end

while true do --this is the menu part
  term.clear() --sets up the terminal and prints all the menu text
  term.setCursorPos(1,1)
  print("Welcome to RedNet!")
  print("v1.0.0")
  print("")
  print("Actions:")
  print("1. Receive")
  print("2. Send Message")
  print("3. Broadcast Message")
  print("4. Configure Modem")
  print("5. Announce")
  print("")
  write("Perform action ")
  local var = read() --reads the input and then there's a long conditional statement
  if var == "1" then -- corresponds to the printed menu
	receive() --this is suppose to call the corresponding function
  elseif var == "2" then
	send()
  elseif var == "3" then
	broadcast()
  elseif var == "4" then
	configure()
  elseif var == "5" then
	announce()
  else --if I enter something stupid like "axolotl" this is supposed to print the next line and restart the loop
	print("This action does not exist.")
  end
end
ChunLing #2
Posted 21 January 2013 - 03:27 PM
local senderId, message, distance, = rednet.receive(var)

That extra "," makes Lua think you were going to put another identifier (<name>) there.
Axolotl #3
Posted 21 January 2013 - 03:39 PM
Wow. I would've never seen that if you hadn't pointed it out. Thanks!

Now it says "=" expected in line 32. Why is that?
Willibilly19 #4
Posted 21 January 2013 - 04:44 PM
just take the capital off the 'w' in while

The next error is line 53, you need to add () to your term.clear.
Axolotl #5
Posted 21 January 2013 - 04:50 PM
what about "=" expected in line 54?
Willibilly19 #6
Posted 21 January 2013 - 04:53 PM
haha, I ninja'd that away from you as well. I edited it into my last reply.
Axolotl #7
Posted 21 January 2013 - 05:47 PM
Oops. I must have been typing this too fast cuz I have a bunch of those stupid little mistakes in it. Thanks for all the help.
Willibilly19 #8
Posted 21 January 2013 - 05:55 PM
No worries, Glad I could help. What other questions did you have? I don't know if I can answer them…but it's worth a shot.
remiX #9
Posted 22 January 2013 - 01:26 AM
In your receive function you're printing "senderId" and not the actually ID.



  local senderId, message, distance = rednet.receive(var) --resolved issue in line 6 (extra comma)
  print("")
  write("From:")
  print(senderId or "nil/no_response")
  print(message or "nil/no_response")
Axolotl #10
Posted 22 January 2013 - 04:21 AM
senderId is supposed to be like that. That way it prints whatever the senderId was.
Willibilly19 #11
Posted 22 January 2013 - 04:26 AM
Yea, it's literally printing "senderId" though because you have quotes around it. take the quotes off to print the Id of the sender.
remiX #12
Posted 22 January 2013 - 04:28 AM
Yeah, to prints variable, you have the variable name in, without quotes.

The way I had it:
print(senderId or "nil/no_response")

If it times out, it will then print "nil/no_response"
Axolotl #13
Posted 22 January 2013 - 04:40 AM
Oh. I actually had that right on my in game computer. The program as a whole runs fine now but I need to make the different functions sleep until I tell them to continue.

For example…

function receive()
  term.clear()
  term.setCursorPos(1,1)
  print("Duration:")
  local var = tonumber(read())
  local senderId, message, distance = rednet.receive(var)
  print("")
  write("From:")
  print(senderId)
  print(message)
  -- at this point I need it to sleep until further notice. that way it doesn't immediately go back to the main menu
end
Willibilly19 #14
Posted 22 January 2013 - 04:59 AM
If you are going to tell it to continue, you can do this.


print("Continue?")
read()

To continue with that, you just hit enter.

You don't have to print if you don't want to.
SuicidalSTDz #15
Posted 22 January 2013 - 05:07 AM
Oops. I must have been typing this too fast cuz I have a bunch of those stupid little mistakes in it. Thanks for all the help.

Remember Lua is case sensitive, and don't forget to close each line with () when needed. Other than that, I wish you the best of luck with your programs!