KapitanWalnut

openPeripherals Tables Notes - NOT CODE

Mar 24th, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.18 KB | None | 0 0
  1. --CODE NOT MEANT TO RUN, JUST EXAMPLE SNIPPETS
  2. --for things which return 1 table (like sensor):
  3. local tile = peripheral.wrap("some_tile")
  4. for k,v in pairs(tile) do
  5.   print(k)
  6. end
  7.  
  8. --for things which return nested tables (like tanks):
  9. tank = peripheral.wrap("back") --or w/e side your thing is on
  10. results = tank.getTankInfo("back") --"unknown" will also work here
  11. for k,_ in pairs(results) do
  12.     for i,v in pairs(results[k]) do
  13.         print(i..v)
  14.     end
  15. end
  16.  
  17. --for most tanks, everything will be in the first nested table, so you can:
  18. tank = peripheral.wrap("back")
  19. tankInfo = tank.getTankInfo("unknown")[1] --the [1] access the first table so you don't have to later
  20.  
  21. local fluidAmount = tankInfo.amount
  22. --if no fluid, this will return nil.
  23. --Can't do arithmetic with nil, so check if it is a nil (nil = false) and if so, set to 0
  24. if not fluidAmount then
  25.     fluidAmount = 0
  26. end
  27. print("Amount: ", fluidAmount)
  28.  
  29. local tankCapacity = tankInfo.capacity
  30. --will always return a capacity if there is a valid tank so no correction needed
  31. print("Capacity: ", tankCapacity)
  32.  
  33. local percent = math.round(fluidAmount/tankCapacity*100, 1) --round to nearest decimal
  34. print("Percent Filled: ", percent)
Advertisement
Add Comment
Please, Sign In to add comment