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

OpenCCSensors inexplicable Radar Errors

Started by A_Tasty_Peanut, 25 December 2015 - 08:31 PM
A_Tasty_Peanut #1
Posted 25 December 2015 - 09:31 PM
Hello this is my first post so I'm sorry if I don't have the proper format down but I digress. Today I finally managed to get my player radar program running but to my dismay it seems to be a very unreliable bit of code which fails inexplicably. My ultimate goal is to make an integrated system between the sensor and the terminal glasses so that I can see outlines of players in a sort of HUD (through blocks ect) as well as see there relative position to me on a mini map. For the moment I'm just building this block of code which will tell me a players coords.


os.loadAPI("ocs/apis/sensor")
local s = sensor.wrap("back")
local POS = {}
local names = {}
local mon = peripheral.wrap("top")
mon.setBackgroundColor(8)
mon.setTextScale(0.5)
local function scan(peris)
  POS = {}
  names = {}
  local r = 0
  local rawTargetData = peris.getTargets()
	if rawTargetData then
	  for k,v in pairs(rawTargetData) do
		if v["IsPlayer"]==true then
		  for z,x in pairs(peris.getTargetDetails(k)) do --this line errors
			if z=="Position" then
			r = r + 1
			names[r] = k
			POS[r] = x
			end
		  end
		end
	  end
	end
end
while true do
  scan(s)
  mon.clear()
	for i=1,#names do
	 mon.setCursorPos(1,1+(i-1)*4)
	 mon.setTextColor(16)
	mon.write(names[i])
	mon.setCursorPos(1,2+(i-1)*4)
	 mon.setTextColor(128)
	 mon.write("X: "..tostring((POS[i]["X"]-562))) --561 is the sensors x coord in the world
	mon.setCursorPos(1,3+(i-1)*4)
	mon.write("Y: "..tostring((POS[i]["Y"]+67)))  --67 is its Y coord
	 mon.setCursorPos(1,4+(i-1)*4)
	 mon.write("Z: "..tostring((POS[i]["Z"]-1142))) --1141 is its Z coord
	end
  sleep(.1)
end

When the code does error its always with the 17th line of code. The error I get is "bad argument; table expected, got nil" I'm not sure if its an error in my code though, as it only happens occasionally and the other times it works as one would expect it to. I haven't been able to tell what causes it. I know that it doesn't happen if i stand still, that it does happen when I move but also not all the time I move and that if it errors out and I stop moving and am in the place it errored out in, it will continue to error out for a few seconds (the time it takes to recover to normal varies) and then it will be fine again until I move.

This truly baffles me since its such an erratic error, any help that can be offered would be very much appreciated, thank you.

A_Tasty_Peanut
Lyqyd #2
Posted 25 December 2015 - 10:47 PM
What's happening is that you're moving across the edge of the sensor's range between the getTargets call and the getTargetDetails call. You should catch the return value of the getTargetDetails call and check to ensure it is not nil before feeding it into the for loop. To be honest, I'm not actually sure why you're using the inner loop, since you could catch the return value of the getTargetDetails call (let's say in a table called details) and then just grab the Position key out of it:


local details = peris.getTargetDetails(k)
if details then --# check to make sure it is a table!
  names[#names + 1] = k
  POS[#POS + 1] = details.Position
end
A_Tasty_Peanut #3
Posted 25 December 2015 - 11:04 PM
That could be it but upon further testing it seems to be more related to where I'm looking on the screen. When ever I look at an object and it is withing an 11 block radius (so there are 9 blocks between me and the block I'm looking) of me the program works. The block can also be transparent like glass and it will still run fine. But when ever I look at empty space it errors out. I have not tested your adjustment however, also your way of setting your table values is much nicer than mine and very appreciated to learn I could set the table that way. I will try to add your methods to my code. Cheers

A_Tasty_Peanut
A_Tasty_Peanut #4
Posted 26 December 2015 - 04:02 AM
I have implemented your code suggestion and it works very well, much cleaner than what I was originally using. Still having the odd issue where it only seems to get the advanced details if I'm looking at a block near to me as I said earlier.
That could be it but upon further testing it seems to be more related to where I'm looking on the screen. When ever I look at an object and it is withing an 11 block radius (so there are 9 blocks between me and the block I'm looking) of me the program works. The block can also be transparent like glass and it will still run fine. But when ever I look at empty space it errors out.
I'm not sure why this is the case, it may well be an intentional short coming of the sensor or a strange quirk of the server I'm on. If anyone has any ideas about it I would love to hear them. As always any help given would be much appreciated.

A_Tasty_Peanut


Edit: sorry i forgot to update for the code i am currently using. It is below

os.loadAPI("ocs/apis/sensor")
local s = sensor.wrap("back")
local POS = {}
local names = {}
local mon = peripheral.wrap("top")
mon.setBackgroundColor(8)
mon.setTextScale(0.5)
local function scan(peris)
  POS = {}
  names = {}
  local r = 0
  local rawTargetData = peris.getTargets()
    if rawTargetData then
	  for k,v in pairs(rawTargetData) do
	    if v["IsPlayer"]==true then
	    local details = peris.getTargetDetails(k)
		  if details then
		  names[#names + 1] = k
		  POS[#POS + 1] = details.Position
		  end
	    end
	  end
    end
end
while true do
  scan(s)
  mon.clear()
    for i=1,#names do
    mon.setCursorPos(1,1+(i-1)*4)
    mon.setTextColor(16)
    mon.write(names[i])
    mon.setCursorPos(1,2+(i-1)*4)
    mon.setTextColor(128)
    mon.write("X: "..tostring((POS[i]["X"]-562)))
    mon.setCursorPos(1,3+(i-1)*4)
    mon.write("Y: "..tostring((POS[i]["Y"]+67)))
    mon.setCursorPos(1,4+(i-1)*4)
    mon.write("Z: "..tostring((POS[i]["Z"]-1142)))
    end
  sleep(.1)
end
Edited on 29 December 2015 - 04:54 AM