Using lists in constructors

I needed a program that could pick from a bunch of ingredients to create random omelet recipes, because I’m record breaking lazy.

Each ingredient needed to have a name, some categories, and a rank, so that the user can distinguish between ingredients, opt to only have ingredients with certain categories(Like Veggies only) and influence the frequency of certain ingredients that they like.

I thought maybe I would have a recipe script that would generate the recipe by picking from a list and editing the text component of the text object the script is on. I’m still testing things out because I’ve never done these kinds of things and I’m trying to learn.

I made the Ingredient class, which does not inherit from MonoBehavior.

public class Ingredient{

    public string itemName = "";
    public string[] categories; //Struggling to implement these
    public int itemRank = 10;

    public Ingredient(string newItemName, int newItemRank)
    {
        itemName = newItemName;
        itemRank = newItemRank;
    }
}

And here is the recipe class.

public class Recipe : MonoBehaviour {

    public List<Ingredient> ingredients = new List<Ingredient>(); //Uses my class as the type
    public Text recipeUI;

    void Start()
    {
        ingredients.Add(new Ingredient("Bacon",10)); //Makes an ingredient called bacon with a rank of 10
        recipeUI.text = ingredients[0].itemName;
    }
}

Things seem to be working the way I want them to, despite it being a little over my head. I just can’t think of how to add categories. I thought maybe I could add the categories array to the Ingredient method as a constructor, but then I don’t know how to pass the category information when I add new items to the list, and I imagine that would be very messy anyways. How might I implement categories to the ingredients?
Or if you have any ideas on a better way to make my program I’m all ears.

EDIT: I simply added the categories afterwards, and now I feel silly. Since I can’t seem to delete this thread, I’ll leave it for criticisms of my methodology.

Don’t use strings for your categories, that is guaranteed to lead to problems down the road. Try an enum or maybe even a separate class.

It’s also perfectly fine to pass the categories to the constructor, I don’t see any reason why you should avoid that.

I was completely stuck trying not to use strings. I couldn’t use enums because I wanted the user to be able to add categories, plus I wanted ingredients to be able to have multiple categories(like “Meat” and “Gluten Free”) . I finally figured something out that I believe will work for me in every way, but I’m still using strings in lists.

ingredients.Add(new Ingredient("Bacon", new List<string> {"Meat",  "Blah"}, 10));

Can you tell me what pitfalls you imagined I’d run into using Strings?

Or maybe shed some light on making and using a Category class? What I did here with the ingredient class is very new stuff to me. I’m writing code I hardly understand.

The main problem you will run into in this case is that strings are very inflexible, additional problems can arise from spelling mistakes and different letter cases etc. In addition, strings are slow, and in Unity will generate garbage on common operations.

There is another problem with your approach here, which is that you create a new list of categories for each ingredient. You instead want to maintain a separate list of categories, and just add references to these from your ingredient. Like now, if you add one bacon and one steak, they will both have a category for meat, but it won’t be the same category.

Basically what you are trying to achieve here is some kind of database, maybe you would be better off using something like SQLite, which has the added benefit of persistent storage.

you might be interested in this:

(somewhere around the middle he discusses using them as replacements to enums/types)

advantage is then that the “categories” are just assets the users can add to their project.