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

Help for a total newbie with AE, tables, library, folders

Question

Hi,

 

i have a program in CC, but it is not working at the moment because CC has some issues with bundled cabels and SFM does not want to interact with EnderIO redstone conduit. 

 

So i ve change to OC. After 10 hour of looking throw and trying to learn how to do the code properly i have some basic things how they work, or at least i think that i do. 

 

My question. can sombody tell me or show me  simple code how to call table in difrent program ?

 

How to create folder on hardisk and then create programs in that folder ?

(i ve use filesystem.makeDirectory(Stock/Stock) as my HDD is labeled Stock , it made a folder Stock but when i use inside the stock edit ***.lua then save it and then used command ls in Stock it showed all program which are in main drive after instaling openOS.)

 

How to add path to packed.path ? 

 

I will post the program from pastbin so you can see what i am trying to do. I would like to recreate the same on OC. so far i was not able to do so.

 

Thank you 

 

 this is the API 

keeplist = {
--Pure Fluix Dust
[1] = { ["id"] = "appliedenergistics2:item.ItemMultiMaterial",
        ["dmg"]= 8,
        ["nbt_has"] = nil,
        ["color"]  = colors.purple,
        ["keepqty"] = 1024,
                   },                   
-- Pure Certus Quartz                   
[2] = { ["id"] = "appliedenergistics2:item.ItemMultiMaterial",
        ["dmg"] = 10,
        ["nbt_hash"] = nil,
        ["color"] = colors.cyan,
        ["keepqty"] = 2048,
              },

-- Pure Nether Quartz
[3] = { ["id"] = "appliedenergistics2:item.ItemMultiMaterial",
        ["dmg"]= 11,
        ["nbt_hash"] = nil,
        ["color"] = colors.white,
        ["keepqty"] = 2048,
        },                                     

-- Pure Fluix Quartz                   
[4] = { ["id"] = "appliedenergistics2:item.ItemMultiMaterial",
        ["dmg"]= 12,
        ["nbt_hash"] = nil,
        ["color"] = colors.pink,
        ["keepqty"] = 2048,
        },
-- Charged Certus quartz
[5] = { ["id"] = "appliedenergistics2:item.ItemMultiMaterial",
        ["dmg"]= 1,
        ["nbt_hash"]= nil,
        ["color"] = colors.lime,
        ["keepqty"] = 2048,
        },

              
}

And this is the main program.

os.loadAPI("keeplistAPI")
rs.setBundledOutput("bottom",0)


local aeSide = "right" -- side where is interface connected
local bunSide = "bottom" -- side where bundled cabel is connected
local ae = peripheral.wrap(aeSide)

function findUpdated(keepItem)
  local newlist = ae.getAvailableItems()
 
  for number,item in pairs(newlist) do 
    if    keepItem["id"] == item.fingerprint.id
      and keepItem["dmg"]== item.fingerprint.dmg
      and keepItem["nbt_hash"] == item.fingerprint.nbt_hash  
      then lvlStock(keepItem["keepqty"], item.size, keepItem["color"])
    end  
  end
end

function lvlStock( kItemQty , aeItemQty , barva)

    if kItemQty <= aeItemQty   then rs.setBundledOutput(bunSide,colors.subtract(rs.getBundledOutput(bunSide),barva))
                                    print("True")
                               else rs.setBundledOutput(bunSide,colors.combine(rs.getBundledOutput(bunSide),barva)) 
                                    print("False")  
     end
end    
    
while true do 
 for i=1,#keeplistAPI.keeplist do 
   findUpdated(keeplistAPI.keeplist[i])

 end
sleep(5)
end 

 

Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 0

External libraries/packages are loaded using the require function, for example require("myAPI") loads the file myAPI.lua. To find libraries, require looks through a few different folders and uses the first file it finds. Your own libraries should go under /usr/lib, OpenOS's own libraries are under /lib, and a few libraries (like math or computer) are provided by Lua or the mod and are loaded "by magic".

 

To write a library that can be loaded using require, it's important that you return something from your library (usually a table). The return value of the library is what require passes back to your main program. For example, your library might look like this:

local mylib = {}

function mylib.foo()
	-- do something
end

function mylib.bar(a, b, c)
	-- do something else
end

return mylib

If you save this file as /usr/lib/mylib.lua, you can load it from another script like this:

local mylib = require("mylib")

And then you can use the functions foo and bar that your library placed into the mylib table.

Link to post
Share on other sites
  • 0

So basickly you didnt helped me very much ive seen this at leas 20x times. I even posted my prgram so you can see what i am trying to do. So you could use to explain it on some example from that program not from soem generic post.  
 
So if i am right then if i will write this 

 local  thisTable = {}
 
thisTable.1 = {  ["id"]= "this is my id",
                         ["dmg"] = 15,
                     },
thisTable.2 = { ["id"] = "next id",
                        ["dmg"] = 22,
                      },
 
return thisTable

Then how i will call the value of dmg in thisTable.2 ?

 

Next is nice that my lib are saved somewhere but i would like to save it to specifick folder togehtr with main program. 

Then i found that if zou edit packed.lua in bin filder or something like that that you can add where the openOS will look for the library.

 

Also did you even looked at what i was doing in the program ? or you just posted generic answer?

 

Thank you.

Link to post
Share on other sites
  • 0

Your example library translation looks fine except for

thisTable.1 = ...

which should be

thisTable[1] = ...

