r/ComputerCraft • u/flauntin • Dec 12 '16
Nested programs or functions?
I'm writing a utility program that will give me about 20 different functions that the computer can perform all red stone related.
Each function is relatively small, is it better to split them into seperate programs to be called on or have a small api of functions that the main program can use.
Also i need the program to persist after restarts etc so what is the best way to do this.
2
Upvotes
1
u/akhener Dec 12 '16 edited Dec 12 '16
You should get to the point where the cost of RP gates is negligible very soon. it is just some stone and redstone in the FTB packs, right? But nonetheless doing it using CC is much more fun!!!
Sidenote: If you don't play at a public server so have access to the game directory add your finished program to the ROM so every computer automatically has your program. Yes it is cheating but copying the file to every logic gate computer could ruin it for you. You can find tutorials on google (This is a older one. Don't know if it still works)
Now to your prgoram. The way you describe your program does make sense :) You save this configuration in an extra file called "config". If this file exists and there is a valid configuration inside your program should act as the peripheral. Otherwise it will go into configuration mode. To change the peripheral type you can use a parameter maybe "config". So you run e.g. "gate config" and it goes into config mode. In your startup file you just have shell.run("gate") or something.
This is a pretty rough sketch. I'm happy to help if you need tips for a specific part.
PS: I just noticed that your original question was how to structure your gate functions. I see your point that having 20+ small functions in one file seems ugly but I would go for it for now. Maybe have just a single second file with all your gate implementions.
You could then do os.loadAPI("gates"), gates.timer(), gates.NOT() if you have functions timer() and NOT in the file gates. Maybe don't call your main program gates in this case.
If you want to go really crazy you could split the functions timer and NOT to seperate files. Maybe do a folder "gates" and in this folder do a file that fs.list to dynamically get all functions and load them. http://pastebin.com/vicQX5GP
EDIT: Forgot config mode EDITII: It just occurred to me that it might also be a great idea to add the description and possible options to each gate into the file in the gates folder. so you don't need to keep one big table up-to-date manually.