#Map from type to struct inconsistent assignment

1 messages · Page 1 of 1 (latest)

jovial delta
#

Having this type

ComponentTable :: struct(T: typeid) {
    sparse: []int,
    dense:  []Entity,
    data:   []T,
    count:  int,
}

Scene :: struct {
    next_id: Entity,
    alive:   []Entity,
    atlas:   map[typeid]ComponentTable(typeid),
}

Where s is a Scene, why is this ok s.atlas[type].sparse[r] = 0 but this isn't s.atlas[type].count = 0

Error: Cannot assign to struct field 's.atlas[type].count' in map

surreal anchor
#

You can't assign to a struct field of a map slot.
The reason s.atlas[type].sparse[r] = 0 works is because you aren't really assigning to a struct field, you are just changing whats in the map sparse[r], if you were to assign the field i.e. s.atlas[type].sparse = some_map that would be prohibited

See https://odin-lang.org/docs/overview/#maps

#

scroll down a bit to "Modifying existing map slots"

jovial delta
#

so the solution here is to use a pointer instead?

#

as in atlas: map[typeid]^ComponentTable(typeid)

#

or to retrieve via pointer I guess;
tmp := &s.atlas[type] and tmp.count = 0

surreal anchor
#

make all the changes u want to tmp and then put it back in the map

jovial delta
#

cool, seems fair

#

ty a lot

surreal anchor
#

ofc ❤️