-
Notifications
You must be signed in to change notification settings - Fork 1
3) TargetIndex
Time for a mostly theoretical interlude. You may have noticed we referred to something called TargetA
in our JobDef
.
That's part of the TargetIndex system. The game stores job-specific data in slots; there's three slots for single items, and two lists for multiple items. It's a way of storing data that's persisted in files. When we save a pawn's job state, we save the TargetIndex contents and the index of the current toil, but (almost) nothing else; any variables in a Toil are lost. So if you have the job "haul this item to a position", then it'll have, say, TargetIndex.A referring to the item, and TargetIndex.B referring to the position.
Target indexes aren't specifically assigned or have a formal connection, but if you were to go back to our example of PetTheCat, you see the code to create a job was return new Job(CatsAreJerks_JobDefOf.PetTheCat, cat);
The parameter cat
becomes TargetIndex.A in the JobDriver we'll be making soon.
Each JobDriver has its own little internal map of which index means what. They are freely assignable. Sometimes TargetIndex.A/B is a destination cell, sometimes it's a Thing. TargetIndex.A
and TargetIndex.B
can also be a List<LocalTargetInfo>
, which is useful for storing multiple targets. TargetIndex.C can't be a list.
We already stated the TargetIndex system contains three slots for single items and two lists for multiple items.
They're called targetA
, targetB
, targetC
, targetQueueA
and targetQueueB
.
They're independently referred to by TargetIndex.A
, TargetIndex.B
, TargetIndex.C
, TargetIndex.A
, and TargetIndex.B
respectively. Notice that TargetIndex.A
and TargetIndex.B
are used twice: as Thing (or cell) or as List. You are responsible for knowing whether your function is going to use a Thing, a Cell or Queue.
You'll have noticed that TargetIndex.A and B can be a List. That's because the TargetIndex.X is just an enumerator used to identify slots - it's often passed into toils so the toil can refer to whatever the intended slot is. LocalTargetInfo stores actual target info.
Sometimes you'll see a reference to targetA instead of TargetIndex.A. These are the same thing. Additionally,
Thing thing = pawn.CurJob.GetTarget(TargetIndex.A).Thing;
Thing thing = TargetThingA;
also point to the same thing. This can be verified in the JobDriver
and Job
constructor classes. Another useful function is TargetLocA
, which is a getter for this.job.targetA.Cell
.
TargetIndex.A and TargetIndex.B can refer to BOTH a List AND a Thing/Cell, depending on how it's used. That's right, it can be BOTH within the same JobDriver.
If you call GetTarget(TargetIndex.A) you get targetA, if you call GetTargetQueue(TargetIndex.A) you get targetQueueA. These two don't depend on each other or reference each other in any way, they're 100% independent.
Credit to ZorbaTHut for patiently explaining the above.