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

Loops

Started by ihatetn931, 22 August 2014 - 05:08 AM
ihatetn931 #1
Posted 22 August 2014 - 07:08 AM
How do i loop through only certian slots?

For example i have a chest with 4 slots, i only wanna loop through slot 2 and 4 and skip slots 1 and 3, how would i go about doing that?

Trying to think of a way so i don't have to add if statements for each slot
Edited on 22 August 2014 - 05:11 AM
Bomb Bloke #2
Posted 22 August 2014 - 07:20 AM
Assuming you already know the non-looping aspects of the issue, like this:

for i=2,4,2 do  -- Start at 2, then go up to 4 in increments of 2.
  -- do slot stuff
end
ihatetn931 #3
Posted 22 August 2014 - 07:30 AM
I actually tried that then relized it won't work cause there is 54 slots in the chest 2 slots next to each other have the same thing

slot 9 and slot 10 hold the same item and so on, but they're on diffrent lines

! = slot i wanna get
* = slot i wanna skip

!**!**!*!
!**!**!*!
!**!**!*!
!**!**!*!
!**!**!*!


1  2  3  4  5  6  7  8  9
10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36
37 38 39 40 41 41 43 44 45
46 47 48 49 50 51 52 53 54
(spaced top line a bit so they line up)
Hope that makes sense, see how the order is not all the same, so doing increments wouldn't work to well, using openperipheral to move items
Edited on 22 August 2014 - 05:51 AM
Bomb Bloke #4
Posted 22 August 2014 - 07:55 AM
local counter, incCounter, increments = 1, 1, {3,3,2,1}

while counter < 55 do
  -- Do slot management stuff here.

  counter = counter + increments[incCounter]
  incCounter = incCounter + 1
  if incCounter > #increments then incCounter = 1 end
end
ihatetn931 #5
Posted 22 August 2014 - 08:01 AM
I'm assuming that can't be put in to a variable correct?

Or am i just to dumb to see the variable, by the looks of it, it looks like i can use counter

Code i'm workin with


local reactorLzh = peripheral.wrap("front")

while true do
for chest = 1 , 27 do
end
for turTle = 1 , 16 do
end

--for lzh = 1 , 4 , 7 , 9 , 10 , 13 , 16 , 18 , 19 , 22 , 25 , 27 , 28 , 31 , 34 , 36 , 37 , 40 , 43 , 45 , 46 , 49 , 52 , 54 do
--end
local aS = reactorLzh.getStackInSlot(lzh)
term.clear()
term.setCursorPos(1,1)

if aS ~= nil then
if turTle ~= nil then
if aS.name == "ic2.reactorCondensatorLap" then
if aS.dmg == 3807 then
print("Moving Slot :"..lzh)
rs.setOutput("front",false)
reactorLzh.pushItemIntoSlot("north", lzh, 1, chest)
reactorLzh.pullItemIntoSlot("west", lzh, 1, turTle)
--pullItemIntoSlot(direction, slot, maxAmount, intoSlot)
--pushItemIntoSlot(direction, slot, maxAmount, intoSlot)
end
end
end
end
end

lzh was my second attempt of doing the loop
Edited on 22 August 2014 - 06:08 AM
Bomb Bloke #6
Posted 22 August 2014 - 08:11 AM
by the looks of it, it looks like i can use counter

Indeed, each iteration, you just use "counter" to check the next slot.
ihatetn931 #7
Posted 22 August 2014 - 08:27 AM
Well this is way over my lua coding knowledge, time to learn how that piece of code works
Bomb Bloke #8
Posted 22 August 2014 - 09:48 AM
After the first line, there are these values set up:

counter = 1
incCounter = 1
increments[1] = 3
increments[2] = 3
increments[3] = 2
increments[4] = 1

If you're not familiar with tables, now would be a good time to read up on them. There're generally the best way to store large sets of data in Lua.

When the loop runs the first time, you can do what you like with slot "counter", which is 1.

Then "counter" is incremented by increments[incCounter]. Since incCounter is 1, increments[incCounter] is 3, so counter goes up from 1 to 4.

Then incCounter goes up one and the loop repeats. On the next iteration, "counter" goes up by 3 again (to 7). But on the iteration after that, incCounter will be 3, so increments[incCounter] will be 2 and "counter" will go up to 9.

