def property of modded building is null

Started by SBP, October 13, 2017, 06:18:29 PM

Previous topic - Next topic

SBP

Hi!

I've created a test mod with a new workbench and have hit a brick wall trying to add bills to it. This error message happens when I select it and click on the 'Bills' tab:

Exception filling tab RimWorld.ITab_Bills: System.NullReferenceException: Object reference not set to an instance of an object
at RimWorld.ITab_Bills.FillTab () [0x00000] in <filename unknown>:0


I looked at ITab_Bills.FillTab in the RimWorld assembly and noticed this part:

for (int i = 0; i < this.SelTable.def.AllRecipes.Count; i++)

So I did some console logging and found out that the 'def' of my workbench is null which would explain the error. I'm confused because of this part of my XML:

  <ThingDef Class="MyMod.ThingDef_MyWorkBench" ParentName="BenchBase">
    <thingClass>MyMod.Buildings_MyWorkBench</thingClass>   


I assumed this would populate that reference? What am I missing here? I've defined some recipes further down in the XML and I'm not getting any other errors.

Thanks


CannibarRechter

Maybe look into the [DefOf] decorator and how various other mods use that?
CR All Mods and Tools Download Link
CR Total Texture Overhaul : Gives RimWorld a Natural Feel
CR Moddable: make RimWorld more moddable.
CR CompFX: display dynamic effects over RimWorld objects

SBP

OK - I've created a MyModDefOf class and it's recognizing the ThingDef name, so the def property is being filled  :) I can see the number of recipes when I write `this.def.AllRecipes.Count`to the log, and it corresponds to the XML, so that's progress. Sadly I'm still seeing a null pointer exception at the same point, in the same method (maybe on a different line but I can't tell...). I've tried to check all the things it looks like it's reading, to see if they're null, but didn't find any. Weirdly, it does the same thing when I comment out all the recipes in the XML, which should cause that loop to be skipped and narrow down the possibilities.

To be honest I was semi-following the smelter tutorial, which I'm aware is quite outdated. Seems to be ok for setting up a constructable building, but not adding behaviour to it, as of A17.

Albion

What kind of bill are you trying to add anyway? If it's a simple production workbench like the machining table you could drop all this and just add a recipe, worktype and jobGiver and you're all set.
No need to add c# code or anything since that might impact performance.

SBP

I don't think that will work long-term - the building I'm hoping to have in the finished mod will need to also be a ThingOwner like the cryptosleep casket, and the list of recipes will ideally be dynamic, based on which nearby buildings it's linked to.

I'll try to get a simple XML-only test project working and take it from there...

CannibarRechter

#5
How are you making your thing in your code? Look at ThingMaker.cs. If you call MakeThing, it will throw an exception if the thing def is null, and will set the def on the thing. I don't think you're supposed to directly "new" a Thing. Anyway, how is your thing made?

I've encountered a cpl of times the same situation as you. I wish I could recall the situation, because this makes me think either there is a core bug somewhere, or there are modders doing something in a bugged way.
CR All Mods and Tools Download Link
CR Total Texture Overhaul : Gives RimWorld a Natural Feel
CR Moddable: make RimWorld more moddable.
CR CompFX: display dynamic effects over RimWorld objects