Hello I have a reflection system for a game engine that consist of tagging a type like this:
pub const Enemy = struct {
pub const refl = ReflectStruct(Enemy, .{ .serializable, .draw_imgui }).fields(&.{ "mana", "health" });
mana: i32 = 0,
health: i32 = 100,
};
This works perfect, what ReflectStruct(…) does is return a type with some comptime generated function pointers to serialize etc…
But now I want to know at runtime HOW many of these ReflectStruct types have been generated.
This will allow me to register these types automatically to a runtime ArrayList or Map.
// Currently I have to do this:
// I have to remember to register the type
pub fn gameInit() void {
registerReflectedType(Enemy);
registerReflectedType(Enemy2);
registerReflectedType(Enemy3);
registerReflectedType(Enemy4);
registerReflectedType(Enemy5);
registerReflectedType(Enemy6);
}
// What I want:
// Automatically register all the ReflectedTypes generated at comptime.
// The idea is to add a type to the g_generated_reflected_types_list on each comptime execution of `ReflectStruct` method.
// And then use it at runtime like this:
pub fn gameInit() void {
registerReflectedTypes(g_generated_reflected_types_list);
}
Is it possible to fill a global comptime array with all the created ReflectStructs created?
I tried with a comptime var global but it doesn’t work…
Also I had a look to this issue: make closure over comptime var a compile error; comptime vars become immutable when they go out of scope · Issue #7396 · ziglang/zig · GitHub
But it’s not clear to me how can be done.
Thank you!!