r/ComputerCraft • • 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

8 comments sorted by

1

u/flauntin Dec 12 '16

So if I load api on startup and then also have a shell.run on a outside program the will be modified to say what to run each time would that work.

Also how does one create a program from within a program?

1

u/flauntin Dec 12 '16

Sorry I'm not a programmer so my terminology sucks a bit, let me explain what I want to do.

So I realised that a computer is the cheapest of the modded red stone logic gate systems over redpower and any other timer I've found. I want to make a utility which has all the logic gates and red stone utilities I can make in one block.

I want to place the computer, run the program, select the gate and then just leave it to always work in that way. Does that make sense

So whichever gate I choose I want to persist whenever the computer restarts or reloads

1

u/akhener Dec 12 '16 edited Dec 12 '16

So I realised that a computer is the cheapest of the modded red stone logic gate systems over redpower and any other timer I've found.

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.

  local args = {...}
  local userConfigMode = #args == 1 and args[1] == "config"

  function readConfigFile()
     if fs.exists("config") then
         -- TODO: Read and return config
         -- Maybe check that it is valid and return nil otherwise
         -- You should return the object you run should use below here
        -- It is a good idea to really use a table {} here so you can't only save the name of the 
        -- gate but for example also the timer interval
        -- use textutils.serialize/textutils.unserialize to turn your table into a string
     end
  end

  function writeConfigFile(config)
     -- TODO: Write config file
  end

  function userConfiguration()
      print("Which logic gate do you want to use?")
      -- TODO: Print list of logic gates and read response
     -- Make nice UI: GO wild here!
  end
  
  -- gets supplied valid config (has to be valid) and does the real job
  function run(config)
      if config.gate == "timer":
         .-- TODO Implement timer. 
  end

  local config = readCOnfigFile()
  if config and not userConfigMode then
      run(config)
  else then
     userConfiguration()
  end

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.

1

u/flauntin Dec 13 '16

Ok my head is now spinning but thank you for all your help, I haven't worked with configs before but I want to make this my most polished program I've done and be thorough so this really helps, might need to ask you specifics when I get round to implementing

0

u/[deleted] Dec 13 '16

[deleted]

1

u/flauntin Dec 13 '16

Yeah sorry, I am mostly on Reddit on my mobile and the big button is the wrong one.

I am currently working on the API and testing the functions to get the as good as I can, then once I have the working logic I will go through to make them more configurable. Then I will work on a UI(dreading I have no idea how to make UIs) also I will be sharing this when it's done. Any advice is greatly received and if you can think of any really useful red stone circuits or logic that aren't obvious let me know as well

1

u/revereddesecration Dec 12 '16

have a small api of functions that the main program can use

This.

Also i need the program to persist after restarts etc so what is the best way to do this.

There are no good ways to do this. There are some bad ways that involve serialising the state of the program into a file but this can fail.

1

u/flauntin Dec 12 '16

What I was thinking is having the program write to startup.

Say we are using the nested programs Shell.run("function")

Or if I use the api Shell.run("api") While true do Api.function() End

That's if it is possible to write to startup from another program I haven't got that far yet

4

u/revereddesecration Dec 12 '16

Don't write to startup. Have a startup program that reads from a file that you write to.