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

OpenPeriph Sensor auto opening door

Started by Shivy011, 06 June 2016 - 10:57 AM
Shivy011 #1
Posted 06 June 2016 - 12:57 PM
Hi,

I have

sensor = peripheral.wrap("top")
sensor.getPlayers()

Now, how do I access the table that getPlayers() makes and then do an if whatever = shivy011 then redstone.setOutput("back") etc

I just want the player name, and maybe print the player name on the screen for debug purposes

Thanks in advance!
KingofGamesYami #2
Posted 06 June 2016 - 02:11 PM
I would start by serializing the table and printing it to the screen, like so:


local sensor = peripheral.wrap( "top" ) --#local variables are good practice
print( textutils.serialize( sensor.getPlayers() ) )

That will give you a visual representation of the table, from which you can determine how to access the data you want.
Shivy011 #3
Posted 06 June 2016 - 02:33 PM
Thanks

I have uuid = "asodhsadf whatever"
name = "Shivy011"

however - how do I extract this "name" and do an if statement with it?
KingofGamesYami #4
Posted 06 June 2016 - 02:38 PM
I assume it looks like this:


{
  {
    uuid="stuffs",
    name="Shivy011",
  }
}

In which case you would do this:

local sensor = peripheral.wrap( "top" )
for k, v in pairs( sensor.getPlayers() ) do --#iterate through the first table, which can contain multiple players
  if v.name == "Shivy011" ) then --#if the name of the player in the current iteration is yours
    --#do stuff
  end
end
Shivy011 #5
Posted 06 June 2016 - 02:43 PM
I have just done;
  • local sensor = peripheral.wrap("top")
  • print(textutils.serialize(sensor.getPlayers()))
  • for k, v in pairs(sensor.getPlayers()) do
  • if v.name = "Shivy011" then
  • redstone.setOutput("back", true)
  • sleep(5)
  • end
  • end

i get

bios:14: [string "sensor"]:4: "then" expected
Bomb Bloke #6
Posted 06 June 2016 - 03:00 PM
Use = for assignments, and == for comparisons. If you stick an assignment operator into the middle of an "if"'s conditional statement, you'll get complaints.
Shivy011 #7
Posted 06 June 2016 - 03:11 PM
Don't worry, it's all working

Thank so much for your help!
Edited on 06 June 2016 - 01:26 PM
Shivy011 #8
Posted 06 June 2016 - 06:25 PM
Another issue

Code: http://pastebin.com/rxkuJvuj


If more than one player is around, the program doesn't open the door (i.e. me and another player.) To be honest I have no idea how to fix this!

Any ideas?
KingofGamesYami #9
Posted 06 June 2016 - 11:23 PM
I'm not sure why that would happen. It should find you if you're within range.

The only issue I see is the program doesn't yield if nobody is in range, and would be killed by CC's yield protection.
Shivy011 #10
Posted 06 June 2016 - 11:38 PM
Hm, I am also finding that the redstone output gets stuck on a LOT and it just doesn't work properly. I think it may be to do with chunk loading, though the chunk the computer in should be loaded when I am testing it…

Could be a server issue?
Emma #11
Posted 06 June 2016 - 11:44 PM
What is happening is that when you are there, it detects you and sets the redstone output to true to open the door, as expected. However, if there are two people, it will open the door, because it detected you, but then the loop continues and sees that someone else is there, it goes into the else part of the if statement because that person is not you. In the else block, you set the redstone output to false, closing the door.
Recap:
Sees you, opens door
Sees your friend, closes door
So it appears that it doesn't work at all, it does, just a little flaw in your logic.

To fix this, you need to stop the 'for' loop if it detects you.
Easy to do this, in your if statement, after you print, put a line that just says 'break'


if v.name == "Shivy011" then
	 redstone.setOutput("left", true)
	 sleep(2)
	 redstone.setOutput("left", false)
	 print("Shivy011 is here")
	 break ---This line here is new
  else

Sidenote: What is the 'random' variable for?
Edited on 07 June 2016 - 01:22 PM
Dragon53535 #12
Posted 07 June 2016 - 01:12 AM
Incinerate you are incorrect, he has a sleep after the rs.setOutput so that it does update. Also, the break keyword will completely make it worse.

What I suspect the issue is that the other unauthorized player is getting detected first, and thus it's turning off the redstone, and then you get detected and it tries to turn it on, but it was already told to turn it off. I would add a sleep after the turning off the redstone in the incorrect part, as well as at the end of the while true loop as that's probably crashing your program.

So

	  print("Shivy001 not here")
	  sleep()
    end
  end
  sleep()
end
Edited on 06 June 2016 - 11:13 PM
Emma #13
Posted 07 June 2016 - 02:29 AM
Incinerate you are incorrect, he has a sleep after the rs.setOutput so that it does update. Also, the break keyword will completely make it worse.

What I suspect the issue is that the other unauthorized player is getting detected first, and thus it's turning off the redstone, and then you get detected and it tries to turn it on, but it was already told to turn it off. I would add a sleep after the turning off the redstone in the incorrect part, as well as at the end of the while true loop as that's probably crashing your program.

So

	  print("Shivy001 not here")
	  sleep()
	end
  end
  sleep()
end

/offtopic

I saw the sleep a couple minutes after I posted it but I had to leave for dinner and could not change it, your solution might work, although it is confusing to me how the rs api would not override after being told to turn back on, maybe an issue with tick delay? Idk. Thanks for correcting me though!

Edit: Although my comment about adding the 'break' token, is not wrong. It would help for efficiencies sake.
Edited on 07 June 2016 - 12:30 AM
Bomb Bloke #14
Posted 07 June 2016 - 03:25 AM
If more than one player is around, the program doesn't open the door (i.e. me and another player.) To be honest I have no idea how to fix this!

Does it still print "Shivy011 is here"?

Personally I'd write the code along these lines:

local sensor = peripheral.wrap("right")

while true do
	local found = false
	
	for k, v in pairs(sensor.getPlayers()) do
		if v.name == "Shivy011" then
			found = true
			break
		end
	end
	
	redstone.setOutput("left", found)
	print(found and "Shivy011 is here" or "Shivy011 not here")
	
	sleep(2)
end

This way, the door should simply stay open so long as you're around, instead of constantly opening / closing all the time.