Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeated object creation #74

Open
JiaRG opened this issue Jul 6, 2024 · 7 comments
Open

Repeated object creation #74

JiaRG opened this issue Jul 6, 2024 · 7 comments

Comments

@JiaRG
Copy link

JiaRG commented Jul 6, 2024

Achtung2/Source/Tools.cs

Lines 33 to 45 in 209d8fc

public static WorkTypeDef RescuingWorkTypeDef => 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
};

Achtung2/Source/Settings.cs

Lines 132 to 135 in 209d8fc

if (rescuing == null && Achtung.Settings.rescueEnabled)
Tools.savedWorkTypeDef = DynamicWorkTypes.AddWorkTypeDef(Tools.RescuingWorkTypeDef, WorkTypeDefOf.Doctor, doctorRescueWorkGiver);
else if (rescuing != null && Achtung.Settings.rescueEnabled == false)
DynamicWorkTypes.RemoveWorkTypeDef(Tools.RescuingWorkTypeDef, Tools.savedWorkTypeDef, doctorRescueWorkGiver);

Achtung2/Source/Main.cs

Lines 210 to 213 in 209d8fc

var rescuing = DefDatabase<WorkTypeDef>.GetNamedSilentFail(Tools.RescuingWorkTypeDef.defName);
var doctorRescueWorkGiver = DefDatabase<WorkGiverDef>.GetNamed("DoctorRescue");
if (rescuing == null && Achtung.Settings.rescueEnabled)
Tools.savedWorkTypeDef = DynamicWorkTypes.AddWorkTypeDef(Tools.RescuingWorkTypeDef, WorkTypeDefOf.Doctor, doctorRescueWorkGiver);

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

@pardeike
Copy link
Owner

pardeike commented Jul 6, 2024

Yes, that is correct

@JiaRG
Copy link
Author

JiaRG commented Jul 6, 2024

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

@pardeike
Copy link
Owner

pardeike commented Jul 7, 2024

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

@JiaRG
Copy link
Author

JiaRG commented Jul 9, 2024

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
Copy link
Owner

pardeike commented Jul 9, 2024

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
Copy link
Author

JiaRG commented Jul 9, 2024

DefDatabase<WorkTypeDef>.Remove(def);
var columnDef = DefDatabase<PawnColumnDef>.AllDefsListForReading.FirstOrDefault(d => d.workType == oldWorkGiver);
if (columnDef != null)
DefDatabase<PawnColumnDef>.Remove(columnDef);

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

@pardeike
Copy link
Owner

pardeike commented Jul 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants