Achtung2 icon indicating copy to clipboard operation
Achtung2 copied to clipboard

Repeated object creation

Open JiaRG opened this issue 1 year ago • 7 comments

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.

JiaRG avatar Jul 06 '24 11:07 JiaRG

Yes, that is correct

pardeike avatar Jul 06 '24 16:07 pardeike

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

JiaRG avatar Jul 06 '24 22:07 JiaRG

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

pardeike avatar Jul 07 '24 03:07 pardeike

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

JiaRG avatar Jul 09 '24 04:07 JiaRG

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.

pardeike avatar Jul 09 '24 06:07 pardeike

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?

JiaRG avatar Jul 09 '24 06:07 JiaRG

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

pardeike avatar Jul 09 '24 06:07 pardeike