Advertisement
xo33ab

how to call nested table

Nov 25th, 2023 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.86 KB | None | 0 0
  1. local t = {
  2.     ["purple"] = {
  3.         side = "left",
  4.         flux_level = 1,
  5.         flux_max = 7
  6.     },
  7.  
  8.     ["lime"] = {
  9.         side = "back",
  10.         flux_level = 1,
  11.         flux_max = 7
  12.     },
  13.  
  14.     ["orange"] = {
  15.         side = "right",
  16.         flux_level = 1,
  17.         flux_max = 7
  18.     },
  19.  
  20.     ["white"] = {
  21.         side = "front",
  22.         flux_level = 1,
  23.         flux_max = 5
  24.     }
  25. }
  26.  
  27. ----------------------------
  28. --Prints Table for debugging
  29. ----------------------------
  30. local function printTable(e)
  31.     if type(e) == "table" then
  32.         for k,v in pairs(e) do -- for every element in the table
  33.             print(k,v)
  34.             printTable(v)       -- recursively repeat the same procedure
  35.             sleep(2)
  36.             print()
  37.  
  38.         end
  39.     else -- if not, we can just print it
  40.         print(e)
  41.     end
  42. end
  43.  
  44. ---------------------------
  45. --Updates table when redstone changes
  46. ---------------------------
  47. local function getUpdate()
  48.     t.purple.flux_level = rs.getAnalogInput(t.purple.side)
  49.     t.lime.flux_level = rs.getAnalogInput(t.lime.side)
  50.     t.orange.flux_level = rs.getAnalogInput(t.orange.side)
  51.     t.white.flux_level = rs.getAnalogInput(t.white.side)
  52.     print("Updated")
  53.     printTable(t)
  54.     sleep(1)
  55. end
  56.  
  57. ---------------------------
  58. --Emits bundled redstone when flux_max is exceeded
  59. ---------------------------
  60. local function stopProduction(index)
  61.     if t.["..index.."].flux_level >= t.["..index.."].flux_max then
  62.         print("stopProduction")
  63.         rs.getBundledOutput(colours.index)
  64.     end
  65. end
  66.  
  67. ---------------------------
  68. --Starts the script
  69. ---------------------------
  70. local function start()
  71.     while true do
  72.         if os.pullEvent("redstone") then
  73.             --getUpdate() -- updates table
  74.             stopProduction(purple)
  75.         end
  76.     end
  77. end
  78.  
  79. start()
  80.  
  81.  
  82. stopProduction(purple)
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement