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

OpenPeripierals- Example of working sensor program

Started by gheotic, 26 October 2015 - 09:07 AM
gheotic #1
Posted 26 October 2015 - 10:07 AM
I have got the direwolf20 1.7.1, with computercraft and openperipherals with the block called sensor. What i want to know is how to actually use it. I have spent a few hours trying to do a simple script, without luck. I have tried other user scripts, none which worked… When i use any of the functions it returns somthing like this: "1: table: (anumber)".
Can someone help me the right direction? mabey just a simple working script.
Thank you.
Lupus590 #2
Posted 26 October 2015 - 11:44 AM
It's not to do with open peripherals but it sounds like you will still learn from this.

http://www.computercraft.info/forums2/index.php?/topic/15062-peripheral-basics/
Bomb Bloke #3
Posted 26 October 2015 - 11:57 AM
See here for an explanation of tables and how to get at their contents.
valithor #4
Posted 26 October 2015 - 12:00 PM
It should also be noted that, if this is the same peripheral mod as I am thinking, almost all of the functions the peripheral returns is a table. might be worth trying to use textutils.serialize on what the function returns before printing.

Posted via phone.

Edit
Slow phone typing ninja lol.
Edited on 26 October 2015 - 11:00 AM
SquidDev #5
Posted 26 October 2015 - 12:10 PM
For OpenPeripheral I normally resort to reading the source code for whatever I want to use. (However you can do use listMethods, doc and getAdvancedMethodsData to get some documentation for peripherals). In the case of the sensor it provides a method for describing nearby entities (5 block radius).

The basic usage would be something like this:

local p = peripheral.wrap("left") -- Presuming the sensor is on the left
for _, id in ipairs(p.getEntityIds("mob")) do -- Or just p.getMobIds() -- Can also use "minecart" and "item"
  print("Entity: " .. id)
  for k, v in pairs(p.getEntityData(id, "mob")) do
     print(k .. ": " .. tostring(v))
  end
end
This should just print out a load of properties about each entity. I haven't actually tested this.

You can also use p.getPlayers(), p.getPlayerByName(name) and p.getPlayerByUUID(name) should you want to deal with players instead.