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

[Lua][Question] Any easy way to create categories in a table?

Started by Novie, 12 March 2013 - 06:21 AM
Novie #1
Posted 12 March 2013 - 07:21 AM
I'm trying to create a crafting program using the Computer Controlled Crafter from MiscPeripheral(http://www.computercraft.info/forums2/index.php?/topic/4587-cc15mc147-miscperipherals-31/). I have a working program but when I started to create the GUI i realized that it would be a lot easier for the player if you could separate some recipes by what mod they are from.

The way i retrieve the recipe info and how many are created per recipe in one string, for example this is the code for a furnace

PatternList["Furnace"]="1:Cobblestone:Cobblestone:Cobblestone:Cobblestone:nil:Cobblestone:Cobblestone:Cobblestone:Cobblestone"
The string is stored in a table and all recipes are in the same table right now and the only way i can come up with is to add an extra piece of string with the catergory at the end and when the GUI is created it has to parse through all the recipes in order to add them to categories. I suspect that will be very inefficient so i was hoping that there is a way that i have missed.

I guess I could also make it work by having strings in each category within a specific table for that category and then all those tables inside one big table. so something like this

PatternList["Vanilla"]["Furnace"]="1:Cobblestone:Cobblestone:Cobblestone:Cobblestone:nil:Cobblestone:Cobblestone:Cobblestone:Cobblestone"
But for one i don't even know if this works and two then i would have to rewrite some of the code to use for each loops every time i go through the PatternList.

I hope my problem came out clear, sorry if I'm rambling and if the English is dreadful.
Lyqyd #2
Posted 12 March 2013 - 08:44 AM
Split into new topic.

There are a couple ways to go about this. One would be to create a subtable for each mod and change all your parsing, etc. Another is to create a separate table that you can use to look up the recipes from an individual mod. For instance,


local recipeTable = {
  furnace = {
    "cobblestone", "cobblestone", "cobblestone",
    "cobblestone", "nil", "cobblestone",
    "cobblestone", "cobblestone", "cobblestone",
    creates = 1
  },
  chest = {
    "planks", "planks", "planks",
    "planks", "nil", "planks",
    "planks", "planks", "planks",
    creates = 1
  }
}

local recipesByMod = {
  vanilla = {
    "furnace",
    "chest"
  }
}

This allows you to keep all of your recipes in one table if you like, while giving you the ability to select a few of them by group.
Novie #3
Posted 12 March 2013 - 09:26 AM
It looks great, thank you.