Problem with Hoppers

Started by skullywag, September 11, 2014, 03:32:14 PM

Previous topic - Next topic

skullywag

How are we dealing with hoppers when they are placed between 2 buildings that can use their inv?

Am i right in thinking currently if building 1 grabs the item from the hopper while building 2 has finished its check to see if theres an item there but hasnt grabbed it yet, itll barf.

Consider this:

private Thing StuffInHopper
        {
            get
            {
                ThingDef thingDef = ThingDef.Named("Hopper");
                Thing result;
                foreach (IntVec3 current in GenAdj.AdjacentSquaresCardinal(this))
                {
                    Thing thing = null;
                    Thing thing2 = null;
                    foreach (Thing current2 in Find.ThingGrid.ThingsAt(current))
                    {
                        if (current2.def == ThingDef.Named("Stuff"))
                        {
                            thing = current2;
                        }
                        if (current2.def == thingDef)
                        {
                            thing2 = current2;
                        }
                    }
                    if (thing != null && thing2 != null)
                    {
                        result = thing;
                        return result;
                    }
                }
                result = null;
                return result;
            }
        }


If 2 buildings were running that in their class and in tick were doing:

do
                    {
                        int num3 = Mathf.Min(StuffInHopper.stackCount, num);
                        num2 += num3;
                        list.Add(StuffInHopper.def);
                        StuffInHopper.SplitOff(num3);
                        if (num2 >= num)
                        {
                            break;
                        }
                        StuffInHopper= this.StuffInHopper;
                    }
                    while (StuffInHopper!= null);


Thats gonna barf right? so can we tell buildings to reserve items in hoppers even if its only for a few ticks?
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

Rikiki

I don't think there can be a problem with that as the Tick() method of each object is run to the end before switching to another object.
With a little scheme:
...TickObject1Start->TickObject1End->TickObject2Start->TickObject2End->...

Also, this is quite hard to understand your code as we can't see the type of all your variables and their name is not really explicit (thing1, thing2: please no! ;)).
What do you want to do with your hopper's content?

A standard treatment would be:
1) Check if an adjacent hopper contains requiredAmount of ingredientThing
2) If no, do nothing.
3) If yes, decrement the hopper content by requiredAmount and start a counter.
4) Update this counter at each Tick call
5) While the counter is valid (inferior to a threshold for example), perform your treatment (energy production or whatever).

I hope it helps.

skullywag

Sorry yeah thats actually code from the wood tweaks mod not mine. Im doing exactly what you state above but am having issues when "sharing" a hopper. That code was the only code i could find doing similar things. Ill do some more tests and see if i cant find a reproducable error, its probably me if what you said about tick handling is true.

As an example i have 2 buildings that need items in hoppers to produce power when i put 1 item in the hopper one of them takes the item but they both produce power. Ive probably got a logic error somewhere.
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

Rikiki

You may want to have a look at this mod http://ludeon.com/forums/index.php?topic=5709.msg54885#msg54885 which seems to be able to share a hopper between several buildings.