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

ccSensors Help

Started by matt2887, 06 July 2014 - 03:45 AM
matt2887 #1
Posted 06 July 2014 - 05:45 AM
I am attempting to use ccSensors to detect a player, because I want to reference said player's name in a program. My goal is to have a computer output a redstone signal when either my friend or I come close to the sensor. I am using CC CraftOS 1.3. All I could find on the ccSensors version was 'version: MC1.2.5 Build017pr1'. The code I have I'm using to attempt to find a way to reference the player name from the probe data.

os.unloadAPI("sensors")
os.loadAPI("/rom/apis/sensors")
function printDict(data)
  for i,v in pairs(data) do
	print(tostring(i).." - "..tostring(v))
  end
end
data = sensors.getSensorReadingAsDict("left", "playerSensor", "vq", "Players.name")
print(data)
The problem i'm running into right now is it's returning a table, not a dictionary. It returns the value 'table: 1b0782c7'. Is there any way to convert this table to a word, for example the player's name.
Also, going one step beyond this. How would I reference this in the program to open a door based on what player is around. A baseline for my code would be something like this. But I still have no clue how to reference the probe for player's name because it's returning a table.

local side = "left"
local opentime = 5
while true do
trusted = matt2887, Agnistu
  if sensors.getSensorReadingAsDict("left", "playerSensor", "vq", "Players.name") == trusted then
	rs.setOutput(side, true)
	sleep(opentime)
	rs.setOutput(side, false)
  end
end
Edited on 06 July 2014 - 03:48 AM
matt2887 #2
Posted 06 July 2014 - 05:58 AM
Update, the table value seems to change everytime I run 'sensors.getSensorReadingAsDict("left", "playerSensor", "vq", "Players.name")' in the lua prompt in the computer. I assume the player.name is being stored as a different table value, or is there something else I'm doing wrong lol?
Bomb Bloke #3
Posted 06 July 2014 - 06:17 AM
A table is a collection of values. It doesn't so much represent anything so much as it represents a folder you can stick things into.

The sensor seems to be returning a number of values at once, and so it dumps these into a table for you to browse through. Yes, it makes a new table every time.

Your "printDict()" function is specifically rigged to accept a table, and print out the different values that're inside it. A "dictionary" isn't a "real" data type - that's just the name that someone's apparently decided to apply to the tables the sensor is returning. That function will work on most any table.

In your first code snippet, try changing the line that reads "print(data)" to "printDict(data)". What's that give you?
matt2887 #4
Posted 06 July 2014 - 06:51 AM
Oh, I forgot to mention that part, when I have it like this it just goes to the next line in the computer without doing anything at all.
Spoiler

os.unloadAPI("sensors")
os.loadAPI("/rom/apis/sensors")
function printDict(data)
  for i,v in pairs(data) do
        print(tostring(i).." - "..tostring(v))
  end
end
data = sensors.getSensorReadingAsDict("left", "playerSensor", "vq", "Players.name")
printDict(data)

Also when I tried it as instructed in a video I watched I got an error 'sensorTest:4: bad argument: table expected, got nil'

video link; https://www.youtube.com/watch?v=34R4KfLmMSY#t=1470

Spoiler

os.unloadAPI("sensors")
os.loadAPI("/rom/apis/sensors")
function printDict(data)
  for i,v in pairs(data) do
	    print(tostring(i).." - "..tostring(v))
  end
end
data = sensors.getSensorReadingAsDict("left", "playerSensor", "vq", "Players")
printDict(data.name)
Edited on 06 July 2014 - 04:48 AM
Bomb Bloke #5
Posted 06 July 2014 - 12:33 PM
Gotta confess that I've minimal interest in watching videos.

It sounds like sensors.getSensorReadingAsDict() is returning an empty table - that is to say, you're getting a "result" because you're asking for one, but the sensor isn't picking anything up when it goes to fetch it so said result doesn't have any info in it. Printing out the "dictionary" hence doesn't do much.

This likely has something to do with those parameters you're passing to the API call. "vq" may not be a valid target, or "Players"/"Players.name" may not be valid "readings" for that target.

Either I'm missing something, or ccSensors has rather poor documentation. But reading through the source of the API involved, it looks like running this should give you something of value:

print("Targets:")
printDict(sensors.getAvailableTargets("left", "playerSensor"))
print("\nReadings:")
printDict(sensors.getAvailableReadings("left", "playerSensor"))
matt2887 #6
Posted 07 July 2014 - 06:56 PM

print("\nReadings:")
printDict(sensors.getAvailableReadings("left", "playerSensor"))
Returns a list,
1 - TargetInfo
2 - LivingEntities
3 - Players
4 - Animals

print("Targets:")
printDict(sensors.getAvailableTargets("left", "playerSensor"))
returns
"Targets: " and that's it :/
Bomb Bloke #7
Posted 08 July 2014 - 01:02 AM
Ok, maybe give this a try:

sensors.setActiveReading("left", "playerSensor", "Players")
printDict(sensors.getReading("left", "playerSensor"))