I have this (not working code)
pub const EntityRef = union {
index: hh.SEI,
ptr: ?*SimEntity,
};
//field
sword: EntityRef = .{ .index = 0 },
switch to ptr:
if (e.sword.index != 0) {
const index = e.sword.index;
e.sword.ptr = null;
for (0..sr.entityCount) |hri| {
const refE: *SimEntity = &sr.entities[hri];
if (refE.storageIndex == index) {
e.sword.ptr = refE;
break;
}
}
if (e.sword.ptr == null) {
const fInf = @import("std").math.inf(f32);
const inf = math.v2f32{ .x = fInf, .y = fInf };
const simIndex = add(sr, index, inf, &state.storedEntities[index], false);
e.sword.ptr = &sr.entities[simIndex];
}
} else {
e.sword.ptr = null;
}
switch to index:
if (se.sim.sword.ptr) |ptr| {
se.sim.sword.index = ptr.storageIndex;
} else {
se.sim.sword.index = 0;
}
RUNTIME Error
panic: access of union field 'ptr' while field 'index' is active
simRegion.zig:201:24: 0x7ff95abd57ab in begin (HandmadeHeroZig_zcu.obj)
e.sword.ptr = null;
I would like to switch the active field toptr, instead of "accessing it", I want to assign it.