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

run program on startup?

Started by mikesaa309, 17 June 2013 - 04:08 PM
mikesaa309 #1
Posted 17 June 2013 - 06:08 PM
I have made a train station in minecraft which help from the mod railcraft. I've decided to use computer craft to control the trains etc. One of the functions of the program is to tell me what stations are occupied by trains and when a train leaves a station. This all works fine however everytime I load the world up I have to go round starting all the computers and running the programs needed. Is there a way to ensure the programs are running when I open the world? If not is there a way I can turn on all computers from one computer in like a control room kinda thing and set it up so the right programs launch on startup (i know you can do this, but i want to be able to turn on the computers from one computer but have no idea how to)
SuicidalSTDz #2
Posted 17 June 2013 - 06:51 PM
The Rednet API will help you, read about it
Bomb Bloke #3
Posted 17 June 2013 - 07:00 PM
That can't be used to tell other computers to start up.

If a computer/turtle has a script called "startup" on its drive, it should run that when the chunk it was in reloads, assuming that computer/turtle was running when the chunk unloaded.
SuicidalSTDz #4
Posted 17 June 2013 - 07:06 PM
That can't be used to tell other computers to start up.

If a computer/turtle has a script called "startup" on its drive, it should run that when the chunk it was in reloads, assuming that computer/turtle was running when the chunk unloaded.
He shouldn't have said 'control room', that mislead me <_</> He should just have one program, named 'startup', that will run on startup and do all the things the other programs would have done. (If that is not what you meant, then sorry)
mikesaa309 #5
Posted 18 June 2013 - 04:20 AM
Basically i have a station and a like control room. in the station is a computer with a program on it which asks me for the name of the station and the id of the computer i want it to send information to. This works fine but everytime i close the world or minecraft and then come back to it i have to go round turning the computers on and starting the program and then telling it the name of station and id. Is there no way it will stay running when i exit the game?
Bomb Bloke #6
Posted 18 June 2013 - 07:50 AM
No, there is not. Closing the world shuts down the computers.

Some options:
  • Set the world up on a server, running on one of your home machines. Connect to that server whenever you want to play in your world. The world can hence keep running while the MineCraft client is closed.
  • Use the modem API so you can specify any ports you like for transmissions, instead of having to worry about computer IDs.
  • Stop changing your computer IDs and hard-code them all in.
  • Have your computers save the IDs to a separate file on their drives when you type them in, and load them from there on subsequent boots.
  • An idea that I've mentioned in another thread is to make use of rednet.broadcast() to allow the computers to work out each other's IDs on their own.
diegodan1893 #7
Posted 18 June 2013 - 10:58 AM
Closing the world doesn't shutdown the computers. Well, it does, but when you enter the world again they turn on automatically.

Your problem is that your program requires user interaction everytime you open it. This can be solved if you save the variables in a file and load them on startup.

For example if you want to save the ID and the name of the station you can do this:

function save()
   hWrite = fs.open("config", "w") --#"config" is the name of the file, "w" is the mode, in this case "write" mode.
   hWrite.writeLine(id) --#we save the ID to the file
   hWrite.writeLine(name) --#we save the name of the station
   hWrite.close() --#ALLWAYS close the files
end

And you cand load these variables with this function:

function load()
   hRead = fs.open("config", "r") --#again, "config" is the file and "r" is "read" mode.
   id = tonumber(hRead.readLine()) --#readLine() will return a string, if you want the id to be a number variable you need to use tonumber()
   name = hRead.readLine()
   hRead.close()
end

In this case you don't need the save function, just write the config file by hand, but it can be helpful if you want to save more things.