3 posts
Posted 27 August 2012 - 11:54 AM
I've been trying to make a system where the user inputs bars (currency) through pipes which are detected by an item detector which then inputs a pulse to the computer which then displays the currency on screen. I've searched through loads of tutorials on here but haven't found any that work. i'm new to Lua so I'm slightly confused. Any Help?
137 posts
Location
the Netherlands
Posted 27 August 2012 - 09:24 PM
I don't know much about pipes/tubes but I made a vending machine once, so I might help.
This is the setup (from the side)
. . . S #
. . F T #
. . # C #
# # # # #
# = solid block
S = Screenblock
F = Floppy drive
C = Chest
T = Turtle looking to the left
Rough code (I am too lazy to test xD):
money=0 --Amount of money
drive = peripheral.wrap("front") -- Saving the diskdrive as table
screen = peripheral.wrap("top") -- Saving the screen as table
while true do
turtle.select(1)
print("Sucking until I get something")
repeat turtle.suck()
until turtle.getItemCount(1)>0
if turtle.compareTo(16) then
print("This is a bar")
turtle.dropDown(1) -- Store one item in the chest below
-- Writing the new currency on the monitor:
money=money+1
screen.clear()
screen.setCursorPos(1,1)
screen.write("Mon: "..money)
else
print("This is not a bar")
end
drive.eject() -- empty the diskdrive
turtle.drop() -- Empty the turtle
drive.eject()-- Ejecting again
end
It is important to put a 'bar' in the last slot of the turtle. It will compare with that.
I am now making the machine in SP.
Maybe I made same errorsIt works! Here is a screenie:
137 posts
Location
the Netherlands
Posted 27 August 2012 - 09:40 PM
i'm new to Lua so I'm slightly confused.
Oops I didn't read that. I could explain the code line per line if you want?
3 posts
Posted 28 August 2012 - 11:48 PM
Would be helpful if you could! Thanks in advance<3
137 posts
Location
the Netherlands
Posted 29 August 2012 - 12:18 PM
Here s the script with some more comments:
-- Things starting with -- are comment and not executed
money=0 --Amount of money
drive = peripheral.wrap("front") -- Saving the diskdrive as table
screen = peripheral.wrap("top") -- Saving the screen as table
-- The screen table has the same functions as 'term'
-- see 'help term' to see the functions
while true do
-- Keeps repeating this block while 'true' is true (always)
turtle.select(1)
-- Selects the first item slot in the turtles inventory
print("Sucking until I get something")
repeat
-- Repeats the block below
turtle.suck()
-- Sucks the item in the diskdrive, into the turtle.
until turtle.getItemCount(1)>0
-- If there are no items sucked up, it will go back to 'repeat' and try again.
-- If there are more than 0 items, it will continue
if turtle.compareTo(16) then
-- Compares the selected slot, with slot 16. If they are the same, it returns true otherwise false
print("This is a bar")
turtle.dropDown(1) -- Store one item in the chest below
-- Writing the new currency on the monitor:
money=money+1
screen.clear()
screen.setCursorPos(1,1)
screen.write("Mon: "..money)
else
print("This is not a bar")
end
drive.eject() -- makes the diskdrive throw out its content
-- (in case the user inserted another item very quickly)
turtle.drop()
-- Put item in the turtle, back into the floppy drive
drive.eject()-- Ejecting again
end
-- This 'end' ends the while loop. So it will start over again from 'while'