Jump to content




Nil value check


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

#1 Carden

  • Members
  • 5 posts

Posted 02 May 2013 - 08:55 AM

Title: Nil value check

Using openccsensors and a turtle to detect a player and open a door.

os.loadAPI("ocs/apis/sensor")
while true do
local prox = sensor.wrap("right")
local targets = prox.getTargets()
if targets == "" then
  redstone.setOutput("bottom", false)
else
  for k,v in pairs(prox.getTargets()) do
	local ptest = prox.getTargetDetails(k)
	if ptest.Name ~= "Player" then
	 redstone.setOutput("bottom", false)
	else
	 redstone.setOutput("bottom", true)
	end
	
  end
end
end

Detection works correctly however walking away and back into range leads to the following error code.

test2:10: attempt to index ? (a nil value) Which should be this line if ptest.Name ~= "Player" then

I'm banging my head against the wall as to how to skip line 10 if there is no Player nearby.

#2 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 02 May 2013 - 09:47 AM

if ptest.Name ~= nil then
   if ptest.Name ~= "Player" then
      --DO STUFF
   end
end


#3 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 02 May 2013 - 10:16 AM

just do
if ptest.Name and ptest.Name ~= "Player" then


#4 Carden

  • Members
  • 5 posts

Posted 02 May 2013 - 11:00 PM

Unfortunately it seems that in both cases a nil value is being indexed as ptest.Name is still being accessed without any valid targets. Is there a way to inject a dummy entry into the table that would not effect the search for players but still allow for a entry to exist?

#5 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 03 May 2013 - 08:12 AM

View PostCarden, on 02 May 2013 - 11:00 PM, said:

Unfortunately it seems that in both cases a nil value is being indexed as ptest.Name is still being accessed without any valid targets. Is there a way to inject a dummy entry into the table that would not effect the search for players but still allow for a entry to exist?
May be:

if ptest and ptest.Name and ptest.Name ~= "Player" then


#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 May 2013 - 08:14 AM

just try something for us... after this line
local ptest = prox.getTargetDetails(k)
add this in
print(type(ptest))
and tell us what it says. btw it should say 'table'

#7 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 03 May 2013 - 09:54 AM

os.loadAPI("ocs/apis/sensor")
while true do
  local prox = sensor.wrap("right")
  local targets = prox.getTargets()
  if targets == "" then
	redstone.setOutput("bottom", false)
  else
	for k,v in pairs(prox.getTargets()) do
	  local ptest = prox.getTargetDetails(k)
	  if type(ptest)=="table" and ptest.Name ~= "Player" then
		redstone.setOutput("bottom", false)
	  else
		redstone.setOutput("bottom", true)
	  end
	end
  end
end

is the corrected code

EDIT: HOWEVER it will not work either, if the targets table contains a player and a mob it will go through the table and set the output to true when it finds the player, then set it to false again when it finds the mob...

os.loadAPI("ocs/apis/sensor")
while true do
  local prox = sensor.wrap("right")
  local targets = prox.getTargets()
  redstone.setOutput("bottom", false) --default to off, set to on if player is found
  if type(targets)=="table" then
	for k,v in pairs(targets) do --no need to execute the command again, you already have it in a var
	  local ptest = prox.getTargetDetails(k)
	  if type(ptest)=="table" and ptest.Name == "Player" then
		redstone.setOutput("bottom", true)
		break --makes the code execute faster as it doesn\'t waste time with other mobs once a player is detected
	  end
	end
  end
  sleep(0.5) --small delay, no need to hog processing time
end

Edited by KaoS, 03 May 2013 - 09:59 AM.


#8 Carden

  • Members
  • 5 posts

Posted 04 May 2013 - 10:02 AM

View PostKaoS, on 03 May 2013 - 09:54 AM, said:

if type(targets)=="table" then
    &
if type(ptest)=="table" and ptest.Name == "Player" then

These 2 solved it perfectly,
Thank you

#9 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 05 May 2013 - 04:30 AM

Glad to help :)

PLEASE read the edit though or your prog won't work in all situations, use the second code





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users