r/ComputerCraft • • Sep 29 '21

I need help stopping a program.

Hello, I found this really cool Matrix Rain code for computercraft, pastebin here : https://pastebin.com/bXvr0vuN

When I make the program a startup the startup never ends, was wondering if someone could make it stop after a certain amount of seconds. Thanks!

5 Upvotes

7 comments sorted by

5

u/JackMacWindowsLinux CraftOS-PC & Phoenix Developer Sep 29 '21

I'd recommend throwing the program inside a parallel.waitForAny call like this:

parallel.waitForAny(
    function() shell.run("rain.lua") end,
    function() sleep(30) end
)

This will run the program, stopping it when the 30-second timer finishes.

1

u/Icewing_Nix Oct 01 '21

Okay, this works great, but one problem. I want to run another shell program thing right after the matrix rain ends. Nothing seems to work, how would I do that?

Like, this is what I am doing:

parallel.waitForAny(

function() shell.run(".matrixrain") end,

function() sleep(10) end

)

shell.run("OS")

I want the matrix rain to be for the beginning to my OS

1

u/JackMacWindowsLinux CraftOS-PC & Phoenix Developer Oct 01 '21

That should work just fine; just make sure to clear the screen first.

1

u/Icewing_Nix Oct 01 '21

Gotcha, thanks!

2

u/cogspace Sep 29 '21 edited Sep 29 '21

The code has a main loop. You should either count how many times it has gone through that loop and then stop the program after that counter gets to a certain number, or get the current time when the program starts, add however long you want the program to run to that number, then exit the loop when you reach the desired time.

Implementation is left as an exercise to the reader. tweaked.cc has a full function reference. =)

2

u/Icewing_Nix Oct 01 '21

Tried that, if only I had an IQ over -4.

1

u/cogspace Oct 01 '21

To be fair the code is very hard to read because it isn't formatted correctly.

It looks like you already got a solution that works for you, but here's a cleaned up and correctly formatted version that will run for 5 seconds and then terminate: https://pastebin.com/w06Dq474

The change is on lines 40-41 (previously lines 45-46). Basically, I changed this:

local bRunning = true
while bRunning do

Which sets a variable called bRunning to true and then keeps looping as long as that variable is true (which it always will be, since it never gets changed), which results in it looping forever. The new code looks like this:

local iMaxLoops = 50
for i = 1, iMaxLoops do

Instead of running forever, this will only run 50 times. Since each loop takes 0.1 seconds, that will be a total of 5 seconds. If you want it to run a different amount of time, change that number to 10x the desired number of seconds.

The 0.1 seconds per loop is because of line 45:

local timer = os.startTimer(.1)

Hope that helps you understand how this code works!