4

I was trying to program a computercraft trading machine which would let the player select an item after he paid a fixed price. This is my current setup: backfront

This is my current program code:

sort = peripheral.warp("top")
term.redirect(peripheral.warp("back"))
print("Welcome at Alexanders record shop!")
print("Every music disc costs 10 diamonds.")
while dia<10 do
print("You have paid"..dia.."Diamonds.")
end

My questions:

  1. How do I count and extract the diamonds?
  2. How can the player select his item via the redstone buttons?
3
  • 1
    So you want 1. A screen with all the record selections available, 2. The records to cost 10 diamonds and for that number to be displayed on-screen, and 3. To use buttons as a means of moving the selection cursor. Correct?
    – Unionhawk
    Commented Dec 12, 2013 at 23:05
  • @Unionhawk Yes, but if there is a better solution than buttons I would appreciate it because stone buttons stay on for 1 second so the cursor speed is limited.
    – Andalur
    Commented Dec 12, 2013 at 23:11
  • Spelling! Not warp but wrap Commented Jun 16, 2015 at 9:26

1 Answer 1

3

To check how many of a given item is in an inventory in a given direction, you need to use the sorter.list(direction) function, where direction is an integer corresponding to the direction of the inventory in relationship to the sorter (0 for down, 1 for up, 2 for -Z, 3 for +Z, 4 for -X and 5 for +X). This function returns a table, so you've got to use a loop to look through it. I implemented a checkInput() function like so:

function checkInput()
  inChest = sorter.list(inputDirection)
  for id,count in pairs(inChest) do
    if id==currency then
      return count
    end
  end
  return 0
end

Where inputDirection is the direction of the ender chest, and currency is the item ID of the desired payment item (in this case, 264). I did it this way so you can configure the payment later without having to change it everywhere. That returns hoe many diamonds, if any, are in the ender chest.

You can extract items from an inventory and move them to another inventory using the sorter function sorter.extract(inputDirection, id, outputDirection, count). So, for example, to trade 10 diamonds for a specified record item, you could do something like this (after verifying that payment is there, and the record is in stock):

sorter.extract(inputDirection, currency, paymentDirection, price)
sorter.extract(stockDirection, item, inputDirection, 1)

As for the buttons, the use of Advanced Monitors allows the use of touchscreens. I used Direwolf20's Button API, found on pastebin here (use pastebin get S8x0K3ui button to download it directly from the ComputerCraft computer). It can be quite confusing, though. Direwolf has a tutorial for it here, which explains what each function does.

The core function that you're going to be using is button.setTable(name, func, xmin, xmax, ymin, ymax), where func is the name of the function that you want the button to perform when you push it. You need to do this for each button you want to have and use. For example:

button.setTable("^", cursorUp, true, 26, 27, 5, 8)
button.setTable("v", cursorDown, true, 26, 27, 13, 16)
button.setTable("Purchase", purchase, true, 10, 19, 18, 19)

(those positionings happen to work well on a 3x3 monitor setup).

I used a cursorPos variable to track where the cursor was, and modified that variable accordingly with cursorUp() and cursorDown(). The function purchase() uses the current cursor position as a reference to which item is selected, and makes the trade.

Monitor touch is a pullEvent that can be used to trigger an action. Like so:

while true do
  display()
  event, side, x, y = os.pullEvent("monitor_touch")
  button.checkxy(x,y)
end

I am very sorry that this answer is probably a tad longwinded and confusing. ComputerCraft can do that sometimes. In my tinkering with the various APIs needed, I actually wrote a storefront script, which can be found on pastebin here. Whether you want to use that, or look at it a little then figure it out on your own, go ahead. The latter is a much better practice in my opinion.

You may want to check out the ComputerCraft Wiki for more on the API involved. Be sure to check out Direwolf's tutorial on the Button API as well, since he's much better at explaining his code than I am. And, finally, though the documentation is a little scarce, there is some documentation on MiscPeripherals on the ComputerCraft forums, found here.

3
  • After I looked at this I decided it would be way too difficult to reprogram all that myself so I've tried to use your program (with the button API set to the right direction), but it returns "startup:25: bad argument: table expected, got nil" and I have absolutely no clue what could be wrong with line 25...
    – Andalur
    Commented Dec 13, 2013 at 3:38
  • 1
    @Andalur Fair enough, lol. Line 25 fails if there is no chest in the direction specified. You're going to need to change inputDirection as appropriate, depending on where the ender chest is in relationship to the sorter.
    – Unionhawk
    Commented Dec 13, 2013 at 5:20
  • I was a bit confused about the direction numbers but now it works.
    – Andalur
    Commented Dec 13, 2013 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.