Custom definition

Started by Danetta, January 18, 2017, 08:17:09 PM

Previous topic - Next topic

Danetta

Now it works, thanks everyone.

Danetta

#16
Got another question about these custom defs.
This is part of the code I am using:
foreach (ThingDef_Alcohol alcoDef in from def in DefDatabase<ThingDef_Alcohol>.AllDefs
                                          where def.category == ThingCategory.Item
                                          select def)
            {
                if (sel.TrueForAll((IAlcoToFermentSettable x) => GenAlco.CanFermAlco(alcoDef, x)))
                {
                    yield return alcoDef;
                }
            }


I adjusted code from Growing Zones to work with barrels and it works. Filtering items by tags also works (we also only finding ThingDef_Alcohol ones), but I also need to filter them by Fermentation Zones in case of diffrent kind of barrels.

Obviously when I do "Thing thing = obj as Thing;" on "CanFermAlco(alcoDef, x)", I can't use my fields as thing.def.alcoholTag, while they are declared as:
    public class ThingDef_FermentingBarrel : ThingDef
    {
        public ThingDef defaultAlcohol;
        public string alcoholTag;
    }


I thought about something like adding derived class "Thing_FermentingBarrel : Thing" and add it's "ThingDef_FermentingBarrel" as a field, but Building_FermentingBarrel already derived from building, while building derived from thing.

So.. well, how to cast an obj so I will be able to use new fields via "thing.def.newfield"?
Currently "IAlcoToFermentSettable" interface used only by "Building_FermentingBarrel", so casting it to "Thing" is kinda pointless.

If I do something like "Building_FermentingBarrel thing = obj as Building_FermentingBarrel;", I still can't access my fields directly and I also can't do anything like "this.alcoholTag = (def as ThingDef_FermentingBarrel).alcoholTag;" in this scope. Maybe I should do that in constructor and then access it's member variable to get the value of field?

I see a lot of workarounds, but I want to do that in a right way, that is why I need help.