Don't zero-init singleton components

wip/source-generators
copygirl 12 months ago
parent c9a986f9bf
commit 982741a364
  1. 7
      src/gaemstone.Client/Systems/ImGuiManager.cs
  2. 5
      src/gaemstone.SourceGen/ModuleGenerator.cs

@ -29,10 +29,7 @@ public partial class ImGuiManager
[DependsOn<Pipeline.OnStore>]
public struct ImGuiRenderPhase { }
// Start out disabled, because we first need to set it with a valid instance.
// TODO: Add a hint when a Singleton class is used without adding Disabled.
// TODO: Add a [Disabled] attribute as shortcut?
[Singleton, Add<Core.Disabled>]
[Singleton]
public class ImGuiData
{
public ImGuiController Controller { get; }
@ -91,7 +88,7 @@ public partial class ImGuiManager
[DependsOn<Pipeline.OnLoad>]
public static unsafe void Initialize<T>(World<T> world, GameWindow window, Canvas canvas,
[Source<Input>] InputContext inputContext, Not<ImGuiData> _)
=> world.Entity<ImGuiData>().Enable().Set(new ImGuiData(
=> world.Entity<ImGuiData>().Set(new ImGuiData(
new(canvas.GL, window.Handle, inputContext.Value, () => {
var io = ImGui.GetIO();
var style = ImGui.GetStyle();

@ -175,7 +175,10 @@ public class ModuleGenerator
else sb.Append($".CreateLookup<{e.FullName}>()");
sb.AppendLine();
if (e.IsSingleton) sb.AppendLine($"\t\t\t.Add<{e.FullName}>()");
// I don't think it makes sense to have singletons pre-initialized to zero.
// Especially for singletons that are reference types, which would default to null.
// if (e.IsSingleton) sb.AppendLine($"\t\t\t.Add<{e.FullName}>()");
// TODO: Look into if it would be possible to detect if we have field initializers.
}
sb.Insert(sb.Length - 1, ";");

Loading…
Cancel
Save