Weird, persistent message when I try to give a pawn a thought

Started by battlemage64, November 11, 2019, 08:24:05 PM

Previous topic - Next topic

battlemage64

I'm calling a method from inside a MapComponent to give a pawn a Hediff and a Thought. The Hediff works fine, but when I try to call pawn.needs.mood.thoughts.memories.TryGainMemory(ThoughtDefOfStubToe.StubbedMyToe), I get an error: Object instance not set to an instance of an object. The only object I see that could be causing that is ThoughtDefOfStubToe.StubbedMyToe, and the code for it is functionally the same as the code for the Hediff (StubbedDef):

namespace StubToe
{
    [DefOf]
    public static class HediffDefOfStubToe
    {
        /* Medical conditions */
        public static HediffDef StubbedDef;
    }
    public static class ThoughtDefOfStubToe
    {
        public static ThoughtDef StubbedMyToe;
    }
}


Both are defined in XML correctly, here's the def for the thought:
<ThoughtDef>
<defName>StubbedMyToe</defName>
<durationDays>0.25</durationDays>
<stackLimit>300</stackLimit>
<stackedEffectMultiplier>0.50</stackedEffectMultiplier>
<stages>
<li>
<label>stubbed my toe</label>
<description>I stubbed my toe. Ouch! That sucked.</description>
<baseOpinionOffset>-3</baseOpinionOffset>
</li>
</stages>
</ThoughtDef>

The error always appears when trying to give a pawn the thought, but never when giving the pawn the Hediff. Does anyone know how to fix this?
I love to mod and boy am I bad at it

K

I'm not entirely sure if this is the issue since you didn't post the code adding the thought, but in my experience you must use the ThoughtMaker:

var thought = (Thought_Memory)ThoughtMaker.MakeThought(ThoughtDef);
pawn.needs.mood.thoughts.memories.TryGainMemory(thought);

This code adds a memory like you're trying to do. You can't just add a def because a def is just that - a definition, and it it needs to be "instantiated" for lack of a better word.

battlemage64

#2
The TryGainMemory method has an overload where you just give it a ThoughtDef. Either way, though, I tried it how you wrote it and got the same error (but in the MakeThought() rather than the TryGainMemory()).
I love to mod and boy am I bad at it

K

I didn't know that. Good to know.

That points to a problem with the XML then. More than likely it's that you didn't assign a thoughtClass. Since you're trying to add a memory it has to be:
<thoughtClass>Thought_Memory</thoughtClass>
Unless you've created a custom Thought type anyway.

battlemage64

I tried this, and the error goes away, but the mood doesn't show up under Needs. It does show up in thought.memories.getallthoughts (I think that's what it is, I'm away from my desktop rn), but it seems to appear as a social interaction rather than a mood thought. Am I supposed to do something else for mood-affecting thoughts? I can't find anything in the game's XML that's different except for thoughtClass tags, which are absent from mood thought defs like AteWithoutTable but present for social thoughts (interactions) like Chitchat/Deep talk. As far as I can tell it seems like the same function is supposed to be called for both kinds of thoughts but it isn't working for me idk why.
I love to mod and boy am I bad at it

K

I read your XML again and noticed that you're using the tag baseOpinionOffset. The tag you need for a mood effect is baseMoodEffect. That should make it show up in the needs tab as a mood thought.

I'm guessing that might be the reason that you had the null reference in the first place. The game may try to interpret what kind of thought the def should be automatically, and since you had an opinion modifier it thought it should be a social thought, and social thoughts require a thoughtClass. There's no harm in assigning a thoughtClass even if it isn't strictly necessary, at least as far as I know.

battlemage64

I love to mod and boy am I bad at it