New to modding - help needed on how to make changes to existing Rimworld code

Started by Jangles, April 06, 2021, 01:05:55 PM

Previous topic - Next topic

Jangles

Hello all. I'm trying to make a mod where gifting the bodies of dead pawns back to their faction gives an improved relations boost, just like you get a boost from gifting prisoners back to their faction rather than giving them to someone else (since it seems weird to me that returning someone's body to their friends and family so they can give them a proper burial gives you exactly the same relations gain as giving people the corpse of some rando they've never seen before).

Having poked around a bit in the decompliled source code, the easiest solution that I've found would be editing the GetBaseGoodwillChange method in FactionGiftUtility, which goes:

private static float GetBaseGoodwillChange(Thing anyThing, int count, float singlePrice, Faction theirFaction)
{
if (count <= 0)
{
return 0f;
}
float num = singlePrice * (float)count;
Pawn pawn = anyThing as Pawn;
if (pawn != null && pawn.IsPrisoner && pawn.Faction == theirFaction)
{
num *= 2f;
}
return num / 40f;
}


If I'm understanding it right, then changing the if statement to just:
if (pawn != null && pawn.Faction == theirFaction)
should make it so gifting dead bodies that belong to that faction should get the same 2x relations boost as gifting prisoners (since as far as I know, the only time you can ever trade pawns is if their dead or a prisoner, so removing the check for .IsPrisoner should make it apply to both prisoners and corpses.)

Alternatively, changing it to:
if (pawn != null && (pawn.IsPrisoner || pawn.Dead) && pawn.Faction == theirFaction)
might be more prudent? Like I say, as far as I know, the only way you would be able to access a pawn belonging to a different faction in the trading menu should be if they're dead or a prisoner, but having the method explicitly check to make sure they're either a prisoner or a corpse might be a good idea just to be safe.


Anyway, from what I've been able to glean from the modding tutorials on the wiki, the way to make mods that alter Rimworld's existing code is by using Harmony, but I haven't really been able to find anything that explains how you use Harmony to do this. All the modding tutorials seem to be about adding new content to the game, not changing an existing bit of code like I'd like to do here.

Can someone guide me through what I need to do to make a mod that does this? I more or less know what I'm doing with C#, but when it comes to modding Rimworld specifically, I don't have any experience aside from following the tutorial on making a Hello World test mod about an hour ago.

Thanks!

RawCode

post you harmony injection annotation and complete replacement method

also you should not replace methods completely, because this automatically makes your mod incompatible with any other mod that rely on same method, best way to make prefix and postfix with state and alter output only if actually needed.

LWM

You might look at my Harmony Patches to try and get an understanding of how it's used in the wild - I try and comment all of my patches!
Here's a bunch I've done: https://github.com/lilwhitemouse/RimWorld-LWM.MinorChanges/tree/master/Source

As for your specific case, I see two things:

1.  ...are dead bodies pawns?  I thought they were Things that contain a reference to the dead pawn?  I could super easily be wrong here ;)
2. The simplest approach might be a Prefix - you could test if the gift is a dead pawn of the correct faction and if so, just return your own goodwill result.