This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Noobular's profile picture

Chest/inventory Peripherals

Started by Noobular, 11 September 2014 - 02:41 AM
Noobular #1
Posted 11 September 2014 - 04:41 AM
I was curious if i would be able to use a peripheral to look at everything in the chest,
like everything listed out so i can put them all on a monitor to show everything i have, I know it's possible i've seen it done but i'd like to see if i'd be able to do it with single chest or an ME system…

Purpose: Making an automated shop and that's pretty much the last bit i need.


Any help would be greatly appreciated!!!
hilburn #2
Posted 11 September 2014 - 11:42 AM
If you have OpenPeripherals then yes, this is doable. However for "remote" chests (ones not adjacent so you are using modems) you will need to use the OpenP Peripheral Proxy block to connect to it. There are also methods to support AE systems, as well as automatically craft and pull items from it
Noobular #3
Posted 12 September 2014 - 01:33 AM
I was thinking maby trying to make it like a vending machine like setting them up around the areas of the map so i get more people purchasing and selling , then use Enderchest to set it to the specific machine..
Noobular #4
Posted 12 September 2014 - 05:33 AM
Oh, i didnt realize i didn't say it , i don't know exactly how to use the proxy block along with calling the getInventorySize and stuff like that , Peripheral Functions
hilburn #5
Posted 12 September 2014 - 12:27 PM
Point the Proxy at the chest, attach a wired modem to the back, right click it to activate. You will get something like "chest_0 connected" appear in chat. That s the peripheral "side" you should wrap to allow you to call the chest methods
Noobular #6
Posted 13 September 2014 - 09:23 PM
I cant seem to get any of the commands for them to work… I don't exactly know specifically how to make them connected like a monitor nor if the commands are being used right.
Noobular #7
Posted 13 September 2014 - 09:33 PM
The code is simply this.

local p = "me_drive_0"
p.getTotalItemBytes()

It constantly says it attempts to call nil
Am I supposed to be putting arguments inside the getTotalItemBytes()?
Dragon53535 #8
Posted 13 September 2014 - 11:41 PM
If it's saying attempt to call nil then that means that getTotalItemBytes does not exist, try using

for key, value in pairs(peripheral.getMethods("me_drive_0")) do
  print(key.." "..value)
end
to get the methods you can call
Dog #9
Posted 14 September 2014 - 12:11 AM
Noobular - you aren't actually wrapping the ME drive as a peripheral.

Change this…

local p = "me_drive_0"

to this…

local p = peripheral.wrap("me_drive_0")
Noobular #10
Posted 14 September 2014 - 01:33 AM
i tried that before and it still didnt work for what ever reason.

Also dragon, I grabbed someones Show Peripherals program so thats how i got it.
Dog #11
Posted 14 September 2014 - 01:50 AM
i tried that before and it still didnt work for what ever reason.
'for what ever reason' doesn't really help us help you - an exact error message would be very helpful. FWIW, what I suggested is the correct way to wrap a peripheral - what you were trying will not work. Are you sure the peripheral you are trying to wrap is actually named "me_drive_0" ?

Dragon may know who's 'Show Peripherals' program you used, but I don't. If correctly wrapping the device (with the correct name) still doesn't work, would you please run his code and tell us what is returned?
Edited on 13 September 2014 - 11:56 PM
Noobular #12
Posted 14 September 2014 - 02:10 AM
Well i guess it seems to be actually running the code without problems… How would i write out the Function: TABLENAME to a text? …
Noobular #13
Posted 14 September 2014 - 04:53 AM
This might help, basicly when i press the button and run the CheckItems() function it just doesn't think it exist.

Noobular #14
Posted 14 September 2014 - 05:05 AM
Crap… I took the picture of the code after the mess up , its actually line 70, Sorry.

After further inspection of the monitor , it writes it out but it just dies after it does…
Dog #15
Posted 14 September 2014 - 06:45 AM
line 70 is

m.write(slot.name)

I'm guessing that slot.name is not a valid variable name. That would result in the error you are seeing.
Bomb Bloke #16
Posted 14 September 2014 - 10:34 AM
Or more specifically, that "slot" isn't a table that can be indexed into.

Perhaps if you changed line 69 to check the existence of "slot" instead of "CurrSlot"…
Dog #17
Posted 14 September 2014 - 04:40 PM
Or more specifically, that "slot" isn't a table that can be indexed into.
Thank you for clarifying and correcting me, Bomb Bloke :)/>
Noobular #18
Posted 14 September 2014 - 09:32 PM
Actually… it turns out that when it is going over the catagory.getStackInSlot() it hits a nil then it stops it self, but i changed the code to

function CheckItems(s)
    local InvSize = Catagory.getInventorySize()
for CurrSlot = 1, InvSize-1 do
   local slot = C1.getStackInSlot(CurrSlot)
  if CurrSlot == nil then -- cycling
   m.write("Nothing")
   else
	 m.setCursorPos(1,CurrSlot+2)
   m.write("|"..slot.name)
   m.setCursorPos(25,CurrSlot+2)
   m.write(""..slot.id.."")
   m.setCursorPos(33,CurrSlot+2)
   m.write(""..slot.qty)
    end
end
end

but for some reason it still wont like stop error….
Dog #19
Posted 14 September 2014 - 09:43 PM
Actually… it turns out that when it is going over the catagory.getStackInSlot() it hits a nil then it stops it self, but i changed the code to

but for some reason it still wont like stop error….
You need to move your check for nil before your first invocation of the variable, like so…

if CurrSlot == nil then
  m.write("Nothing")
else
  local slot = C1.getStackInSlot(CurrSlot)
  m.setCursorPos(1,CurrSlot+2)
  m.write("|"..slot.name)
  --# rest of code
Edited on 14 September 2014 - 07:43 PM
Noobular #20
Posted 14 September 2014 - 11:38 PM

Current Code :


It's still just ending it self basicly once it hits an empty slot.
Bomb Bloke #21
Posted 15 September 2014 - 12:50 AM
You need to move your check for nil before your first invocation of the variable, like so…

What? No! CurrSlot will never be nil. There's no point in checking it.

It's a counter variable used by a "for" loop. On the first iteration of that loop, it'll be 1. On the last, it'll be InvSize-1. On every other iteration, it'll be an integral number somewhere in between.

"slot", on the other hand, will be nil whenever you attempt to call C1.getStackInSlot(CurrSlot) and the value of CurrSlot points to an empty slot.

