Jump to content

OpenPeripierals- Example of working sensor program

peripheral api

4 replies to this topic

#1 gheotic

  • Members
  • 64 posts
  • LocationDenmark

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.

#2 Lupus590

  • Members
  • 2,011 posts
  • LocationUK

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.


#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 October 2015 - 11:57 AM

See here for an explanation of tables and how to get at their contents.

#4 valithor

  • Members
  • 1,053 posts

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.

Slow phone typing ninja lol.

Edited by valithor, 26 October 2015 - 12:00 PM.

#5 SquidDev

    Frickin' laser beams

  • Members
  • 1,425 posts
  • LocationDoes anyone put something serious here?

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))
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.

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users