Skip to content

1a) Job Assignment via JobGiver

Mehni edited this page Feb 25, 2018 · 2 revisions

Jobs are assigned either by a JobGiver or by a WorkGiver.

JobGivers are found either in the ThinkTree or in a DutyDef. The ThinkTree defines main colonist behaviour and most jobs in here are short: the ThinkTree is there for stateless decisions, not for tasks with a lot of state; it's expected that things will be cancelled with little-to-no notification. DutyDefs are mostly used for Lords(more advanced AI, think of a Lord as a squad leader).

A simple example of a JobGiver is JobGiver_SeekSafeTemperature, which is used by animals and humans alike. It inherits from ThinkNode_JobGiver. It contains a helper function to select a region, and the TryGiveJob itself. The below example isn't that, because I don't want to spread decompiled code. Instead, we'll be making a very simple new Job where colonists will go to a cat and pet it.

namespace CatsAreJerks
{
    //we inherit from ThinkNode_JobGiver to save us some trouble
    class JobGiver_PetCats : ThinkNode_JobGiver
    {
        //the method that's defined in the inherited method. We override it here and return our job.
        protected override Job TryGiveJob(Pawn pawn)
        {
            //find a cat using our helper method
            Pawn cat = FindCatToPet(pawn);

            //if there is no cat, or we can't reach the cat, we don't do a job.
            if (cat == null || !pawn.CanReserveAndReach(cat, PathEndMode.Touch, Danger.Deadly, 1, -1, null, false))
            {
                return null;
            }

            //If we found a cat, return a new Job called PetTheCat.
            //Since we're using our own job, we're using the below syntax, including the class we gave it
            //If we were using a vanilla Job, the below command would be
            //return new Job(JobDefOf.Slaughter, cat);
            //for a slaughter job ;)
            return new Job(CatsAreJerks_JobDefOf.PetTheCat, cat);
        }

        public static Pawn FindCatToPet(Pawn pawn)
        {
            //helper method to find a cat. Truncated for the sake of brevity.
            return result;
        }

        //using [DefOf] allows us to reference any Defs we've defined in XML. 
        [DefOf]
        class CatsAreJerks_JobDefOf
        {
            public static JobDef PetTheCat;
        }
    }
}
Clone this wiki locally