Adding resource cost to Research

Started by Dr_V, February 23, 2015, 05:37:42 AM

Previous topic - Next topic

Dr_V

Has anyone tried to add resource costs to research (e.g. requiring 100 steel to research turret cooling)? I'm currently looking at how best to implement such a feature. From what I can tell the only way to do this is to bypass the existing research system and instead (awkwardly) implement it as a one-off recipe. Before I head too far down this road, I wanted to check if anyone can see a way I have missed to add it to the existing system.

Gaesatae

Yep, did that in A8 but I suspended the work on the mod until RW is out of alpha. If what you're trying to do is like vanilla research you'll need a workgiver, jobdriver and optionally a new researchdef class to include the research material cost easily.

You can do very interesting things once you get the hang of the jobdriver.

Dr_V

Can you clarify if you've managed to integrate it with existing system? And in any case could you go into a bit more depth as to how you went about it? Making a researchdef subclass seems counter-intuitive as I can't see a way for the code to ever call it.

Gaesatae

As I said, using a custom Def is optional, but I was planning to expand and link the research system to other areas of the mod, and using the Defs gave me flexibility. No integration of the Defs with the ResearchManager, I was using them to store the research material cost at the time.

For example, the workgiver will tell the pawn on what conditions it will create the job, mine was an identical copy of the research workgiver but with a couple of lines added to it to tell the jobdriver to use my Researchprojectdefs.

IEnumerator<NewResearchProjectDef> enumnresearchdef = DefDatabase<NewResearchProjectDef>.AllDefs.GetEnumerator();
            NewResearchProjectDef currentresproj = new NewResearchProjectDef();

            if (Find.ResearchManager.currentProj == null)
            {
                return null;
            }
            else
            {
                while(enumnresearchdef.MoveNext())
                {
                    if (enumnresearchdef.Current.label == Find.ResearchManager.currentProj.label)
                        {
                            currentresproj = enumnresearchdef.Current;
                        }
                }
            } 

            if (currentresproj != null)
            {
                 return new Job(DefDatabase<JobDef>.GetNamed("AltResearch", true), new TargetPack(t), currentresproj);
            }
            return null;


My jobdriver code is a mess now that I look into it after a few months, and I'm sure it will cause problems under certain conditions unless I remember what I was thinking when I wrote dozens of seemingly stupid lines. But the idea is the same, get the things and materialcost data from the new defs the workgiver returned, bring it to the table (I used the construction jobdriver for reference), and do whatever you want with the items you carry near the table. In my case, I was using an item similar to an unfinished thing to store the materials and other data I needed to save. Once the materials are there the rest of the jobdriver is the same as the research one.

Maybe there's an easier way, but it worked for what I was planning on doing. Oh god, what a mess I made... I'll better stop looking at my code before I start crying.