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

for loop storing data

Question

Good day

I am reading a chest full of items, and I want to store the items data for later use.

you can see the snippet of the program and I hope you see what I mean.

How do I store and recall a list of items?

local component = require("component")
 local sides = require("sides")
 local tp = component.transposer


slot = 1
   for i = 1 , slot do
       gis = tp.getItemInSlot(sides,east,i) --store item that is being read to recall later
         if gis then
            gp.label(i) == gis.label
            gp.size(i) == gis.size
            slot = slot + 1
          end

end

 

for i = 1 , slot do -- recall stored item and print

    print(gp.label(i)

    print(gp.size(i)

end

 

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

So there's quite a bit wrong with this, for a start the for loop can't be used that way, not in Lua atleast. I'd recomend a while loop which would look something like this.

while true do
	local gis = tp.getItemInSlot(sides,east,i) --store item that is being read to recall later
	if not gis then break end
  	
  	-- Rest of code
  
end

With your list, you first need to create a table before you can reference  gp like that. So at the top of your code you would need something like "local gp = {}". Then when adding to it the simplest way is to use

gp[slot] = gis
-- or if you want to copy it
gp[slot] = {}
gp[slot].label = gis.label
gp[slot].size  = gis.size

In above code I also fixed some other issues where you used the calling syntax "(" and ")" instead of indexing syntax "[" and "]". Also "==" is the equality operator, what you meant to have was "=" the assignment operator.

For the last bit of code the easiest way to do it correctly is using the same indexing you saw in above code (gp[slot].label) and replace slot with #gp

Link to post
Share on other sites
  • 0

Thanks alot

 

I will try this, Iam still a beginner trying to write an advanced program.

Although I think I will get this to help me.

There is still one question that I have left and that is, I am reading items from a chest and then I print it ona screen. How can I compare all the slots by gp[slot].label and if it is the same then I want to add the gp [slot].size together.

The slots might be scrambled so I need to check all the slots compare then and then save them.  Will it look almost like this?

for i = 1, slot do

 If gp [slot].label == gp .label then

 gp [slot].size = gp [slot].size + gp.size

 gp .label = nil --to remove the compared slot if it was the same

 gp .size = nil

end --if

end --for

Link to post
Share on other sites
  • 0
function checkDust()
    top = trans.getInventorySize(chest)
    for i = 1,top do
        inventory = trans.getStackInSlot(chest, i)
        if inventory ~= nil then
            for k,v in pairs(inventory) do
                if k == "name" then
                    if v == "minecraft:cobblestone" then
                        trans.transferItem(chest, hammer, 1,i, 1)
                        os.sleep(.5)
                        trans.transferItem(hammer, chest)
                    elseif v == "minecraft:gravel" then
                        trans.transferItem(chest,hammer, 1,i,1)
                        os.sleep(.5)
                        trans.transferItem(hammer, chest)
                    elseif v == "minecraft:sand" then
                        trans.transferItem(chest, hammer, 1,i, 1)
                        os.sleep(.5)
						trans.transferItem(hammer, chest, 1,i,1)
                    elseif v == "exnihloadscensio:blockDust" then
						trans.transferItem(chest, siev, 1,i,1)
					end
                end
            end
        end
    end
end

this is one of my functions on my functions in my sieve program, this is how i check all the slots =), hope its for any use to u :P

 

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.