#Accessing fields in struct
1 messages · Page 1 of 1 (latest)
fields need the instance as the first argument to @field, not the type which only works for decls
so ray.Color?
that's also a type
so i assume the brick_color which is the in argument
that looks good
you also probably need to do var colors: [fields.len]ray.Color = undefined;
This is illegal behavior btw
colors[i] accesses colors.ptr which is undefined
hmmmm
remove the inline
no you definitely need inline, see my last message
nvm im blind 😄
np
i didnt see you make the colors comptime
You need inline because fields is comptime-only, but you need (non-comptime) var because brick_color is runtime
Wouldn't you want
var colors: [brick_color_count]ray.Color = undefined
well the latest is
src/main.zig:79:15: error: cannot store runtime value in compile time variable
colors[i] = @field(brick_color, field.name);?
with
const fields = @typeInfo(BrickColors).Struct.fields;
const brick_color_count = fields.len;
comptime var colors: [brick_color_count]ray.Color = undefined;
inline for (fields, 0..) |field, i| {
colors[i] = @field(brick_color, field.name);
}
oh it compiled haha
but it crashed pretty much instantly
you need an allocator and do:
var colors = allocator.alloc(ray.Color, brick_color_count)
oh yeah 🤣
If you're returning colors you would need to allocate it though
Unless you want to return it by value
with this it does work to access colors at indexes
so it might be another issue that's unrelated to this
so thankks for the help, i'm closer now
it works!
just need some more colors so the effort was worth it