Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
scienceguy8

Tracking Specific Item Amounts in AE2

Question

Forgive me if I'm making some obvious mistakes, but I've only just started playing with Lua about 6 hours ago.  Had some C/C++ classes in college years ago, but still.

I'm playing on a 1.7.10 single player world with OpenComputers 1.6.0.9 (Feed The Beast Infinity with a couple extra mods on top).  I use level emitters to turn on and off machines and processes in my base.  Say, I need more of a particular Thaumcraft essentia (stored using Thaumic Energistics).  The level emitter goes high, a machine starts up and produces the essentia until there is enough, then the emitter goes low again.  Works great, but having all these level emitters around is driving me nuts and eating up valuable AE channels.  So, I want to use a single computer with an Interface on a ME Controller and a Redstone I/O where it is needed to act as 8 to 16 (or more) level emitters.

 

Sudo code follows.

while stop=0

stop=redstone input (as soon as a redstone input is applied to the computer, break loop)

essentiatable=getEssentiaInNetwork()

extract data about essentiaType1 from essentiatable, write to variable Type1

if Type1>some value, then emit redstoneBundle0 true, otherwise redstoneBundle0 false

extract data about essentiaType2 from essentiatable, write to variable Type2

if Type2>some value, then emit redstoneBundle1 true, otherwise redstoneBundle1 false

...

extract data about essentiaType16 from essentiatable, write to variable Type16

if Type16>some value, then emit redstoneBundle15 true, otherwise redstoneBundle15 false

close while loop, return to top

 

My problem is I can't wrap my head around tables.  I've tried executing print(component.me_controller.getItemsInNetwork()) a couple times on a static network (no items in or out), and get back a 16 digit string of 16 bit numbers.  Each time I run it, I get back a slightly different number.  I don't know how to decode it.  I don't know how to extract the data I need from it.  Because it is changing, even though the stacks of items in the network aren't, I can't quite be sure that it is even working correctly.  So how do you turn a table into usable information?  How do I make it readable?  Is there a function that easily takes a table and cuts it down to just the relevant parts you want?

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

The Lua table can't be just printed -- you'll get the address of the table in memory. Fortunately, there's the serialization library that can serialize a table (convert it into a string).

The function to use is serialization.serialize(tbl: table). Or serialization.serialize(tbl: table, math.huge) that will make the returned string way more readable.

Before you can use the library in your script, you need to require() the library.

local component = require("component")
local serialization = require("serialization")

print(serialization.serialize(component.me_controller.getItemsInNetwork(), math.huge))

But there's an easier way. In Lua interpreter (the lua program), you can put = into the beginning of the line to pretty-print the return value. Also the interpreter automatically require()'s all libraries, so you don't need to do it yourself.

=component.me_controller.getItemsInNetwork()

 

Link to post
Share on other sites
  • 0

Since you have probably done arrays in your C/C++ tables can be explained like this: there are 2 types of tables, numerically indexed table which is like an array (except you start at array[1] rather than array[0]) and there is the hash table which contains 2 values, the key which is used to look up the value, this can be a a number, a string, any type except another table I think. Luckily for you getItemsInNetwork returns this type, so you can just loop from 1 to #essentiatable to get essentiatable. The itemstack values inside the array are hash tables though, you can think of them like structs/classes in C/C++ in this case. They have a specific format and the easiest way to find that format is just by doing what Fingercomp stated above:

=component.me_controller.getItemsInNetwork()[1]

I'm pretty sure it has a field named "name" so as an example if you wanted to check if the item is cobblestone then you'd do

for i = 1,#essentiatable do
  local item = essentiatable[i]
  if item.name == "minecraft:cobblestone" then
    print(serialization.serialize(item)
  end
end

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.