How to properly define ThingDefs?

Started by lost_RD, June 09, 2016, 06:26:25 AM

Previous topic - Next topic

lost_RD

I'm working on a mod that copies much of the SteamGeyser code to create oil gushers. The idea is that you build a pump jack on the gusher and then pawns can use the pump to produce barrels of oil.

This is the error:

Object reference not set to an instance of an object

This is the class throwing the error:

using Verse;

namespace BlackGold
{
    public class PlaceWorker_OnOilGusher : PlaceWorker
    {
        public override AcceptanceReport AllowsPlacing(BuildableDef checkingDef, IntVec3 loc, Rot4 rot)
        {
            Thing thing = Find.ThingGrid.ThingAt(loc, ThingDefOf.OilGusher);
            if (thing == null)
            {
                return "Thing is null: " + thing.ToString();
            }
            if (thing.Position != loc)
            {
            return "MustPlaceOnOilGusher".Translate();
            }
            return true;
        }
    }
}


I believe that I have need to define some ThingDefs to make this work. Here is my ThingDefOf.cs:

using RimWorld;
using Verse;

namespace BlackGold
{
    public static class ThingDefOf
    {
        public static ThingDef OilGusher;

        public static void RebindDefs()
        {
            DefOfHelper.BindDefsFor<ThingDef>(typeof(ThingDefOf));
        }
    }
}


Defining a ThingDef in ThingDefOf did not fix the issue though.

Any thoughts?

1000101

#1
Your RebindDefs() is never actually called.

You need to add [StaticConstructorAtStartup] to the class.

using RimWorld;
using Verse;

namespace BlackGold
{
    [StaticConstructorOnStartup]
    public static class ThingDefOf
    {
        public static ThingDef OilGusher;

        public static void RebindDefs()
        {
            DefOfHelper.BindDefsFor<ThingDef>(typeof(ThingDefOf));
        }
       
        static ThingDefOf()
        {
            RebindDefs();
        }
    }
}
(2*b)||!(2*b) - That is the question.
There are 10 kinds of people in this world - those that understand binary and those that don't.

Powered By

lost_RD

Thanks for your response, I'm getting build errors - am I missing something simple?

Severity Code Description Project File Line Column Suppression State
Error CS0246 The type or namespace name 'StaticConstructorAtStartupAttribute' could not be found (are you missing a using directive or an assembly reference?) BlackGold C:\Program Files (x86)\RimWorld\Mods\BlackGold\Source\BlackGold\ThingDefOf.cs 6 6 Active
Error CS0246 The type or namespace name 'StaticConstructorAtStartup' could not be found (are you missing a using directive or an assembly reference?) BlackGold C:\Program Files (x86)\RimWorld\Mods\BlackGold\Source\BlackGold\ThingDefOf.cs 6 6 Active

1000101

Whoops, it's [StaticConstructorOnStartup] - Sorry about that.  :)
(2*b)||!(2*b) - That is the question.
There are 10 kinds of people in this world - those that understand binary and those that don't.

Powered By

lost_RD