And so on. Whenever incCounter exceeds the number of entries in the increments table (represented by #increments), it resets back to 1. Get it?
ihatetn931 #9
Posted 22 August 2014 - 04:27 PM
Actually yes, i have just recently found out how tables work and been using them.

So i sat here thinking, is there really a need to skip slots, when i can just loop through each slot and just pull the item by name, well i went and did that and it's not liking it, i find funny cause the other 2 for loops work fine.

Error (one i've never seen)

startup:17: Parmeter "slotNumber' has null value, but is not marked as nullable


local reactorLzh = peripheral.wrap("front")


for turTle = 1 , 16 do
end

for chest = 1, 27 do
end

for reactor = 1, 54 do
end

while true do
local aS = reactorLzh.getStackInSlot(reactor) --line 17
term.clear()
term.setCursorPos(1,1)
print(reactor)
if turTle ~= nil then
if aS.id ~= nil then
if aS.name == "ic2.reactorCondensatorLap" then
if aS.dmg == 3807 then
print("Moving Slot :"..reactor)
rs.setOutput("front",false)
reactorLzh.pushItemIntoSlot("north", reactor, 1, chest)
reactorLzh.pullItemIntoSlot("west", reactor, 1, turTle)
--pullItemIntoSlot(direction, slot, maxAmount, intoSlot)
--pushItemIntoSlot(direction, slot, maxAmount, intoSlot)
end
end
end
end
sleep(0)
end
Edited on 22 August 2014 - 02:32 PM
Bomb Bloke #10
Posted 22 August 2014 - 05:00 PM
When you start a "for" loop, the variable you use as the counter is local to that "for" loop. That means that when the loop ends, it's discarded.

For whatever reason, you're ending your first three "for" loops immediately after initialising them. This means that "reactor" will be nil when you hit line 17 - you really want to wrap those three loops around the bulk of the script. It may be worth taking a read through this tutorial as well as these notes on indentation.

Also consider that your "while" loop is only getting in the way of your current plan. Ditch it.

Lastly, you could reduce this sort of thing:

if turTle ~= nil then
	if aS.id ~= nil then
		if aS.name == "ic2.reactorCondensatorLap" then
			if aS.dmg == 3807 then
				.
				.
				.
			end
		end
	end
end

… down to this sort of thing:

if turTle ~= nil and aS.id ~= nil and aS.name == "ic2.reactorCondensatorLap" and aS.dmg == 3807 then
	.
	.
	.
end
ihatetn931 #11
Posted 22 August 2014 - 09:26 PM
Best i could come out with, dosen't work as planned tho, it seems to loop trough one then when it gets to the end the other loop goes up, i was hoping they would all loop at the same time. My code is indented, it just looses intendtation when i paste and or post it


for r = 1,54 do
for t = 1,16 do
for c = 1,27 do
local reactor = reactorLzh.getStackInSlot(r)
term.clear()
term.setCursorPos(1,1)
print("R: "..r)
print("T: "..t)
print("C: "..c)
if t ~= nil and reactor ~= nil and reactor.id == 30083  and reactor.dmg == 3807 then
print("Moving Slot :"..r)
rs.setOutput("front",false)
reactorLzh.pushItemIntoSlot("north", r, 1, c)
--pullItemIntoSlot(direction, slot, maxAmount, intoSlot)
--pushItemIntoSlot(direction, slot, maxAmount, intoSlot)
reactorLzh.pullItemIntoSlot("west", t, 1, r)
end
end
end
end
What code looks like on my end

Edited on 22 August 2014 - 07:56 PM
Bomb Bloke #12
Posted 23 August 2014 - 01:28 AM
it seems to loop trough one then when it gets to the end the other loop goes up, i was hoping they would all loop at the same time.

Yes, that's generally how for loops work.

If you wanted all three values to go up every time the loop repeats, it'd go something like this:

local t, c = 1, 1

for r = 1,54 do
	local reactor = reactorLzh.getStackInSlot(r)
	term.clear()
	term.setCursorPos(1,1)
	print("R: "..r)
	print("T: "..t)
	print("C: "..c)

	if t ~= nil and reactor ~= nil and reactor.id == 30083  and reactor.dmg == 3807 then
		print("Moving Slot :"..r)
		rs.setOutput("front",false)
		reactorLzh.pushItemIntoSlot("north", r, 1, c)
		--pullItemIntoSlot(direction, slot, maxAmount, intoSlot)
		--pushItemIntoSlot(direction, slot, maxAmount, intoSlot)
		reactorLzh.pullItemIntoSlot("west", t, 1, r)
	end

	t = t + 1
	c = c + 1

	if t > 16 then t = 1 end
	if c > 27 then c = 1 end
end

My code is indented, it just looses intendtation when i paste and or post it

Up the top left of the posting box is a light switch button - this toggles the rich text editor. That that off before pasting in your code.
ihatetn931 #13
Posted 23 August 2014 - 02:09 AM
Thank you for the indent tip, i didn't know that. I will eventually get the hang of this lua coding, you have been a lot of help. I do appericate it.

Code test

function mon1Day()
	local day = os.day()
	mon1.setCursorPos(1,10)
	mon1.clearLine()
	centerMon1("MC Day: "..day)
end