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

how can i program a computer to count how many times a minecart past

Started by andriesmenze, 27 February 2015 - 09:37 PM
andriesmenze #1
Posted 27 February 2015 - 10:37 PM
i want to make a computer that counts how many time a minecart past

(wen de minecart past there is a redstone signal i want a codering that counts how many times there was a redstone signal)

i am a noob when i program a computer in minecraft so i need a full tutorial (when you don't have a full tutorial its not a problem mebi it helps what you saying :)/> )
HPWebcamAble #2
Posted 28 February 2015 - 02:04 AM
You'll probably want to use os.pullEvent()
It stops your program until something happens:
-Redstone update
-User input (like pressing a key or clicking the mouse)

These are the only ones that happen by default, but there are more when a peripheral is connected (like a monitor or modem)

So, the program

local mcPasses = 0 --#stores the number of passes
local redstoneSide = "right" --#Which side the detector rail/redstone is

while true do --#runs forever
  os.pullEvent("redstone") --#calls os.pullEvent, waiting for only the 'redstone' event
  if rs.getInput(redstoneSide) then --#Only increments when the signal turns on (Event also fires when it turns off)
    mcPasses = mcPasses+1
  end
end

That will do it. Of course, nothing will happen on screen, since the program never updates anything.
If you want, you can add a 'print(mcPasses)' after it increments, to display the number


NOTE: Usually, os.pullEvent() returns several things, but for the 'redstone' event, nothing important is returned.

NOTE 2: You did ask for a tutorial, not the program, but since it was so short, I just wrote it.
Edited on 28 February 2015 - 04:49 PM
Bomb Bloke #3
Posted 28 February 2015 - 11:56 AM
Bear in mind that you'll get an event every time a redstone signal activates, and every time a redstone signal deactivates.
andriesmenze #4
Posted 28 February 2015 - 03:18 PM
thx i gone try dis

You'll probably want to use os.pullEvent()
It stops your program until something happens:
-Redstone update
-User input (like pressing a key or clicking the mouse)

These are the only ones that happen by default, but there are more when a peripheral is connected (like a monitor or modem)

So, the program

local mcPasses = 0 --#stores the number of passes

while true do --#runs forever
  os.pullEvent("redstone") --#calls os.pullEvent, waiting for only the 'redstone' event
  mcPasses = mcPasses+1
end

That will do it. Of course, nothing will happen on screen, since the program never updates anything.
If you want, you can add a 'print(mcPasses)' after it increments, to display the number


NOTE: Usually, os.pullEvent() returns several things, but for the 'redstone' event, nothing important is returned.

NOTE 2: You did ask for a tutorial, not the program, but since it was so short, I just wrote it.


it say counter: 4: attempt to call nil
wath dit i wrong i don't know anything about computercraft
HPWebcamAble #5
Posted 28 February 2015 - 05:50 PM
you'll get an event every time a redstone signal activates, and every time a redstone signal deactivates.

Right. Fixed in my first post
andriesmenze #6
Posted 01 March 2015 - 03:11 PM
do i neat to set the computer next to the detector rail?

it say's

bios:339: [string "counter"] :4: '=' expected

what did i wrong again
HPWebcamAble #7
Posted 01 March 2015 - 08:01 PM
do i neat to set the computer next to the detector rail?

it say's

bios:339: [string "counter"] :4: '=' expected

what did i wrong again

The code I posted should be fine, but you might have misspelled something when you transferred it to the computer.
Remember that capitalization matters

If you don't see a typo, copy and paste the code here, so we can see the exact code.

Also, you don't HAVE to have the computer next to the detector rail, but they do need to be connected with redstone if they aren't touching.