You can add a custom location to the package searching path. You just have to modify the variable package.path. (i.e. by appending ";/myProgram/?.lua")
But since this is looking more like a simple configuration file using the function dofile is another option.

package.path = package.path .. ";/myProgram/?.lua" --This is just a quick and dirty example. You should only append this string once - i.e. when the computer is starting - and not every time you start your program.
local keeplistAPI = require("keeplistAPI")
print(keeplistAPI[1].dmg)

vs.

local keeplistAPI = dofile("/myProgram/keeplistAPI.lua")
print(keeplistAPI[1].dmg)

There are a lot of other things that need some work.

Like the fact that OC has got a different API for component interaction:

local component = require("component")
local sides = require("sides")
local rs = component.redstone --alternatively you could use component.get("address-of-your-component")
rs.setOutput(sides.left, 15)
Link to post
Share on other sites
  • 0

Thank you, 

 

I know about diffrent calling for the apis as components and i kind of understand that. 

And thank you for that table now i know how to do it. 

 

And that dofile completly forgot that there is something like that. 

 

But still i am not able to make working the creation of folder on HDD and putting the programs into the forlder. 

As i mention i was able to use the command " filesystem.makeDirectory"   but if i use cd to get into the folder , if it is folder, and then i do edit Stock inside that folder it adds program Stock but not into that folder. It just puts it togherther with the rest on the HDD. 

 

And i was still not able to make it work. 

 

thank you. 

Link to post
Share on other sites
  • 0

You want to do it manually in the shell?

/> mkdir myProgram        #create a directory
/> cd myProgram           #move to the newly creaeted directory
/myProgram> edit test.lua #edit a file in the directory
                          #Attention: /test.lua would refer to a file at the root directory Was that your error?

Or did you try to write lua code? Then it would be good to see what you have to help debugging.

Link to post
Share on other sites
  • 0

ohh now i get it. i did not remember mkdir.

 

I was ruuning the command (filesystem.makeDirectory) from lua becase that was the only place where it worked. 

 

I will post result of my codding soon. At least i think, because now i should have everything i need. 

 

Thank you. 

Link to post
Share on other sites
  • 0

Thank you for a great help. 
 
Here is the result and it is working. 
 
keeplistAPI.lua http://pastebin.com/rfDsM1hQ

Stock.lua (main program) http://pastebin.com/xk3SFnZZ

 

 

Here you have a code so you dont need to go to pastebin. 

keeplistAPI.lua

local keeplist ={}
local colors = require("colors")


--Pure Fluix Dust
keeplist[1] = { ["id"] = "appliedenergistics2:item.ItemMultiMaterial",
                 ["dmg"]= 8,
                 ["nbt_has"] = nil,
                 ["color"]  = colors.purple,
                 ["keepqty"] = 1024,
                   }                   
-- Pure Certus Quartz                   
keeplist[2] = {   ["id"] = "appliedenergistics2:item.ItemMultiMaterial",
                  ["dmg"] = 10,
                  ["nbt_hash"] = nil,
                  ["color"] = colors.cyan,
                  ["keepqty"] = 2048,
              }

-- Pure Nether Quartz
keeplist[3] = {  ["id"] = "appliedenergistics2:item.ItemMultiMaterial",
                  ["dmg"]= 11,
                  ["nbt_hash"] = nil,
                  ["color"] = colors.white,
                  ["keepqty"] = 2048,
        }                                     

-- Pure Fluix Quartz                   
keeplist[4] = {  ["id"] = "appliedenergistics2:item.ItemMultiMaterial",
                  ["dmg"]= 12,
                  ["nbt_hash"] = nil,
                  ["color"] = colors.pink,
                  ["keepqty"] = 2048,
        }
-- Charged Certus quartz
keeplist[5] = {  ["id"] = "appliedenergistics2:item.ItemMultiMaterial",
                  ["dmg"]= 1,
                  ["nbt_hash"]= nil,
                  ["color"] = colors.lime,
                  ["keepqty"] = 2048,
        }

              
return keeplist

Stock.lua

local keeplistAPI = dofile("/Stockprg/keeplistAPI.lua")
local comp   = require("component")
local sides  = require("sides")
local colors = require("colors")
local rs     = comp.redstone


local bunSide = sides.left -- side where bundled cabel is connected
-- //local ae = comp.get("2904f916-5266-443d-b392-41eae14f30c")
local ae = comp.me_interface
function findUpdated(keepItem)
  local newlist = ae.getAvailableItems()
 
  for number,item in pairs(newlist) do 
    if    keepItem["id"] == item.fingerprint.id
      and keepItem["dmg"]== item.fingerprint.dmg
      and keepItem["nbt_hash"] == item.fingerprint.nbt_hash  
      then lvlStock(keepItem["keepqty"], item.size, keepItem["color"])
    end  
  end
end

function lvlStock( kItemQty , aeItemQty , barva)

    if kItemQty <= aeItemQty   then rs.setBundledOutput(bunSide,barva,0)
                                    print("True")
                               else rs.setBundledOutput(bunSide,barva,255) 
                                    print("False")  
     end
end    
 
rs.setBundledOutput(bunSide,{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0})
    
while true do 
 for i=1,#keeplistAPI do 
   findUpdated(keeplistAPI[i])

 end
os.sleep(5)
end 

 

If you have any questions feel free to ask. If i know the answer i will be happy to help. 

 

Advices are welcomed. I am expecting some advice on better coding. But i am just a noob in this. 

 

Thank you.

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.