C# - Adding new pawn thought for corpses

Started by SickBoyWi, December 26, 2018, 11:07:01 PM

Previous topic - Next topic

SickBoyWi

I've added a new custom pawn. I've also added a few new pawn mood alterations when coming into contact with it. The issue I'm having is trying to make a new mood modifier for the new pawn if the pawns see a dead one. The issue seems to be that the pawn no longer counts as a pawn of the new type, but rather as a corpse. That comes from my observation of how the game is behaving, as well as the logs I'm kicking out as the interactions happen.

So the question is, how do I add a new mood change to pawns when they see a corpse of my specific pawn? I'd rather not mess with the core code in any way.

Here's some sample code from my worker class for the new pawn:

        public Thought_Memory GiveObservedThought()
        {
            Thought_MemoryObservation thought_MemoryObservation = null;
           
            if (this.Dead)
            {
                if (ThoughtDef.Named("RH_TET_ObservedJohnnysPawnDead") is ThoughtDef td)
                {
                    thought_MemoryObservation =
                        (Thought_MemoryObservation)ThoughtMaker.MakeThought(td);
                }
            }
            else
            {
                if (ThoughtDef.Named("RH_TET_ObservedJohnnysPawn") is ThoughtDef td)
                {
                    thought_MemoryObservation =
                        (Thought_MemoryObservation)ThoughtMaker.MakeThought(td);
                }
            }

            if (thought_MemoryObservation != null)
            {
                thought_MemoryObservation.Target = this;
                return thought_MemoryObservation;
            }

            return null;
        }

The else logic for 'this.Dead' works just fine. The 'this.Dead' logic is never hit. When I observe the pawn type, indeed it has the 'Corpse_' in front of it's pawn type. Makes sense from an internal game perspective, but how the heck do I add this mood change then?

Any ideas are appreciated!

Mehni

How Custom is your pawn? Own class, inheriting from Pawn? If so, you'll also want your own Corpse with your own IThoughtGiver. Careful of the MakeCorpse method in that case. It's in Pawn.Kill(), and makes a regular Corpse.

The possibly easier, with fewer pitfalls, method is a Harmony postfix. Whether you want that depends on your definition of "messing with core code".

SickBoyWi

Thanks for the response!

Yes, I do have my own class that extends pawn.

After reading your comment I've created a new class that extends Corse, with the IThoughtGiver, and made the GiveObservedThought method hide the base method. The issue I'm having now is that I don't know how to tie the new corpse class to the specific corpse. I tried adding a new XML config for the corpse (with 'Corpse_' at the beginning of the name), but when I do so it says one already exists with that name.  Here's the error log message:

Adding duplicate Verse.ThingDef name: Corpse_RH_TET_JohnnysPawnRace

Your comment about the MakeCorpse method is noted!

I may look into the Harmony postfix here; I do feel like I'm super close, but am just missing something small. It's fun doing this, but frustrating when trying to code against something that is largely (and for valid reason, I understand) undocumented.

Razuhl

Corpse definition are created during runtime not in xml. You can use a class with a static constructor and the annotation "StaticConstructorOnStartup" to have your code executed after the implied definitions were created. Then you simply switch the thingClass in the corpse definition found in your race definition for the custom pawn in question.

SickBoyWi

Nailed it! Thanks much for the response; both of you guys!