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

[1.41][SSP]gps.locate needs timeout?

Started by jerry242, 22 September 2012 - 02:55 PM
jerry242 #1
Posted 22 September 2012 - 04:55 PM
Calling gps.locate() errors" gps:69: attemp to compare nil with number", however if you call gps.locate(1) it functions as normal.
How to reproduce: call gps.locate() without the timeout specified


A quick peek in the code shows us whats wrong

function locate( _nTimeout, _bDebug )
	if _bDebug then
		print( "Finding position..." )
	end
	rednet.broadcast( "PING" )
	
	local startTime = os.clock()
	local timeOut = _nTimeout -- set timeOut to nil if nothing is specified
	local timeElapsed = 0
	
	local tFixes = {}
	local p1, p2 = nil
	while timeElapsed < timeOut and (p1 == nil or p2 ~= nil) do -- here we try to compare, and lua errors

A solution could be:

function locate( _nTimeout, _bDebug )
	if _bDebug then
		print( "Finding position..." )
	end
	rednet.broadcast( "PING" )
	
	local startTime = os.clock()
	if _nTimeout ~= nil then --checking if _nTimeout isn't nil
		local timeOut = _nTimeout -- if it is not nil then set timeOut to _nTimeout
	else
		local timeOut = 1 -- else set timeOut to a default value
	end
	local timeElapsed = 0
	
	local tFixes = {}
	local p1, p2 = nil
	while timeElapsed < timeOut and (p1 == nil or p2 ~= nil) do -- no error
Cloudy #2
Posted 22 September 2012 - 05:11 PM
I don't see the issue? The function is obviously designed to need a timeout - unless it is documented anywhere that you can call it without a timeout.
jerry242 #3
Posted 22 September 2012 - 05:33 PM
Yes, but woudn't it be more userfriendly if it didn't. Only reason I posted this was because I used the past hour going through my code looking for a solution.
Even if it isn't a bug it should work in a smilar way as the rednet.receive().
Both have a timeout variable and I thought they both functioned the same way.
Either that or specify that it needs the timeout parameter on the wiki.
Cloudy #4
Posted 22 September 2012 - 07:00 PM
Then why is it a bug? You can't just assume that something with a timeout parameter is optional just because it is optional on another function.

It should be assumed that the parameter is needed unless it states it is optional.
MysticT #5
Posted 22 September 2012 - 07:17 PM
sleep has a timeout also, do you want it to be optional? I don't think so :P/>/>
Also, your "solution" won't work, since you define a local variable inside an if statement that is never used. If you want to change it, you can change:

local timeOut = _nTimeout
to:

local timeOut = _nTimeout or 1
simple, isn't it?
jerry242 #6
Posted 22 September 2012 - 07:26 PM
Wow I feel stupid :P/>/>