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

[OpenPeripheralAddons] Sensor help?

Started by billbertking1, 14 August 2016 - 11:07 PM
billbertking1 #1
Posted 15 August 2016 - 01:07 AM
I'm trying to detect if a player is at a certain location, and if they are, disable a redstone signal. When they're not, the redstone signal is true. I've been looking online for the past hour or so and can't find anything.
Diagram if that doesn't make sense


Thank you.
Emma #2
Posted 15 August 2016 - 07:07 AM
The OpenPeripharalsAddons sensor can get relative position data of nearby players. It can also get all nearby players in case you want it to work for anybody.

This will get all nearby players:

local sensor = peripheral.wrap("top") -- The peripheral doesn't have to be on the top, I'm just using the top side as an example
local players = sensor.getPlayers()

It returns a table with this structure:

{
  {
	uuid = "4afdca7f-0fed-456b-9215-b74e6f0a1a15",
	name = "examplePlayer"
  },
  {
	uuid = "b63b9bda-8d65-48e1-9b22-381cd0ef3e04",
	name = "anotherPlayer"
  }
}

You can then get the position information of each respective player by using something like this

local position = sensor.getPlayerByName("examplePlayer").basic().position
-- OR you can use their UUID
local position = sensor.getPlayerByUUID("4afdca7f-0fed-456b-9215-b74e6f0a1a15").basic().position

Obviously, if you are doing all nearby players you wouldn't hard-code like that, but you get the idea.

Also, this is what the content of position might look like

{
  x = 2.510653,
  y = -1,
  z = 0.542056
}

Don't forget that the contents of the position table are relative to the sensor's position.

Also, you probably want to check whether the player is close enough to the block you are checking if they are in, so you would perform some sort of distance threshold calculation to check for that. The following is an example of what I mean by that:

local position = sensor.getPlayerByName("examplePlayer").basic().position

-- Let's say we want the player to be more or less 2 blocks above the sensor
local target = {x = 0, y = 2, z = 0}

-- We're going to use the 3d distance formula to calculate the distance from the player to the target position
local distance = math.sqrt(
  (target.x - position.x)^2 +
  (target.y - position.y)^2 +
  (target.z - position.z)^2 )

if distance <= 0.5 then -- So if the player is less than or is half a block from the target
  redstone.setOutput("back", false) -- Again, back could be any side
else -- Or else, the player is more than half a block from the target, so that doesn't count
  redstone.setOutput("back", true)
end

Hope I helped!
Edited on 15 August 2016 - 05:21 AM
billbertking1 #3
Posted 15 August 2016 - 06:49 PM
Awesome, got it work! Kind of.

Saying it's going too long without yielding on line 18, which is
redstone.setOutput("top", true)

I put in a while true do and end to make it run continuously.


Now I got it to work right. Changed where the while true do was. Thank you very much!
Edited on 15 August 2016 - 05:00 PM