Noobular, go back to the code you were using here and change line 69 to check "slot" instead of "CurrSlot".
Dog #22
Posted 15 September 2014 - 02:29 AM
D'oh, talk about not seeing the forest for the trees - slightly embarrassed that I missed something so obvious. Thanks, yet again, Bomb Bloke.
Noobular #23
Posted 15 September 2014 - 05:38 AM
D'oh, talk about not seeing the forest for the trees - slightly embarrassed that I missed something so obvious. Thanks, yet again, Bomb Bloke.

Pro's helping Pro's lol , I feel the same way just noticing that

I used the code from a video trying to figure out how to use the methods. that's why i didnt understand it lol.
Noobular #24
Posted 15 September 2014 - 05:43 AM
Thank you all for helping :)/>
Noobular #25
Posted 15 September 2014 - 05:54 AM
Now on to trying to figure out how i can make the listed items, add together so its not a long list of "cobble stone 64" its just "Cobble stone 426"
sjonky #26
Posted 15 September 2014 - 06:29 AM
You can read all the chest contents into a table, and create the table index as item names. Then when adding a new slot, you check if the item name index already exists add the current qty to the table qty, if it doesn't exist create a new table index. Then when you went through everything, you print your table to the screen instead.
Noobular #27
Posted 15 September 2014 - 06:43 AM
That's what I wanted to do but i'm unsure of how to do it correctly..
Dog #28
Posted 15 September 2014 - 07:36 AM
While this isn't based on your code, the way I would approach the problem would be something like this…

itemList is the master list of all cataloged items
newItem is the table holding the info for the current slot


