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

OpenP Sensor getPlayerNames() problem when no players in range

Started by PhantomOfTheRouter, 02 January 2015 - 01:47 AM
PhantomOfTheRouter #1
Posted 02 January 2015 - 02:47 AM
I'm trying to create a sensor in my base that will do a few things, like turning on a shower if I TP in and am on fire, spit out food if hungry, or potions if injured, that sort of thing.

I created a program that should read player status from the sensor.

I start the program on a computer, and leave the area.

When I return, I will find that the program ends, with a line that say
programname:5: Entity not found

How can I better loop this, so that it'll only run s.getPlayerData("Phantom") when I'm in range, but will keep running even when no entities are in range?

Here is a quick example:

sensor = peripheral.wrap("right")
rs.setoutput("bottom",false)

while true do
	 local owner = sensor.getPlayerData("Phantom") --  Determine stranger danger or not...

	 if owner then
		 local hunger = sensor.getPlayerData("Phantom")["foodLevel"]  -- getting hunger level
		 if hunger < 20 then
				-- Code to spit out some food, then sleep for 15
		 else
		 rs.setoutput("bottom",false) --  Did this to make sure that my redstone signal returned to false
		 end
	 end
sleep(0)
end




If this has been answered before, I apologize, but I couldn't find any examples quite close enough to help me out using the sensor block. :(/>

Thank you!


**Edited to add code tags… my bad!
***Edited again for clarity.
Edited on 02 January 2015 - 03:53 AM
Bomb Bloke #2
Posted 02 January 2015 - 06:44 AM
That's odd, I'd expect sensor.getPlayerData("Phantom") to simply return nil if there's no entity by that name around - I'm pretty sure some versions of OpenPeripheral would return nil instead of erroring, but it beats me if they're recent or not. There's really no way to totally prevent the error otherwise - you can never be absolutely certain that that entity is there.

I assume the idea is to use sensor.getPlayerNames(), but that "ensures" nothing - forcing us to use pcall instead:

while true do
  local owner  
  pcall(function() owner = sensor.getPlayerData("Phantom") end)

  if owner then
    -- etc

Anyway, what's with this?:

                 local hunger = sensor.getPlayerData("Phantom")["foodLevel"]  -- getting hunger level
                 if hunger < 20 then

Since you already have all that data in your "owner" table, why not just do?:

                 if owner.foodLevel < 20 then
PhantomOfTheRouter #3
Posted 02 January 2015 - 02:42 PM
Oops :(/> I thought it was working, but it isn't. I still get the same error at line 5…

local owner --This is line 5
pcal(function() owner = sensor.GetPlayerData("Phantom") end)
if owner then
	 if owner.foodLevel < 20 then
		 --redstone stuff
	 else
		 --redstone all false
	 end
end
sleep(0)
end
Dragon53535 #4
Posted 02 January 2015 - 03:34 PM
Oops :(/> I thought it was working, but it isn't. I still get the same error at line 5…
–snip–
Change it to this>

local success,owner = pcall(function() return sensor.GetPlayerData("Phantom") end) --#pcall returns two values, if it was successful, or not, and the error or actual value.
if success then
		 if owner.foodLevel < 20 then
				 --redstone stuff
		 else
				 --redstone all false
		 end
end
sleep(0)
end
Edited on 02 January 2015 - 02:36 PM
Bomb Bloke #5
Posted 02 January 2015 - 09:41 PM
… and more to the point, make sure the script you're running is the one you're putting the new code in. There's no way the line "local owner" would throw the same error.