C# / DLL - Counting pawns

Started by Telkir, July 09, 2014, 10:36:50 AM

Previous topic - Next topic

Telkir

Like the subject says I am starting to experiement with C# and DLLs... fun stuff! After a tiny bit of success with the first simple thing I tried though, I've become stuck. :(

I'm trying to count the number of colonist pawns and the number of hostile pawns.

I've found the ListerPawns class which would seem to be capable of giving me the data, but I don't seem to be using it correctly because I get an error in-game. My code is doing this:

        private ListerPawns pawnCounter = new ListerPawns();
        private int numFriendlies;
        private int numHostiles;

        public override void TickRare()
        {
            base.TickRare();
            UpdatePawnCounts();
        }

        private void UpdatePawnCounts()
        {
            numFriendlies = pawnCounter.ColonistCount;
            numHostiles = pawnCounter.PawnsHostileToColony.Count();
        }

The error I am getting is KeyNotFoundException: The given key was not present in the dictionary.

It looks like I somehow need to manually add pawns into the list before I can count them. How would I do that? Or is there some global data that I can pull from instead?

Thanks in advance for any advice!

mrofa

You didnt tell it where to look :D
Anyways look on radar code of Miscellaneous mod its do exacly what you want.
All i do is clutter all around.

Telkir

Okay, I'm checking that over now to see how I can improve...

Typically right after I first made the thread I stumbled upon another topic on the forum with code that also seems to work, which is simply by calling Find:
numFriendlies = Find.ListerPawns.ColonistCount;
numHostiles = Find.ListerPawns.PawnsHostileToColony.Count();

StorymasterQ

Out of topic, but I almost thought, just reading the last two words of the title, that this is a post combining Counting Cars and Pawn Stars. But no.
I like how this game can result in quotes that would be quite unnerving when said in public, out of context. - Myself

The dubious quotes list is now public. See it here