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

making a program that creates a program

Started by Treky123, 02 February 2013 - 05:40 AM
Treky123 #1
Posted 02 February 2013 - 06:40 AM
Hello, I am making an item "vending machine" using computer craft computer and red-power machines, the way I have made the system is such that I need one chest for every type of item (gold chest, egg chest, copper cable chest). Anyways I am also writing a separate program for each item (the code is not important here). But I was wondering if their is a way to write one program, that would allow me to create these small programs for each item (I would write in Notepad++ and put it into the CC server programs folder. But basically a program that when run would ask for what item, and the color of cable then after those were entered it would create the program to retrieve that item on only that computer (the item name, and cable color are the only things that change program to program). thank you
Lyqyd #2
Posted 02 February 2013 - 06:43 AM
Split into new topic.

Seems like you may be going about this the hard way. It strikes me that a table of the relevant information might be easier.
Treky123 #3
Posted 02 February 2013 - 06:56 AM
I apologize however I am very new to any type of programming, how would one create and use a table of relevant information?
Lyqyd #4
Posted 02 February 2013 - 08:49 AM
Since the item name and cable color are the only things that change, you could use a table containing tables of the item name/cable color pairings. Or simply use the item name as the key and the cable color as the value.

So, you might do this:


--declare a table
itemTable = {
  --use [""] notation because of the space.
  ["iron ingot"] = colors.magenta,
  diamond = colors.cyan,
  --other entries do not need [""], but could also use it.
  bucket = colors.lightGray,
  steak = colors.red,
  --all table entries must have a comma after them, except the last one is not necessary.
}

--now we want to get a diamond.
rs.setBundledOutput("back", itemTable.diamond)
sleep(1)
rs.setBundledOutput("back", 0)
sleep(1)

--now we want to print all of the available item names.
for name, colorValue in pairs(itemTable) do
  print(name)
end