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

[lua] 'then' expected

Started by Liude, 03 October 2012 - 01:27 AM
Liude #1
Posted 03 October 2012 - 03:27 AM
Im writing a relay server to use with a com tower and satellite network. Im having trouble with this stretch of code, getting the 'then' expected error on the line that has a 'then'.


level == 0
senderId == nil
local function relay(senderId, level)
  if level == 0 AND senderId == nil then
  senderId1, header, distance = rednet.receive()
  -- multiple lines of code to avoid conflict between messages to relay and broadcasts that lead to senderId and level being set to other values depending on content received
  end
end
PixelToast #2
Posted 03 October 2012 - 03:31 AM
AND needs to be lowercase, lua is mostly case sensitive when it comes to these types of things
Lettuce #3
Posted 03 October 2012 - 03:32 AM
Either nil needs quotation marks, or it may not accept nil. Besides which, you don't need to expressly say nil, Lua automatically assumes nil. Also, AND is in lowercase. (I've been :(/>/> 'd)

Just noticed: to set variables, you use "=" not "=="
== returns true or false, it does not set values.
Liude #4
Posted 03 October 2012 - 03:38 AM
tried using lowercase and, it runs the rest of the code ignoring the rednet.receive(). and my bad, Im not setting the variables like that in the actual code, I set it by parallel.waitForAll(rungps, relay(nil, 0)) since Im using the satellite for gps too

Spoiler

shell.run("id")

local function rungps()
  shell.run("gps","host",-192,254,263)
end

local function relay(senderId,level)
print("Relay server active.")
if level == 0 and senderId == nil then
  event, senderId1, header, distance = os.pullEvent("rednet_message")
  senderId1 = tonumber(senderId1)
  header = tostring(header)
  if header == "Header" then
   event, senderId2, receiver, distance = os.pullEvent("rednet_message")
   print("Step 2 Complete")
   senderId2 = tonumber(senderId2)
   receiver = tonumber(receiver)
   if senderId2 == senderId1 then
	event, senderId3, message, distance = os.pullEvent("rednet_message")
	senderId3 = tonumber(senderId3)
	message = tostring(message)
	if senderId3 == senderId1 then
	 rednet.send(receiver, message)
	else
	 if header == "Header" then
	  parallel.waitForAll(relay(senderId1,2),relay(senderId3,1))
	 end
	end
   else
	if header == "Header" then
	 parallel.waitForAll(relay(senderId1,1),relay(senderId2,1))
	end
   end
  end
elseif level == 1 then
elseif level == 2 then
else
end
print("Done")
end

parallel.waitForAll(rungps, relay(0, nil))

this is the full code, Ive only coded level 0 as of now which is for the first user to send a message with no interruptions. will do the other levels if I can fix this problem.

I know it skips the code since it displays the "Done" message right after "Relay server active."

What it does is wait for a first message "Header", which tells it someone is trying to send a message. Afterwards it waits for the reciever of said message, then the message from the same Id that sent "Header". If another user sends a "Header" message while the first user is being processed then a new process is created on level 1, which means you already know the SenderId and will wait for further messages from the same sender, while switching the first user to a new process starting at the same level in order to be able to multitask multiple messages from multiple senders at the same time.
Edited on 03 October 2012 - 02:11 PM
KaoS #5
Posted 03 October 2012 - 07:15 AM
you still have to open an attached modem to receive any messages… rednet.open(modemside)
Doyle3694 #6
Posted 03 October 2012 - 08:52 AM
Also, formatting abit more would be nice. not just leaveing spaces but leaving blank lines sometimes like when you write a text would be nice.
Liude #7
Posted 03 October 2012 - 03:35 PM
Modem was opened in startup file so I dont need to open it here too… Do I? This is run straight from startup so I dont think I'd need to, and the modem is never closed. Anyways, added a line to open the modem, but it still does the same. Also for some reason the code insert screwed up the formatting, this is straight from Notepad++ to the post this time.


rednet.open("back")

shell.run("id")

local function rungps()
  shell.run("gps","host",-192,254,263)
end

local senderId = nil
local level = 0

local function relay(senderId,level)
	if level == 0 and senderId == nil then
		event, senderId1, header, distance = os.pullEvent("rednet_message")
		senderId1 = tonumber(senderId1)
		header = tostring(header)
		if header == "Header" then
			event, senderId2, receiver, distance = os.pullEvent("rednet_message")
			senderId2 = tonumber(senderId2)
			receiver = tonumber(receiver)
			if senderId2 == senderId1 then
				event, senderId3, message, distance = os.pullEvent("rednet_message")
				senderId3 = tonumber(senderId3)
				message = tostring(message)
				if senderId3 == senderId1 then
					rednet.send(receiver, message)
				else
					if header == "Header" then	
						parallel.waitForAll(relay(senderId1,2),relay(senderId3,1))
					end
				end
			else
				if header == "Header" then
					parallel.waitForAll(relay(senderId1,1),relay(senderId2,1))
				end  
			end
		end
	elseif level == 1 then
	elseif level == 2 then
	else
	end
end

parallel.waitForAll(rungps, relay)

Also worth to note that besides 'then' expectedget no other errors when the code is run. I ran a smaller version of this on another computer and it worked fine so Im not sure why it isnt working here. The smaller version just relays the message so it might get scrambled by gps pings and other broadcasts so its not viable to use on servers.
Liude #8
Posted 07 October 2012 - 10:43 PM
No one has any solutions?