Confusing behaviour when modifying struct fields with #soa
#6290
Answered
by
sumit-sahrawat
sumit-sahrawat
asked this question in
Q&A
-
|
Minimal Example package example
import "core:fmt"
Entity :: struct {
moving: bool,
position: [2]int,
velocity: [2]int,
}
update_entity :: proc(entity: ^Entity) {
if entity.moving {
entity.position += entity.velocity
}
}
draw_entity :: proc(entity: Entity) {
fmt.printfln(
"[Entity] moving = %v, position = (%v, %v)",
entity.moving,
entity.position.x,
entity.position.y,
)
}
main :: proc() {
entities: #soa[2]Entity
entities[0].moving = true
entities[0].velocity = {1, 1}
entities[1].moving = false
for _ in 1 ..= 3 {
for &entity in entities {
update_entity(&entity)
}
for entity in entities {
draw_entity(entity)
}
}
}Output with
|
Beta Was this translation helpful? Give feedback.
Answered by
sumit-sahrawat
Feb 28, 2026
Replies: 1 comment
-
|
Looks like this is the root cause: |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
sumit-sahrawat
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like this is the root cause: