Jump to content




[Solved] Exit a function early


  • You cannot reply to this topic
2 replies to this topic

#1 Cyclonit

  • Members
  • 50 posts
  • LocationGermany

Posted 09 July 2012 - 08:54 PM

Hi,

I just wrote a small script for opening the first available rednet modem but somehow it does not work properly. This is the function:

function rednetOpen()

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

	for i, side in ipairs(sides) do
	  
		if (rednet.open(side)) then
			return true
		end

	end

	return false

end

Sadly this function returns false, even though it opened a rednet modem. It simply does not exit when it opened the first available modem. I tried finding answers regarding this issue using google but all of the functions I found did use return the same way I did.

Cyclonit

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 09 July 2012 - 08:58 PM

rednet.open doesn't return any value, so the if will allways evaluate to false, even if there's a modem. It opens the side for use with modem or cable, doesn't matter what's on that side.
If you want to use only modems, you can detect them with the peripheral api:
local function rednetOpen()
  for _,s in ipairs(rs.getSides()) do
    if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
	  rednet.open(s)
	  return true
    end
  end
  return false
end


#3 Cyclonit

  • Members
  • 50 posts
  • LocationGermany

Posted 09 July 2012 - 09:09 PM

Sheesh should have noticed that... thank you





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users