for i = 1, #itemList do --# cycle through the master item list
  if newItem.name == itemList[i].name then --# if the new item's name matches the name of item [i] in the master list then...
    itemList[i].count = itemList[i].count + newItem.count --# add the count of the matching item to the total in the master list
    itemList[i].slots[#itemList[i].slots+1] = newItem.slotNumber --# store the slot number, so we know which slot(s) this item is in
    break --# break out of the loop because we're done and don't need to let the loop finish
  end
end
This probably isn't the best example, but it should give you an idea of how to keep a table of items with a running total
Edited on 15 September 2014 - 05:38 AM
Noobular #29
Posted 15 September 2014 - 07:55 AM
for the first line of that you do #itemList, what does the # refer to in lua?
Dog #30
Posted 15 September 2014 - 08:01 AM
# returns the number of items in a table, or the number of characters in a string. As I understand it, # doesn't drill down into sub-values/sub-tables, so it only counts the number of 'master' listings for the table or table entry you point it to.
Edited on 15 September 2014 - 06:04 AM
Noobular #31
Posted 15 September 2014 - 11:06 AM
Gosh.. i'm really confused on how to put that into what i have done..
Dog #32
Posted 15 September 2014 - 04:32 PM
Please post your code as it is now and we'll go from there.
Noobular #33
Posted 16 September 2014 - 08:18 PM
The giant spots are for me to click to since i use sublime text i see the words on the side for quick moving around.
But i think having it list everything from C1 - C6 in tables/table right when it loads up would be a good idea so when someone is navigating its not just murding the computer.


os.loadAPI("button")
m = peripheral.wrap("back")
C1 = peripheral.wrap("obsidian_10") -- Basic materials
C2 = peripheral.wrap("obsidian_11") -- Machines
C3 = peripheral.wrap("obsidian_12") -- Armor / Weapons
C4 = peripheral.wrap("obsidian_13") -- Misc
C5 = peripheral.wrap("obsidian_14") -- Extra
C6 = peripheral.wrap("obsidian_15") -- Extra
Catagory = C1
m.clear()
-- ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄
--▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
-- ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀
-- ▄			   ▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄   ▄		    ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄
--▐░▌			 ▐░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌ ▐░▌		  ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
-- ▐░▌		   ▐░▌ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌ ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌▐░▌		  ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀
--  ▐░▌		 ▐░▌  ▐░▌	   ▐░▌▐░▌	   ▐░▌	 ▐░▌	 ▐░▌	   ▐░▌▐░▌	   ▐░▌▐░▌		  ▐░▌		  ▐░▌		 
--   ▐░▌	   ▐░▌   ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌	 ▐░▌	 ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌▐░▌		  ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄
--    ▐░▌	 ▐░▌    ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌	 ▐░▌	 ▐░░░░░░░░░░░▌▐░░░░░░░░░░▌ ▐░▌		  ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
--	 ▐░▌   ▐░▌	 ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀█░█▀▀	  ▐░▌	 ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌▐░▌		  ▐░█▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀█░▌
--	  ▐░▌ ▐░▌	  ▐░▌	   ▐░▌▐░▌	 ▐░▌	   ▐░▌	 ▐░▌	   ▐░▌▐░▌	   ▐░▌▐░▌		  ▐░▌				    ▐░▌
--	   ▐░▐░▌	   ▐░▌	   ▐░▌▐░▌	  ▐░▌  ▄▄▄▄█░█▄▄▄▄ ▐░▌	   ▐░▌▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄█░▌
--	    ▐░▌	    ▐░▌	   ▐░▌▐░▌	   ▐░▌▐░░░░░░░░░░░▌▐░▌	   ▐░▌▐░░░░░░░░░░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
--		 ▀		  ▀		 ▀  ▀		 ▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀		 ▀  ▀▀▀▀▀▀▀▀▀▀   ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀
																														  
Vers = "[1.0]"
FirstButtonLength = 24
ItemValue = 5
ScreenVal = "Idle"

function header()
m.setTextColor(colors.red)
m.setBackgroundColor(colors.gray)
CText("*********************",15,1,colors.red,colors.gray)
CText("				    *",15,2,colors.red,colors.gray)
    CText("*Noobs Shop "..Vers,15,2,colors.red,colors.gray)
CText("				    *",15,3,colors.red,colors.gray)
    CText("*Currently: "..ScreenVal,15,3,colors.red,colors.gray)
CText("*********************",15,4,colors.red,colors.gray)
    m.setBackgroundColor(colors.black)
   
end
function CText(text,x,y,color,background)
m.setBackgroundColor(background)
m.setTextColor(color)
m.setCursorPos(x,y)
   m.write(text) 
end
-- ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄
--▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
-- ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀
-- ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄	    ▄  ▄▄▄▄▄▄▄▄▄▄▄
--▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░▌	  ▐░▌▐░░░░░░░░░░░▌
--▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀  ▀▀▀▀█░█▀▀▀▀  ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌░▌	 ▐░▌▐░█▀▀▀▀▀▀▀▀▀
--▐░▌		  ▐░▌		  ▐░▌			   ▐░▌		  ▐░▌	 ▐░▌	   ▐░▌▐░▌▐░▌    ▐░▌▐░▌		 
--▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌			   ▐░▌		  ▐░▌	 ▐░▌	   ▐░▌▐░▌ ▐░▌   ▐░▌▐░█▄▄▄▄▄▄▄▄▄
--▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌			   ▐░▌		  ▐░▌	 ▐░▌	   ▐░▌▐░▌  ▐░▌  ▐░▌▐░░░░░░░░░░░▌
-- ▀▀▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀ ▐░▌			   ▐░▌		  ▐░▌	 ▐░▌	   ▐░▌▐░▌   ▐░▌ ▐░▌ ▀▀▀▀▀▀▀▀▀█░▌
--		  ▐░▌▐░▌		  ▐░▌			   ▐░▌		  ▐░▌	 ▐░▌	   ▐░▌▐░▌    ▐░▌▐░▌		  ▐░▌
-- ▄▄▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄	  ▐░▌	  ▄▄▄▄█░█▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░▌	 ▐░▐░▌ ▄▄▄▄▄▄▄▄▄█░▌
--▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌	 ▐░▌	 ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌	  ▐░░▌▐░░░░░░░░░░░▌
-- ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀	   ▀	   ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀	    ▀▀  ▀▀▀▀▀▀▀▀▀▀▀
function CheckItems()
m.setBackgroundColor(colors.red)
   local InvSize = Catagory.getInventorySize()
for CurrSlot = 1, InvSize-1 do --Checking slots
   local slot = C1.getStackInSlot(CurrSlot)
  if slot ~= nil then -- Slots with stuff in them
   m.setBackgroundColor(colors.red)
   m.write(slot.name.."  QTY: "..slot.qty)
    end
end
end
function NewItem(itemname,itemvar,xmin,price) -- Constructor for Items
m.setTextColor(colors.red)
m.setBackgroundColor(colors.gray)
itemvar = 0
ItemYStart = ItemYStart + 1
ymax = 1 
xmax = 20
price = ("$"..price)
m.setTextColor(colors.black)
m.setCursorPos(xmin,ItemYStart)
    m.write("				    "..itemvar.." ("..price.."/Ea)")
    m.setTextColor(colors.red)
    m.setCursorPos(xmin,ItemYStart)
    m.write(itemname..": ")
end
function BuyBasicMaterialsList() -- All Basic Materials
NewItem("Sand",Sand,2,100)
NewItem("Sandstone",Sandstone,2,100)
NewItem("Cobble Stone",CobbleStone,2,100)
NewItem("Stone",Stone,2,100)
NewItem("Grass", Grass,2,100)
NewItem("Dirt",Dirt,2,100)
NewItem("Oak log",Olog,2,100)
NewItem("Spruce Log",Slog,2,100)
NewItem("Maple Log",Mlog,2,100)
NewItem("Jungle Log",Jlog,2,100)
NewItem("Wooden Planks",Planks,2,100)
end
function BuyMachinesList()
NewItem("MFSU",MFSU,2,100) 
end
function BuyArmorWeaponList()
  NewItem("Nano Chestplate",NanoChest,2,100)
end
function BuyMiscList()
  NewItem("Wooden Fence",WoodFence,2,100) 
end
-- ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄
--▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
-- ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀
-- ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄	    ▄  ▄▄▄▄▄▄▄▄▄▄▄
--▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░▌	  ▐░▌▐░░░░░░░░░░░▌
--▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░▌░▌	 ▐░▌▐░█▀▀▀▀▀▀▀▀▀
--▐░▌		  ▐░▌		  ▐░▌	   ▐░▌▐░▌		  ▐░▌		  ▐░▌▐░▌    ▐░▌▐░▌		 
--▐░█▄▄▄▄▄▄▄▄▄ ▐░▌		  ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░▌   ▐░▌▐░█▄▄▄▄▄▄▄▄▄
--▐░░░░░░░░░░░▌▐░▌		  ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌  ▐░▌  ▐░▌▐░░░░░░░░░░░▌
-- ▀▀▀▀▀▀▀▀▀█░▌▐░▌		  ▐░█▀▀▀▀█░█▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░▌   ▐░▌ ▐░▌ ▀▀▀▀▀▀▀▀▀█░▌
--		  ▐░▌▐░▌		  ▐░▌	 ▐░▌  ▐░▌		  ▐░▌		  ▐░▌    ▐░▌▐░▌		  ▐░▌
-- ▄▄▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░▌	  ▐░▌ ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌	 ▐░▐░▌ ▄▄▄▄▄▄▄▄▄█░▌
--▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌	   ▐░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌	  ▐░░▌▐░░░░░░░░░░░▌
-- ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀		 ▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀	    ▀▀  ▀▀▀▀▀▀▀▀▀▀▀
function TerminalScreen()
term.setCursorPos(1,1)
term.write("***************************************************")
term.setCursorPos(1,2)
term.write("***************************************************")
term.setCursorPos(1,7)
term.write("***************************************************")
term.setCursorPos(1,8)
term.write("***************************************************")
for val = 3,7 do
  term.setCursorPos(1,val)
  term.write("**")
  term.setCursorPos(50,val)
  term.write("**")
end
end
function TermUpdate()
term.setCursorPos(3,3)
term.write(" Rainbow Text Total: "..what)
term.setCursorPos(1,10)
end
function BuyScreen()
ItemYStart = ItemValue
m.setBackgroundColor(colors.black)
ScreenVal = "Buying"
button.clearTable()
m.setTextColor(colors.black)
m.setBackgroundColor(colors.black)
BuySections()
    back1()
    button.screen()
    header()
end
function SellScreen()
ItemYStart = ItemValue
ScreenVal = "Selling"
button.clearTable()
m.setTextColor(colors.black)
m.setBackgroundColor(colors.black)
SellSections()
    back3()
    button.screen()
    header()
end
function StartScreen()
ScreenVal = "Idle"
ItemYStart = ItemValue
m.setTextColor(colors.black)
m.setBackgroundColor(colors.black)
button.clearTable()
header()
m.setTextColor(colors.black)
button.setTable("Buy Items",BuyScreen,15,35,7,9,colors.red,colors.gray)
button.setTable("Sell Items",SellScreen,15,35,11,13,colors.red,colors.gray)
button.screen()
end
-- fillSECTION used to load the screen
-- fillSECTION used to load the screen
-- fillSECTION used to load the screen
function fillMaterials()  -- Materials
Catagory = C1
ItemYStart = ItemValue
m.setTextColor(colors.red)
   button.clearTable()
    back2()
   if ScreenVal == "Buying" then
	 CheckItems()
    --print("Basic Materials [Buying]")
    --BuyBasicMaterialsList()
   elseif ScreenVal == "Selling" then
    print("Basic Materials [Selling]")
    SellBasicMaterialsList()
  end
   button.screen()
   header()
end
function fillMachines() -- Machines
ItemYStart = ItemValue
button.clearTable()
    back2()
    if ScreenVal == "Buying" then
	 print("Machines [Buying]")
	    BuyMachinesList()
    elseif ScreenVal == "Selling" then
	 print("Machines [Selling]")
	  SellMachinesList()
  end
    button.screen()
    header()
end
function fillArmorWeapon() -- Armor or Weapons
ItemYStart = ItemValue
button.clearTable()
m.setBackgroundColor(colors.black)
    back2()
	   if ScreenVal == "Buying" then
    print("Armor / Weapons [Buying]")
    BuyArmorWeaponList()
   elseif ScreenVal == "Selling" then
    print("Armor / Weapons [Selling]")
    SellArmorWeapons()
  end
    button.screen()
    header()
  
end
function fillMisc() -- Misc stuff
ItemYStart = ItemValue
button.clearTable()
    back2()
	   if ScreenVal == "Buying" then
    print("Misc [Buying]")
    BuyMisc()
   elseif ScreenVal == "Selling" then
    print("Misc [Selling]")
    SellMisc()
  end
   button.screen()
    header()
end
-- ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄
--▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
-- ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀
-- ▄▄▄▄▄▄▄▄▄▄   ▄		 ▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄	    ▄  ▄▄▄▄▄▄▄▄▄▄▄
--▐░░░░░░░░░░▌ ▐░▌	   ▐░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░▌	  ▐░▌▐░░░░░░░░░░░▌
--▐░█▀▀▀▀▀▀▀█░▌▐░▌	   ▐░▌ ▀▀▀▀█░█▀▀▀▀  ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌░▌	 ▐░▌▐░█▀▀▀▀▀▀▀▀▀
--▐░▌	   ▐░▌▐░▌	   ▐░▌	 ▐░▌		  ▐░▌	 ▐░▌	   ▐░▌▐░▌▐░▌    ▐░▌▐░▌		 
--▐░█▄▄▄▄▄▄▄█░▌▐░▌	   ▐░▌	 ▐░▌		  ▐░▌	 ▐░▌	   ▐░▌▐░▌ ▐░▌   ▐░▌▐░█▄▄▄▄▄▄▄▄▄
--▐░░░░░░░░░░▌ ▐░▌	   ▐░▌	 ▐░▌		  ▐░▌	 ▐░▌	   ▐░▌▐░▌  ▐░▌  ▐░▌▐░░░░░░░░░░░▌
--▐░█▀▀▀▀▀▀▀█░▌▐░▌	   ▐░▌	 ▐░▌		  ▐░▌	 ▐░▌	   ▐░▌▐░▌   ▐░▌ ▐░▌ ▀▀▀▀▀▀▀▀▀█░▌
--▐░▌	   ▐░▌▐░▌	   ▐░▌	 ▐░▌		  ▐░▌	 ▐░▌	   ▐░▌▐░▌    ▐░▌▐░▌		  ▐░▌
--▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌	 ▐░▌		  ▐░▌	 ▐░█▄▄▄▄▄▄▄█░▌▐░▌	 ▐░▐░▌ ▄▄▄▄▄▄▄▄▄█░▌
--▐░░░░░░░░░░▌ ▐░░░░░░░░░░░▌	 ▐░▌		  ▐░▌	 ▐░░░░░░░░░░░▌▐░▌	  ▐░░▌▐░░░░░░░░░░░▌
-- ▀▀▀▀▀▀▀▀▀▀   ▀▀▀▀▀▀▀▀▀▀▀	   ▀		    ▀	   ▀▀▀▀▀▀▀▀▀▀▀  ▀	    ▀▀  ▀▀▀▀▀▀▀▀▀▀▀
function BuySections()
button.setTable("Basic Materials", BuyBasicMaterials, 2,FirstButtonLength,6,8,colors.red,colors.gray)
    button.setTable("Machines", BuyMachines, 2,FirstButtonLength,10,12,colors.red,colors.gray)
    button.setTable("Armor / Weapons", BuyArmorWeapons,26,FirstButtonLength*2,6,8,colors.red,colors.gray)
    button.setTable("Misc", BuyMisc,26, FirstButtonLength*2,10,12,colors.red,colors.gray)
end
function SellSections()
button.setTable("Basic Materials", SellBasicMaterials, 2,FirstButtonLength,6,8,colors.red,colors.gray)
    button.setTable("Machines", SellMachines, 2,FirstButtonLength,10,12,colors.red,colors.gray)
    button.setTable("Armor / Weapons", SellArmorWeapons,26,FirstButtonLength*2,6,8,colors.red,colors.gray)
    button.setTable("Misc", SellMisc,26, FirstButtonLength*2,10,12,colors.red,colors.gray)
end
function BuyBasicMaterials()
button.flash("Basic Materials")
fillMaterials()
end
function BuyArmorWeapons()
button.flash("Armor / Weapons")
fillArmorWeapon()
end
function BuyMachines()
button.flash("Machines")
fillMachines()
end
function BuyMisc()
button.flash("Misc")
fillMisc()
end

function SellBasicMaterials()
button.flash("Basic Materials")
fillMaterials()
end
function SellArmorWeapons()
button.flash("Armor / Weapons")
fillArmorWeapon()
end
function SellMachines()
button.flash("Machines")
fillMachines()
end
function SellMisc()
--button.flash("Misc")
fillMisc()
end
function basicfunc()
print("I don't do anything yet.. but someday I might.")
end
function back1()
m.setTextColor(colors.black)
m.setBackgroundColor(colors.black)
button.setTable("Back",StartScreen,44,50,1,1,colors.red,colors.gray)
button.setTable("Done",StartScreen,44,50,2,2,colors.red,colors.gray)
button.screen()
m.setBackgroundColor(colors.black)
end
function back2()
m.setTextColor(colors.black)
m.setBackgroundColor(colors.black)
button.setTable("Back",BuyScreen,44,50,1,1,colors.red,colors.gray)
button.setTable("Done",StartScreen,44,50,2,2,colors.red,colors.gray)
button.screen()
m.setBackgroundColor(colors.black)
end
function back3()
m.setTextColor(colors.black)
m.setBackgroundColor(colors.black)
button.setTable("Back",SellScreen,44,50,1,1,colors.red,colors.gray)
button.setTable("Done",StartScreen,44,50,2,2,colors.red,colors.gray)
button.screen()
m.setBackgroundColor(colors.black)
end
-- ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄
--▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
-- ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀
function getClick()
   event,side,x,y = os.pullEvent("monitor_touch")
   button.checkxy(x,y)
   button.checkxy(1,1)
end
function Start()
term.clear()
TerminalScreen()
StartScreen()
end

Start()
TotalClicks = 0
num = 0
while num == 0 do
term.setCursorPos(3,3)
term.write(" Total Clicks: "..TotalClicks)
term.setCursorPos(1,10)
getClick()
TotalClicks = TotalClicks + 1
end
Dog #34
Posted 16 September 2014 - 08:58 PM
Integrating a 'total count' into your code would involve a fair bit of work, based on my experience level with programming and Lua. I sat down with your code and realized that my above example was not appropriate to your circumstances (and would have needed some correction either way). After spending some time working on the problem, I'm sorry to say that the only solution I came up with is rather complicated (again, based on my knowledge level).

This will require you to change how you call CheckItems() a little. For example, I've removed all monitor writing from the CheckItems() function and made it solely for building your inventory. I moved the monitor writes for CheckItems() into their own function called drawScreen(). After calling CheckItems(), you'll need to call drawScreen() to update the monitor (the drawScreen function is not elegant). This also creates a new table called itemList that should be defined at the beginning of the program.


local itemList = { }  --# place this at or near the beginning of your code

local function drawScreen()  --# place this *before* any calls to this function
  m.setBackgroundColor(colors.red)
  m.clear()
  local tmpY = 1
  m.setCursorPos(1,tmpY)
  for i = 1, #itemList do
    m.write(itemList[i].name .. "  QTY: " .. itemList[i].qty)
    tmpY = tmpY + 1
    m.setCursorPos(1,tmpY)
  end
end

function CheckItems()
  local InvSize = Catagory.getInventorySize()
  for CurrSlot = 1, InvSize-1 do --Checking slots
    local slot = C1.getStackInSlot(CurrSlot)
    if slot ~= nil then -- Slots with stuff in them
      local success = false
      for i = 1, #itemList do --# cycle through the master item list
        if slot.name == itemList[i].name then                --# if the new item's name matches the name of item [i] in the master list then...
          local match = false
          for j = 1,#itemList[i].slots do                    --# see if we've already inventoried the slot before
            if itemList[i].slots[j].slot == CurrSlot then    --# if we have inventoried it before, we update the qty
              itemList[i].qty = itemList[i].qty - itemList[i].slots[j].qty
              itemList[i].slots[j].qty = slot.qty
              itemList[i].qty = itemList[i].qty + itemList[i].slots[j].qty
              match = true
              break --# break out of the loop because we're done and don't need to let the loop finish
            end
          end
          if not match then
            itemList[i].qty = itemList[i].qty + slot.qty   --# add the count of the matching item to the total in the master list
            itemList[i].slots[#itemList[i].slots+1].slot = CurrSlot --# store the slot number, so we know which slot(s) this item is in
            itemList[i].slots[#itemList[i].slots].qty = slot.qty
            success = true
            break --# break out of the loop because we're done and don't need to let the loop finish
          end
        end
      end
      if not success then
        itemList[#itemList+1].name = slot.name
        itemList[#itemList].slots[1].slot = CurrSlot
        itemList[#itemList].slots[1].qty = slot.qty
        itemList[#itemList].qty = slot.qty
      end
    end
  end
end

If any other pros see a relatively easy way to do this, please chime in!
Edited on 16 September 2014 - 10:26 PM
Noobular #35
Posted 16 September 2014 - 09:42 PM
.. Well can we Create a table then have the table update it self every X seconds/minutes instead of constantly updating .. hell that would break the Computer if it was constantly updating.
Dog #36
Posted 16 September 2014 - 09:50 PM
I'm going to go ahead and say the above example is flawed. It didn't take into account items removed or changed. By clearing the itemList table before re-inventorying that issue is addressed, so this is probably a better place to start

local itemList = { }  --# place this at or near the beginning of your code

local function drawScreen()  --# place this *before* any calls to this function
  m.setBackgroundColor(colors.red)
  m.clear()
  local tmpY = 1
  m.setCursorPos(1,tmpY)
  for i = 1, #itemList do
    m.write(itemList[i].name .. "  QTY: " .. tostring(itemList[i].qty))
    tmpY = tmpY + 1
    m.setCursorPos(1,tmpY)
  end
end

function CheckItems()
  itemList = { }
  local InvSize = Catagory.getInventorySize()
  for CurrSlot = 1, InvSize-1 do --Checking slots
    local slot = C1.getStackInSlot(CurrSlot)
    if slot ~= nil then -- Slots with stuff in them
      local match = false
      if #itemList == 0 then --# if there are no entries yet, add the first one
        itemList[1].name = slot.name
        itemList[1].slots[1].slot = CurrSlot
        itemList[1].slots[1].qty = slot.qty
        itemList[1].qty = slot.qty 
      else
        for i = 1, #itemList do                 --# cycle through the master item list
          if slot.name == itemList[i].name then --# if the new item's name matches the name of item [i] in the master list then update the info
            itemList[i].slots[#itemList[i].slots+1].slot = CurrSlot
            itemList[i].slots[#itemList[i].slots].qty = slot.qty
            itemList[i].qty = itemList[i].qty + slot.qty
            match = true
            break
          end
        end
        if not match then --# if the item isn't already in the list, add it
          itemList[#itemList+1].name = slot.name
          itemList[#itemList].slots[1].slot = CurrSlot
          itemList[#itemList].slots[1].qty = slot.qty
          itemList[#itemList].qty = slot.qty 
        end
      end
    end
  end
end
Edited on 16 September 2014 - 10:32 PM
Noobular #37
Posted 17 September 2014 - 12:21 AM
I'm getting so butthurt with this ugh, Keep getting an error at 46, the table / index was expected but it got nil


local C1 = peripheral.wrap("obsidian_1")
local itemList = { }  --# place this at or near the beginning of your code
local function drawScreen()  --# place this *before* any calls to this function
  m.setBackgroundColor(colors.red)
  m.clear()
  local tmpY = 1
  m.setCursorPos(1,tmpY)
  for i = 1, #itemList do
    m.write(itemList[i].name .. "  QTY: " .. itemList[i].qty)
    tmpY = tmpY + 1
    m.setCursorPos(1,tmpY)
  end
end
Catagory = C1
function CheckItems()
  local InvSize = Catagory.getInventorySize()
  for CurrSlot = 1, InvSize-1 do --Checking slots
    local slot = Catagory.getStackInSlot(CurrSlot)
    if slot ~= nil then -- Slots with stuff in them
	  local success = false
	  for i = 1, #itemList do --# cycle through the master item list
	    if slot.name == itemList[i].name then			    --# if the new item's name matches the name of item [i] in the master list then...
		  local match = false
		  for j = 1,#itemList[i].slots do				    --# see if we've already inventoried the slot before
		    if itemList[i].slots[j].slot == CurrSlot then    --# if we have inventoried it before, we update the qty
			  itemList[i].qty = itemList[i].qty - itemList[i].slots[j].qty
			  itemList[i].slots[j].qty = slot.qty
			  itemList[i].qty = itemList[i].qty + itemList[i].slots[j].qty
			  match = true
			  break --# break out of the loop because we're done and don't need to let the loop finish
		    end
		  end
		  if not match then
		    itemList[i].qty = itemList[i].qty + slot.qty   --# add the count of the matching item to the total in the master list
		    itemList[i].slots[#itemList[i].slots+1].slot = CurrSlot --# store the slot number, so we know which slot(s) this item is in
		    itemList[i].slots[#itemList[i].slots].qty = slot.qty
		    success = true
		    break --# break out of the loop because we're done and don't need to let the loop finish
		  end
	    end
	  end
	  if not success then
	    itemList[#itemList+1].name = slot.name
	    itemList[#itemList].slots[1].slot = CurrSlot
	    itemList[#itemList].slots[1].qty = slot.qty
	    itemList[#itemList].qty = slot.qty
	  end
    end
  end
end

CheckItems()
drawScreen()
Dog #38
Posted 17 September 2014 - 12:48 AM
Forget that code…with my apologies. If you look above your post, you'll see I posted 'new' code that is better suited to what you want to do (and *should* avoid the error you were seeing). Unfortunately I don't have a way to test the code atm :/ Try the new code and let me know what happens - we'll go from there. Hang in there, I think we're pretty close.
Edited on 16 September 2014 - 10:49 PM
Noobular #39
Posted 17 September 2014 - 12:58 AM
Just pointing out the obvious, Since its going to be multiple chest the Catagory you keep swapping to C1 needs to stay catagory and just above the function do Catagory = C1 , But let me test what you have other then that lol



(25 same issue)

local C1 = peripheral.wrap("obsidian_1")
local itemList = { }  --# place this at or near the beginning of your code
local function drawScreen()  --# place this *before* any calls to this function
  m.setBackgroundColor(colors.red)
  m.clear()
  local tmpY = 1
  m.setCursorPos(1,tmpY)
  for i = 1, #itemList do
    m.write(itemList[i].name .. "  QTY: " .. tostring(itemList[i].qty))
    tmpY = tmpY + 1
    m.setCursorPos(1,tmpY)
  end
end
function CheckItems()
  itemList = { }
  local InvSize = Catagory.getInventorySize()
  for CurrSlot = 1, InvSize-1 do --Checking slots
    local slot = C1.getStackInSlot(CurrSlot)
    if slot ~= nil then -- Slots with stuff in them
	  local match = false
	  if #itemList == 0 then --# if there are no entries yet, add the first one
	    itemList[1].name = slot.name
	    itemList[1].slots[1].slot = CurrSlot
	    itemList[1].slots[1].qty = slot.qty
	    itemList[1].qty = slot.qty
	  else
	    for i = 1, #itemList do				 --# cycle through the master item list
		  if slot.name == itemList[i].name then --# if the new item's name matches the name of item [i] in the master list then update the info
		    itemList[i].slots[#itemList[i].slots+1].slot = CurrSlot
		    itemList[i].slots[#itemList[i].slots].qty = slot.qty
		    itemList[i].qty = itemList[i].qty + slot.qty
		    match = true
		    break
		  end
	    end
	    if not match then --# if the item isn't already in the list, add it
		  itemList[#itemList+1].name = slot.name
		  itemList[#itemList].slots[1].slot = CurrSlot
		  itemList[#itemList].slots[1].qty = slot.qty
		  itemList[#itemList].qty = slot.qty
	    end
	  end
    end
  end
end

CheckItems()
drawScreen()
Edited on 16 September 2014 - 11:00 PM
Dog #40
Posted 17 September 2014 - 01:18 AM
Swapping C1 was my mistake. However, for this code, you don't have Category defined, so it might be good to stick with C1 until this is working as intended.

Line 25 is

itemList[i].qty = slot.qty

I'm guessing slot.qty is not valid
Edited on 16 September 2014 - 11:20 PM
Noobular #41
Posted 17 September 2014 - 01:23 AM
I'm curious what exactly does local do, does it just keep that within the current code its in .. because wouldn't that mess with the C1 at the very top?
Dog #42
Posted 17 September 2014 - 01:30 AM
Basically, yes, that is what local does. Any variables or functions that aren't declared local are assumed global and can be accessed from any program on the computer. Localized variables/functions also receive a small performance boost. It's considered good practice to localize your variables and functions unless you require them to be global. One thing to keep in mind is that local variables and functions are only available to things below them in the code, not above them; and localized variables and functions only exist within their scope (if you create a local variable within a for loop, for example, it would only exist in that for loop).

I'm not sure what you mean about local messing with C1 at the top. Which localized variable or function are you concerned about?

Also, just for testing this code, 'Catagory' should be replaced with C1 since 'Catagory' is not defined.
Edited on 16 September 2014 - 11:42 PM
Noobular #43
Posted 17 September 2014 - 01:42 AM
Well after that being said its not an issue , i was thinking the fact it was local would mess it up by not reconizing what C1 was in the first place , when i put Catagory = C1

as if C1 didn't equal the peripheral
Dog #44
Posted 17 September 2014 - 01:44 AM
No worries. So, currently it appears that slot.qty is not a valid table entry we can reference. Do you know the correct table entry we need for quantity?
Noobular #45
Posted 17 September 2014 - 01:49 AM
It's not the slot.qty, it would be the name atleast its the name on mine and i tried to swap slot.name to "test value" and it still doesn't want it to work

Specificially "Index expected, got nil"

as if the Table was never created.

SPECIFICALLY :
if #itemList == 0 then –# if there are no entries yet, add the first one
itemList[1].name = "test value"


function CheckItems()
  itemList = { }
  local InvSize = Catagory.getInventorySize()
  for CurrSlot = 1, InvSize-1 do --Checking slots
    local slot = C1.getStackInSlot(CurrSlot)
    if slot ~= nil then -- Slots with stuff in them
	  local match = false
	  if #itemList == 0 then --# if there are no entries yet, add the first one
	    itemList[1].name = "test value"
	    itemList[1].slots[1].slot = CurrSlot
	    itemList[1].slots[1].qty = slot.qty
	    itemList[1].qty = slot.qty
	  else
	    for i = 1, #itemList do				 --# cycle through the master item list
		  if slot.name == itemList[i].name then --# if the new item's name matches the name of item [i] in the master list then update the info
		    itemList[i].slots[#itemList[i].slots+1].slot = CurrSlot
		    itemList[i].slots[#itemList[i].slots].qty = slot.qty
		    itemList[i].qty = itemList[i].qty + slot.qty
		    match = true
		    break
		  end
	    end
	    if not match then --# if the item isn't already in the list, add it
		  itemList[#itemList+1].name = slot.name
		  itemList[#itemList].slots[1].slot = CurrSlot
		  itemList[#itemList].slots[1].qty = slot.qty
		  itemList[#itemList].qty = slot.qty
	    end
	  end
    end
  end
end

I should point out i'm using this code directly on a test program not in the shop program.
Edited on 16 September 2014 - 11:48 PM
valithor #46
Posted 17 September 2014 - 01:52 AM
It should be noted that local variables can only be used in the same code block or ones that extend from it (Not just if where it is used is under) a example of this would be this


for i = 1, 5 do
  local example = i
end
if example == nil then
  print("yes")
end

In this example it will print yes because example was not defined in the same block of code if the if example == nil was in the for loop it would have worked.
Edited on 16 September 2014 - 11:53 PM
Noobular #47
Posted 17 September 2014 - 01:57 AM
Yea, i assumed so , If inside then usable but if its open it can be openly used in the program
Dog #48
Posted 17 September 2014 - 01:58 AM
Based on the code you posted on the previous page, itemList[1].name isn't line 25 by my count, itemList[1].qty = slot.qty is line 25. Give me a moment to setup a test world and check this out.

btw…thank you, valithor for the clarification on locals :)/>
Edited on 17 September 2014 - 12:57 AM
Noobular #49
Posted 17 September 2014 - 02:20 AM
I changed one thing that's why.

it changed to 24 after i did something and that's the name.
Edited on 17 September 2014 - 12:20 AM
Dog #50
Posted 17 September 2014 - 02:23 AM
I see where the problem is happening, but I'm not sure (yet) how to deal with it. Apparently we can't create sub-tables as easily as I had assumed. I can get the code working but it's very ugly. I'm going to need some time to do some research.

If any other pros have suggestions, please do join in.
Noobular #51
Posted 17 September 2014 - 02:30 AM
Ahh ok.. yea that's the same problem ish, i was thinking about originally i don't qute know how to just make a table and add things while doing stuff , i know how to with just listing everything in the code just not exactly when its running when its grabbing data.

Btw this is how mine i setup, the terminal is facing the monitor

Also i think how i want to do it is .. When it list.. i want it to still use the NewItem() Function, and just make the ItemVar be the same item name or something and that correlates to the qty to make it actually equal something other then 0 when its created with that.
Dog #52
Posted 17 September 2014 - 02:34 AM
We'll get there - one thing at a time (that's all I can handle :D/> ).

This code is working for me…

local C1 = peripheral.wrap("obsidian_0")
local m = peripheral.wrap("top")
local itemList = { }        --# place this at or near the beginning of your code

local function drawScreen() --# place this *before* any calls to this function
  m.setBackgroundColor(colors.red)
  m.clear()
  local tmpY = 1
  m.setCursorPos(1,tmpY)
  for i = 1, #itemList do
    m.write(itemList[i].name .. "  QTY: " .. tostring(itemList[i].qty))
    tmpY = tmpY + 1
    m.setCursorPos(1,tmpY)
  end
end

function CheckItems()
  itemList = { }
  local InvSize = C1.getInventorySize()
  for CurrSlot = 1, InvSize-1 do --Checking slots
    local slot = C1.getStackInSlot(CurrSlot)
    if slot ~= nil then -- Slots with stuff in them
      local match = false
      if #itemList == 0 then --# if there are no entries yet, add the first one
        itemList[1] = { name = slot.name }
        itemList[1].slots = { }
        itemList[1].slots[1] = { slot = CurrSlot }
        itemList[1].slots[1].qty = slot.qty
        itemList[1].qty = slot.qty 
      else
        for i = 1, #itemList do                 --# cycle through the master item list
          if slot.name == itemList[i].name then --# if the new item's name matches the name of item [i] in the master list then update the info
            itemList[i].slots[#itemList[i].slots+1] = { }
            itemList[i].slots[#itemList[i].slots] = { slot = CurrSlot }
            itemList[i].slots[#itemList[i].slots].qty = slot.qty
            itemList[i].qty = itemList[i].qty + slot.qty
            match = true
            break
          end
        end
        if not match then --# if the item isn't already in the list, add it
          itemList[#itemList+1] = { name = slot.name }
          itemList[#itemList].slots = { }
          itemList[#itemList].slots[1] = { slot = CurrSlot }
          itemList[#itemList].slots[1].qty = slot.qty
          itemList[#itemList].qty = slot.qty 
        end
      end
    end
  end
end

CheckItems()
drawScreen()
Edited on 17 September 2014 - 12:37 AM
Noobular #53
Posted 17 September 2014 - 02:37 AM
WOOHOO!! progress! :D/>
Dog #54
Posted 17 September 2014 - 02:40 AM
Get that merged into and working with your current code and we'll proceed from there :)/>
Noobular #55
Posted 17 September 2014 - 02:43 AM
I think a smarter way of doing this, is instead of calling CheckItems() when someone opens one of the catagorys we just do CheckItems() at the start of the Buy or Sell buttons then if someone buys something recheck the Masterlist.
Because i already see a bit amount of like lag during the time you click Basicmaterials to when it actually loads the header(usable screen)
Dog #56
Posted 17 September 2014 - 02:45 AM
Yeah - it seems to take a bit of time to scan the entire chest. Give your idea a shot, and if you have problems, I (or someone) will be here :)/>
Noobular #57
Posted 17 September 2014 - 03:01 AM
Tad bit confused how to reference the Item specifically in NewItem()

When doing this, i get (end of post) , There is only cobble stone , jungle wood and Sandstone, when Jungle wood i don't even think is identified correctly the way i made newitem

function NewItem(itemname,itemvar,xmin,price) -- Constructor for Items
m.setTextColor(colors.red)
m.setBackgroundColor(colors.gray)
itemvar = itemList[1].name

ItemYStart = ItemYStart + 1
ymax = 1 
xmax = 20
price = ("$"..price)
m.setTextColor(colors.black)
m.setCursorPos(xmin,ItemYStart)
    m.write("				    "..itemList[1].qty.." ("..price.."/Ea)")
    m.setTextColor(colors.red)
    m.setCursorPos(xmin,ItemYStart)
    m.write(itemname..": ")
end
Dog #58
Posted 17 September 2014 - 03:51 AM
Tad bit confused how to reference the Item specifically in NewItem()

When doing this, i get (end of post) , There is only cobble stone , jungle wood and Sandstone, when Jungle wood i don't even think is identified correctly the way i made newitem

function NewItem(itemname,itemvar,xmin,price) -- Constructor for Items
m.setTextColor(colors.red)
m.setBackgroundColor(colors.gray)
itemvar = itemList[1].name

ItemYStart = ItemYStart + 1
ymax = 1
xmax = 20
price = ("$"..price)
m.setTextColor(colors.black)
m.setCursorPos(xmin,ItemYStart)
	m.write("					"..itemList[1].qty.." ("..price.."/Ea)")
	m.setTextColor(colors.red)
	m.setCursorPos(xmin,ItemYStart)
	m.write(itemname..": ")
end

You'll need to use a loop to go through the list of items. Something like this…

local tmpY = 5 --# Y position for first entry
for i = 1, #itemList do
  m.setCursorPos(1,tmpY)
  m.write(itemList[i].name .. "	 " .. itemList[i].qty .. " (" .. price .. "/Ea)")
  tmpY = tmpY + 1
end
Noobular #59
Posted 17 September 2014 - 06:05 AM
I want to .. do a forloop and in that do an if statement when , if the name is correct then it will list the qty

Name in the table = itemvar currently these are what i have but obviously with this i need to change the itemvar's


NewItem("Sand",Sand,2,100)
NewItem("Sandstone",Sandstone,2,100)
NewItem("Cobble Stone",CobbleStone,2,100)
NewItem("Stone",Stone,2,100)
NewItem("Grass", Grass,2,100)
NewItem("Dirt",Dirt,2,100)
NewItem("Oak log",Olog,2,100)
NewItem("Spruce Log",Slog,2,100)
NewItem("Maple Log",Mlog,2,100)
NewItem("Jungle Log",Jlog,2,100)
NewItem("Wooden Planks",Planks,2,100)


function NewItem(itemname,itemvar,xmin,price) -- Constructor for Items
--[[
m.setTextColor(colors.red)
m.setBackgroundColor(colors.gray)
itemvar = itemList[1].name

ItemYStart = ItemYStart + 1
ymax = 1 
xmax = 20
price = ("$"..price)
m.setTextColor(colors.black)
m.setCursorPos(xmin,ItemYStart)
    m.write("				    "..itemList[1].qty.." ("..price.."/Ea)")
    m.setTextColor(colors.red)
    m.setCursorPos(xmin,ItemYStart)
    m.write(itemname..": ") ]]

end
Dog #60
Posted 17 September 2014 - 06:21 AM
The basics don't change, you just add an if statement in there, like so…

for i = 1, #itemList do
  if itemname == itemList[i].name then --# itemvar or itemname ?
    m.setCursorPos(xmin,ItemYStart)
    m.write(itemList[i].name .. "  " .. itemList[i].qty .. " (" .. price .. "/Ea)")
    --# rest of your stuff
    break
  end
end

This will go through itemList and display the matching item on screen, then break the loop since we're only looking for one match. Obviously, you'll have to adapt this to work with your code, but if I understood you correctly, this should give you an idea of how to approach the idea.
Noobular #61
Posted 17 September 2014 - 06:32 AM
Well i did this without looking at the thread and this seems to work…

function NewItem(itemname,itemvar,xmin,price) -- Constructor for Items
itemvar = 0
m.setTextColor(colors.red)
m.setBackgroundColor(colors.gray)
ItemYStart = ItemYStart + 1
ymax = 1 
xmax = 20
price = ("$"..price)
m.setTextColor(colors.black)
m.setCursorPos(xmin,ItemYStart)
    m.write("				    "..itemvar.." ("..price.."/Ea)")
    m.setTextColor(colors.red)
    m.setCursorPos(xmin,ItemYStart)
    m.write(itemname..": ")
  for CurrSlot = 1, InvSize-1 do --Checking slots
    local slot = Catagory.getStackInSlot(CurrSlot)
	 if slot ~= nil then -- Slots with stuff in them
	  itemList[i].name = itemname
	  itemvar = itemList[i].qty
	 else
	  break
	 end
   
  end
end

Oh never mind , i got a Index expected got nil error again on


	  itemList[i].name = itemname
Noobular #62
Posted 17 September 2014 - 06:45 AM
I changed to yours it works perfectly .. i just need to remember do use #itemList when i want to check out something from it.

ACTUALLY : The break in it just breaks it out of checking all of the items when trying to list them out in the shop menu, so it goes to sandstone then stops…

Actually i'm stupid, i forgot everything was 100% case sensitive.
Edited on 17 September 2014 - 05:12 AM
Dog #63
Posted 17 September 2014 - 07:20 AM
Glad you got it working, Noobular :)/> If you need any further help, or have any other questions, don't hesitate to ask!
Noobular #64
Posted 17 September 2014 - 10:01 AM
I think the only thing that upsets me about this, is if you count the button api i'm at like 550 lines of code, I HAVENT EVEN ADDED LIKE , adding to cart and .. Processing a Purchase Like shit.

>600 lines left.
Noobular #65
Posted 21 September 2014 - 02:13 AM
I haven't worked on the shop in the last few days but, i see a lot of people use inpairs or pairs and i'm really not sure what exactly it does i'm guessing it takes these to bits of info and keeps them together but .. i'm unsure lol
KingofGamesYami #66
Posted 21 September 2014 - 02:24 AM
I haven't worked on the shop in the last few days but, i see a lot of people use inpairs or pairs and i'm really not sure what exactly it does i'm guessing it takes these to bits of info and keeps them together but .. i'm unsure lol

This iterates through a table:

for k, v in pairs( tbl ) do
end
This iterates through a numerically indexed table:

for k, v in ipairs( tbl ) do
end

You can use any variables you like, not just k, v.
Noobular #67
Posted 22 September 2014 - 12:27 AM
You've shown me how to use it but i don't know what its used for lol

(by the way the next step in the shop will most likely be using the miscperipherals player detector and making credits so you just log your self in and you can buy from there instead of needing X number of an item)
KingofGamesYami #68
Posted 22 September 2014 - 03:39 AM
You've shown me how to use it but i don't know what its used for lol

It's used for iterating through tables. That's what pairs() and ipairs() do.
Bomb Bloke #69
Posted 22 September 2014 - 04:06 AM
You can get some more examples on them here.