Problems coding my own fermenting barrel thing

Started by yadaptor, June 21, 2019, 05:50:36 PM

Previous topic - Next topic

yadaptor

Ok, I wanted to get into C# for some time, and decided to add one of those fermenting things to my mod. It turns pollen (Raw plant, considered desperate only food) into honey (can be a joy source, gives positive mood), but I can't figure out what am I doing wrong here. I'm using the core and fertile fields mod as reference, I added the barrel building class, Jobdriver and WorkGiver classes for both jobs (fill the barrel, and take honey out), but whatever a pawn is idle, and decide to get this job, it gives exceptions.
At first where Exceptions on Verse.AI.ThinkNodePrioritySorter TryIssueJobPackage "cannot cast from source type to destination type".
Now I changed the way the code calls for defs, to instead of using DefDatabases, to use the shorter ThingDefOf/JobDefOf, but it gives another error "exception on verse.ai tryjobpackage "value does not fall within the expected range"" and something related to drug policies?

I really need some guidance here.

Edit 1:I cannot believe I forgot to include the logs in the archive -3-
But basically,it shows this:
Exception in Verse.AI.ThinkNode_PrioritySorter TryIssueJobPackage: System.InvalidCastException: Cannot cast from source type to destination type.
  at Arthropods.WorkGiver_FillHoneyBarrel.JobOnThing (Verse.Pawn pawn, Verse.Thing t, Boolean forced) [0x00000] in <filename unknown>:0
  at RimWorld.JobGiver_Work.TryIssueJobPackage (Verse.Pawn pawn, JobIssueParams jobParams) [0x00000] in <filename unknown>:0
  at Verse.AI.ThinkNode_PrioritySorter.TryIssueJobPackage (Verse.Pawn pawn, JobIssueParams jobParams) [0x00000] in <filename unknown>:0

When pawns are idle and the only job is to fill the barrel, and this:
Root level exception in OnGUI(): System.InvalidCastException: Cannot cast from source type to destination type.
  at Arthropods.WorkGiver_FillHoneyBarrel.JobOnThing (Verse.Pawn pawn, Verse.Thing t, Boolean forced) [0x00000] in <filename unknown>:0
  at RimWorld.FloatMenuMakerMap.AddJobGiverWorkOrders (IntVec3 clickCell, Verse.Pawn pawn, System.Collections.Generic.List`1 opts, Boolean drafted) [0x00000] in <filename unknown>:0
  at RimWorld.FloatMenuMakerMap.AddUndraftedOrders (Vector3 clickPos, Verse.Pawn pawn, System.Collections.Generic.List`1 opts) [0x00000] in <filename unknown>:0
  at RimWorld.FloatMenuMakerMap.ChoicesAtFor (Vector3 clickPos, Verse.Pawn pawn) [0x00000] in <filename unknown>:0
  at RimWorld.FloatMenuMakerMap.TryMakeFloatMenu (Verse.Pawn pawn) [0x00000] in <filename unknown>:0
  at RimWorld.Selector.HandleMapClicks () [0x00000] in <filename unknown>:0
  at RimWorld.Selector.SelectorOnGUI () [0x00000] in <filename unknown>:0
  at RimWorld.MapInterface.HandleLowPriorityInput () [0x00000] in <filename unknown>:0
  at RimWorld.UIRoot_Play.UIRootOnGUI () [0x00000] in <filename unknown>:0
  at Verse.Root.OnGUI () [0x00000] in <filename unknown>:0

when I right click a barrel to force a pawn to do the job.

[attachment deleted due to age]

LWM

Well, the "cast" error thrown looks like an error in your C# code, but without seeing what you've done there, we cannot help much.

--LWM

yadaptor

#2
Quote from: LWM on June 27, 2019, 11:34:06 AM
Well, the "cast" error thrown looks like an error in your C# code, but without seeing what you've done there, we cannot help much.

--LWM
I included my C# code as an attachment, with the related xmls

Mehni

The code you posted points to WorkGiver_FillHoneyBarrel's JobOnThing method.

public override Job JobOnThing(Pawn pawn, Thing t, bool forced = false)
{
    Building_HoneyBarrel building_HoneyBarrel = (Building_HoneyBarrel)t;
    Thing t2 = this.FindPollen(pawn, building_HoneyBarrel);
    return new Job(JobDefOf.ArthropodFillHoneyBarrel, t, t2)
    {
        count = building_HoneyBarrel.SpaceLeftForPollen
    };
}


You're casting Thing t to a Building_HoneyBarrel, but t isn't a Building_HoneyBarrel.

Your HasJobOnThing method does it better:

public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
{
    Building_HoneyBarrel building_HoneyBarrel = t as Building_HoneyBarrel;
    if (building_HoneyBarrel == null || building_HoneyBarrel.Fermented || building_HoneyBarrel.SpaceLeftForPollen <= 0)
    {
        return false;
    }
    //snip


One of them is an explicit cast, the other implicit.

FWIW I think you're reinventing the wheel here. There's a Universal Fermenter framework mod.

yadaptor

The 'reinventing the wheel' is on purpose, this is more a learning opportunity for me than anything else o3o
I changed the cast method from JobOnThing to your sugestion, rebuild everything, but the exact same two exceptions remain, any other suggestion?

LWM


yadaptor

Okay, my bad, I forgot to rebuild, it works fine now ^3^;
thanks