pardeike / Achtung2

Command your colonists like a boss!
https://www.patreon.com/pardeike
MIT License
29 stars 11 forks source link

Repeated object creation #74

Open JiaRG opened 4 months ago

JiaRG commented 4 months ago

https://github.com/pardeike/Achtung2/blob/209d8fc813bd541d421de5825ea8db33d796cdb1/Source/Tools.cs#L33-L45

https://github.com/pardeike/Achtung2/blob/209d8fc813bd541d421de5825ea8db33d796cdb1/Source/Settings.cs#L132-L135

https://github.com/pardeike/Achtung2/blob/209d8fc813bd541d421de5825ea8db33d796cdb1/Source/Main.cs#L210-L213

Tools.RescuingWorkTypeDef Calling it multiple times will generated different object.

pardeike commented 4 months ago

Yes, that is correct

JiaRG commented 4 months ago

Shouldn't RescuingWorkTypeDef be a single case? In this case, adding and removal are the same object

pardeike commented 4 months ago

Sorry but I don’t understand what you are saying.

JiaRG commented 4 months ago

like this

private static readonly Lazy<WorkTypeDef> Lazy = new(() => new()
{
    defName = "Rescuing",
    labelShort = "WorkType_Rescue_Label".Translate(),
    pawnLabel = "WorkType_Rescue_PawnLabel".Translate(),
    gerundLabel = "WorkType_Rescue_GerundLabel".Translate(),
    description = "WorkType_Rescue_Description".Translate(),
    verb = "Rescue",
    naturalPriority = 1310,
    alwaysStartActive = true,
    workTags = WorkTags.Caring | WorkTags.Commoner | WorkTags.AllWork
});

public static WorkTypeDef RescuingWorkTypeDef => Lazy.Value;    

Each call of Tools.RescuingworkTypef will only generate an object instance

pardeike commented 4 months ago

Why is it important to only have one instance? It’s a tiny object and will get garbage collected as soon as no references to it exist anymore.

JiaRG commented 4 months ago

https://github.com/pardeike/Achtung2/blob/209d8fc813bd541d421de5825ea8db33d796cdb1/Source/DynamicWorkTypes.cs#L128-L132

If there is only one instance, the remove operation don't need 130-132 code , right?

pardeike commented 4 months ago

Well, it’s not only about the worktab visuals. The def database has significant influence on mods and the base game