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

How to use os.pullEvent() in a function?

Started by CreeperGoBoom, 08 December 2019 - 12:02 PM
CreeperGoBoom #1
Posted 08 December 2019 - 01:02 PM
If i try use this in a function it hangs the computer, and errors if i try put break in the function.So is this possible?

my code snippet:

function checkRSInput(inputside,val)
  local event=os.pullEvent()
  if event=="redstone" then
	if rs.getInput(inputside,val) then
	  local result=val
	  return result
	  --break --this is commented because it throws end expected
	end
  end
end
Edited on 08 December 2019 - 12:07 PM
Lupus590 #2
Posted 08 December 2019 - 01:29 PM
the break is redundant because you have a return statement just before it which acheives the same thing (you can't exit that function without also exiting the loop that the break would exit)
CreeperGoBoom #3
Posted 08 December 2019 - 01:51 PM
Any idea whats making my code hang when my function is called?

https://pastebin.com/ZVLmzaF2
Lupus590 #4
Posted 08 December 2019 - 02:38 PM
I think you should wait for redstone events (os.pullEvent("redstone")) and remove the sleep(0) statements.
CreeperGoBoom #5
Posted 08 December 2019 - 03:32 PM
Ok so player query detected showing but its not taking payment from the chest. any idea whats going on?
Updated the pastebin:
Edited on 08 December 2019 - 02:32 PM
Lupus590 #6
Posted 08 December 2019 - 04:01 PM
what does your network setup look like?

if you add some print statements to print out the values returned by the chest functions what do you get?
CreeperGoBoom #7
Posted 08 December 2019 - 06:07 PM
Print statements are our best friend! :)/>
Turns out I was missing a rs.setOutput.

I have updated the pastebin.
Edit: I have got it to work but have a small problem (see line 87-107)

say if I put some stacks in the chest,

for reference my toll is set to 4

Stacks:
3 1 1 3

This should prepay for two trips. but for some reason it takes all of it not counting it correctly in one trip. any thoughts?

And for convenience ill just copy and paste the link to the paste again :)/>
https://pastebin.com/ZVLmzaF2

Do you still require the system layout?
Edited on 08 December 2019 - 05:40 PM
Lupus590 #8
Posted 08 December 2019 - 06:39 PM
Try this, it merges item stacks in a chest


--# public domain: https://discordapp.com/channels/477910221872824320/477912446946902019/638356931886710784

local function compactSlots(chest)
  --# TODO: arg checker?
  for slot in pairs(chest.list()) do
    chest.pushItems(chest._peripheralName, slot)
  end
end
local c = peripheral.wrap("minecraft:chest_7")
c._peripheralName = "minecraft:chest_7"
compactSlots(c)
CreeperGoBoom #9
Posted 08 December 2019 - 06:42 PM
just edited my post as you posted that if that changes anything. sorry about that.

This is for mainly if someone wants to charge like 2 stacks of something as a toll.

Edit: on that note the pastebin is updated.
https://pastebin.com/ZVLmzaF2
Edited on 08 December 2019 - 05:45 PM
Lupus590 #10
Posted 08 December 2019 - 06:48 PM
for that, you will have to count one stack and the add the count of the next stack and do that for every stack that you go through

here it is with a turtle: https://github.com/lupus590/CC-TreeFarm/blob/d8edf2b9e426a839e53271141ee5ecae4d7f2b2b/treeFarm/libs/utils/invUtils/init.lua#L144
CreeperGoBoom #11
Posted 08 December 2019 - 07:33 PM
Thankyou for the example

From that i devised that I need a table that records the slot number and qty for each slot that contains the matching item.
Then I need to do a count
Then I need to deduct from each slot accordingly and
Last but not least. Make sure my table is cleared after this operation ready for next player to come through the tollway.

How would one get a for statement to continue whilst a repeat until statement is active?

In other words say I is for i=1.10 and I gets to 6
if a repeat gets triggered, will i still count through to 10

Eg:

for i=1.10 do
   repeat
	 chest.pushItem("south",i,amount)
   until topay==0
end
Lupus590 #12
Posted 08 December 2019 - 07:52 PM
when the repeat until finishes it should go on to the next for loop, but your repeat until in that snippet will never end or always run once as topay is never changed in the loop

for loops (without any break or return statements in them) are the same as writing out the internal code the same number of times as the loop would go round. In the case of your code snippet it's the same as having ten copies of the repeat until loop


--# this
for i=1.3 do
   repeat
		 chest.pushItem("south",i,amount)
   until topay==0
end
--# is the same as
local i = 1
repeat
	  chest.pushItem("south",i,amount)
until topay==0
i = i + 1
repeat
	  chest.pushItem("south",i,amount)
until topay==0
i = i + 1
repeat
	  chest.pushItem("south",i,amount)
until topay==0
i = i + 1
Edited on 08 December 2019 - 06:57 PM
CreeperGoBoom #13
Posted 08 December 2019 - 08:02 PM
What are good ways to break a for loop. I had trouble with this in my code so I redesigned it.

But if i do need to break a for loop, specifically a big one that might end up being for i = 1,150 with some of the chest mods available.
Lupus590 #14
Posted 08 December 2019 - 08:21 PM
I forget if break is a thing in lua but putting the loop in a function and returning from the function will definitely work

You might also want to which the order of your loops


repeat
  for i = 1, 3 do
	chest.pushItem("south", i, amount)
  end
until topay == 0

this will loop through the first three slots trying to push items until they have payed

your code would try to see if they payed in the first slot and then make them pay again in the 2nd and then again in the 3rd slots
Edited on 08 December 2019 - 07:24 PM