diff --git a/OpenRA.Mods.Common/Traits/Cargo.cs b/OpenRA.Mods.Common/Traits/Cargo.cs index a1b6b53d9a..9d5c3472ad 100644 --- a/OpenRA.Mods.Common/Traits/Cargo.cs +++ b/OpenRA.Mods.Common/Traits/Cargo.cs @@ -107,6 +107,8 @@ namespace OpenRA.Mods.Common.Traits int loadingToken = ConditionManager.InvalidConditionToken; Stack loadedTokens = new Stack(); bool takeOffAfterLoad; + bool initialised; + readonly CachedTransform> currentAdjacentCells; public IEnumerable CurrentAdjacentCells @@ -161,17 +163,6 @@ namespace OpenRA.Mods.Common.Traits totalWeight = cargo.Sum(c => GetWeight(c)); } - foreach (var c in cargo) - { - c.Trait().Transport = self; - - foreach (var nec in c.TraitsImplementing()) - nec.OnEnteredCargo(c, self); - - foreach (var npe in self.TraitsImplementing()) - npe.OnPassengerEntered(self, c); - } - facing = Exts.Lazy(self.TraitOrDefault); } @@ -192,6 +183,23 @@ namespace OpenRA.Mods.Common.Traits if (!string.IsNullOrEmpty(Info.LoadedCondition)) loadedTokens.Push(conditionManager.GrantCondition(self, Info.LoadedCondition)); } + + // Defer notifications until we are certain all traits on the transport are initialised + self.World.AddFrameEndTask(w => + { + foreach (var c in cargo) + { + c.Trait().Transport = self; + + foreach (var nec in c.TraitsImplementing()) + nec.OnEnteredCargo(c, self); + + foreach (var npe in self.TraitsImplementing()) + npe.OnPassengerEntered(self, c); + } + + initialised = true; + }); } static int GetWeight(Actor a) { return a.Info.TraitInfo().Weight; } @@ -410,14 +418,17 @@ namespace OpenRA.Mods.Common.Traits loadingToken = conditionManager.RevokeCondition(self, loadingToken); } - foreach (var nec in a.TraitsImplementing()) - nec.OnEnteredCargo(a, self); + // Don't initialise (effectively twice) if this runs before the FrameEndTask from Created + if (initialised) + { + a.Trait().Transport = self; - foreach (var npe in self.TraitsImplementing()) - npe.OnPassengerEntered(self, a); + foreach (var nec in a.TraitsImplementing()) + nec.OnEnteredCargo(a, self); - var p = a.Trait(); - p.Transport = self; + foreach (var npe in self.TraitsImplementing()) + npe.OnPassengerEntered(self, a); + } string passengerCondition; if (conditionManager != null && Info.PassengerConditions.TryGetValue(a.Info.Name, out passengerCondition))