Avoid keeping ActorInitializers in memory.
The SupportPowerManager and WithSpriteBody trait captured the ActorInitializer in lambda expressions, which keeps it alive as long as the trait. The lambdas didn't need to capture the ActorInitializer, so rejig them to allow the ActorInitializer to be reclaimed after the traits have been created. As the TypeDictionary in the ActorInitializer can be quite large, this helps reduce memory usage.
This commit is contained in:
@@ -72,20 +72,22 @@ namespace OpenRA.Mods.Common.Traits.Render
|
||||
protected WithSpriteBody(ActorInitializer init, WithSpriteBodyInfo info, Func<WAngle> baseFacing)
|
||||
: base(info)
|
||||
{
|
||||
var rs = init.Self.Trait<RenderSprites>();
|
||||
var self = init.Self;
|
||||
|
||||
var rs = self.Trait<RenderSprites>();
|
||||
|
||||
bool Paused() => IsTraitPaused &&
|
||||
DefaultAnimation.CurrentSequence.Name == NormalizeSequence(init.Self, Info.Sequence);
|
||||
DefaultAnimation.CurrentSequence.Name == NormalizeSequence(self, Info.Sequence);
|
||||
|
||||
Func<WVec> subtractDAT = null;
|
||||
if (info.ForceToGround)
|
||||
subtractDAT = () => new WVec(0, 0, -init.Self.World.Map.DistanceAboveTerrain(init.Self.CenterPosition).Length);
|
||||
subtractDAT = () => new WVec(0, 0, -self.World.Map.DistanceAboveTerrain(self.CenterPosition).Length);
|
||||
|
||||
DefaultAnimation = new Animation(init.World, rs.GetImage(init.Self), baseFacing, Paused);
|
||||
DefaultAnimation = new Animation(init.World, rs.GetImage(self), baseFacing, Paused);
|
||||
rs.Add(new AnimationWithOffset(DefaultAnimation, subtractDAT, () => IsTraitDisabled), info.Palette, info.IsPlayerPalette);
|
||||
|
||||
// Cache the bounds from the default sequence to avoid flickering when the animation changes
|
||||
boundsAnimation = new Animation(init.World, rs.GetImage(init.Self), baseFacing, Paused);
|
||||
boundsAnimation = new Animation(init.World, rs.GetImage(self), baseFacing, Paused);
|
||||
boundsAnimation.PlayRepeating(info.Sequence);
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
Self = init.Self;
|
||||
DevMode = Self.Trait<DeveloperMode>();
|
||||
TechTree = Self.Trait<TechTree>();
|
||||
RadarPings = Exts.Lazy(() => init.World.WorldActor.TraitOrDefault<RadarPings>());
|
||||
RadarPings = Exts.Lazy(() => Self.World.WorldActor.TraitOrDefault<RadarPings>());
|
||||
|
||||
init.World.ActorAdded += ActorAdded;
|
||||
init.World.ActorRemoved += ActorRemoved;
|
||||
|
||||
Reference in New Issue
Block a user