how can I create a new instance of this struct, an...
# kotlin-native
a
how can I create a new instance of this struct, and also assign the values of the vectors?
Copy code
typedef struct Ray {
    Vector3 position;       // Ray position (origin)
    Vector3 direction;      // Ray direction
} Ray;
Kotlin/Native generates a class with `val`s for the vector properties
Copy code
expect class Ray(rawPtr: NativePtr) : CStructVar {
  expect companion object : CStructVar.Type {}

  expect val direction: Vector3 /* compiled code */
  expect val position: Vector3 /* compiled code */
}
I’ve tried to create a new instance using
cValue {}
Copy code
val v = cValue<Ray> {
  this.direction.x = 1f
  this.direction.y = 2f
  this.direction.z = 3f
  this.position.x = 7f
  this.position.y = 8f
  this.position.z = 9f
}
but the direction and position vectors are still zero
hmmm no, that’s not true…. it does seem to work
what I’ve actually got is a wrapper class for Ray so I don’t have to interact with the K/N C interop code, and I’m creating the value from the wrapper class properties
Copy code
sealed interface Ray {
  /** Ray direction */
  val direction: Vector3

  /** Ray position (origin) */
  val position: Vector3

  fun readValue(): CValue<RaylibRay> {
    val v = cValue<RaylibRay> {
//      this.direction.x = this@Ray.direction.x
//      this.direction.y = this@Ray.direction.y
//      this.direction.z = this@Ray.direction.z
//      this.position.x = this@Ray.position.x
//      this.position.y = this@Ray.position.y
//      this.position.z = this@Ray.position.z
      this.direction.x = 1f
      this.direction.y = 2f
      this.direction.z = 3f
      this.position.x = 7f
      this.position.y = 8f
      this.position.z = 9f
    }
    val dirString = v.useContents { direction.run { "[$x,$y,$z]" } }
    val posString = v.useContents { position.run { "[$x,$y,$z]" } }
    println("Ray(dir=$dirString,pos=$posString")
    return v
  }
}
and for some reason all of the wrapper class vector values, like
this@Ray.position.z
, return zero
I figured it out: I had a utility function to copy a vector, but it was doing this inside a memory scope, and also lazily. So, when I tried to fetch the vector values the memory scope had already ended, so the vector values defaulted to zero.