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

Computer-Based Billing System

Started by Cain, 02 June 2013 - 03:42 PM
Cain #1
Posted 02 June 2013 - 05:42 PM
Hi,

On my server I am trying to set up some sort of system that uses enderchests to pay for a subscription to a tesseract. I have it set up so that it only needs a redstone signal to turn the items on or off just fine, but I can't seem to get the interactive sorter part figured out.

I would like it so that whenever certain items are in the chest, that it will add a timer for 48 hours before the item gets shut off. How would I go about doing this? Thanks.
Bomb Bloke #2
Posted 02 June 2013 - 07:33 PM
turtle.compareTo() can be used to identify your items, but you need to pre-populate the turtle's inventory with samples of those items. CC can't identify things by ID numbers (by design), though some addons allow this.

os.startTimer and os.pullEvent can be used to start counting your 48 hour period and catch it when it ends. Because the turtle/computer normally cannot catch events while doing other things (eg checking for new items in the chest), you need to have it multitask.

Although you wouldn't know it from the wiki page, use of parallel functions allow you to effectively duplicate the event queue, letting two functions pull from it at once without blocking those events from the other function.

This is handy when you realise that certain functions pull events from the queue, including unrelated events (eg your timer events) - meaning you might not be able to pull them yourself, so you never know when your timers have expired. None are documented as doing so, so have fun figuring that out on your own, but as a starting point I can tell you most turtle functions do this. Use of the parallel API solves all this.

More importantly, in your case, it allows you to have one function sit and do nothing (wait for timer events to fire) while another does actual work (starts new timers based on what the chest is doing).

local timerlist = {}  -- We'll use one index per type of item in the turtle's inventory.

local function main()
  turtle.select(1)

  while true do
    -- Pull stuff from the chest into the first slot of the turtle.

    for i=2,16 do if turtle.compareTo(i) then -- Try to match the chest item with something the turtle's carrying.
      timerlist[i] = os.startTimer(172800) -- Start a timer to end two days from now.

      -- Enable your redstone signal for the item matching the type in turtle slot i.

      break
    end end

    -- Send that item away somewhere.
  end
end

local function catchTimers()
  local event,timerID = "",0

  while true do
    event, timerID = os.pullEvent("timer")

    for i=1,table.getn(timerlist) do if timerlist[i] == timerID then
      timerlist[i] = 0

      -- The timer for the item type matching whatever's in turtle slot i
      -- just expired, so toggle your redstone signal off or whatever.

      break
    end end
  end
end

parallel.waitForAny(main, catchTimers)  -- Run the two functions at once.
Imque #3
Posted 03 June 2013 - 02:17 AM
You just want to make sure that the location of the computers used are loaded within there respective chunk.
Cain #4
Posted 03 June 2013 - 07:36 AM
Thanks guys, I'll give this a go and let you know what happens.