Making a new RecipeDef at runtime. How do you set its private properties?

Started by Shifted, January 26, 2017, 01:45:36 AM

Previous topic - Next topic

Shifted

If I wanted to make a RecipeDef at runtime programatically, is there a way to set its "ingredientValueGetterClass" private property somehow? Or perhaps there's something I'm missing here, maybe a better way of creating RecipeDefs at runtime other than instantiating the object and adding it to DefDatabase<RecipeDef>. I'm also wondering how you'd go about clearing the AllRecipes cache on building defs like the cooking stove.

Thanks!

RawCode


skullywag

Shifted, can I ask what it is youre trying to achieve, just to be sure there is a definite need for what youre asking?
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

Shifted

Thanks for the responses. I guess I wanted to make sure there wasn't a less hacky way of doing it than using reflection. After looking at other mods, it seems some people are even straight up editing memory to detour methods from the assembly so I guess "hacky" is the name of the game in here.

I'm making a mod that allows you to combine any ingredient and creates procedural recipes and items from each combination, randomizing the effects for each new game. It's my first venture into Rimworld modding (though not to programming) and I'm still trying to figure out the entire architecture of it and how things happen.

So far, I've managed to make a proof of concept prototype; though there's still a few details I'm trying to figure out. I ended up using a combination of hoppers and a normal work table that lets a pawn combine the materials and produce a "Recipe" item that saves ingredient information. This creates a new "recipe template" for that particular combination on a global static registry that verifies it's not a duplicate, and gives the template a new name (eventually random stat buffs once I figure that out hopefully). A pawn can then activate the recipe item through right-click (similar to artifacts) and that causes the "learning" to happen. When learning happens, it generates a new RecipeDef and ThingDef using the previously generated recipe template on the registry.

In regards to my question, I ended up just needing to add the new RecipeDef to the ThingDef.AllRecipes list of the appropriate users to make it work. The ingredient getter property seems to default to the volumen type, which is fine for what the new recipes are doing, don't really need to use nutrition; so thankfully I managed to avoid having to use reflection, which I've never liked using. Though there's still some things I'm not sure about, like how does this get affected by save/load of games. The idea would be to make sure these recipes and items persist through reloads, but not for future new games. I'm looking at HugsLibs right now, which seems to have a convenient way to save data on the save file that I might be able to use, though strangely enough from the tests I've made it seems once the new RecipeDef is added to the DefDatabase<> saving and reloading the game maintains the new recipe.

Here's a quick video showcasing the prototype, I'll clean it up a bit and make a github repository soon in case anyone else is interested in contributing.
https://youtu.be/A83hKDmVsl0