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

[LUA] Coding a file picker

Started by TheGeek, 19 December 2012 - 12:45 PM
TheGeek #1
Posted 19 December 2012 - 01:45 PM
I'm working on a very large and complex project that will run instructions from a file. However, I'm at a loss on how to implement a file selection mechanism. Something that will read how many, and what files are in a given directory, and then prompt the user to pick one using two redstone buttons, one to switch files, and one to choose; all displayed on a monitor. It likely involves arrays and or tables, which I don't have much experience with yet. It would also be useful to read say the first line or two of each file and place it into an array, to display under the filename, say a description or somesuch. Once picked, it will set the chosen filename to a variable, and then the program would then load the file and start reading instructions from it (this ive already almost figured out).

I'm not looking for complete code, but some examples on how to get the list of files, then pick them would be greatly appriciated.
Lyqyd #2
Posted 19 December 2012 - 02:07 PM
  • fs.list will return a table of the files in a directory.
  • A for loop can iterate through this table, allowing you to grab a bit of each file.
  • Another table can contain the information for display on the monitor.
  • A numeric variable can keep track of which index is selected, allowing you to easily index the table correctly to get the chosen filename.