utopia-rise / godot-kotlin-jvm

Godot Kotlin JVM Module
MIT License
636 stars 44 forks source link

Cannot convert PoolColorArray to Variant #218

Closed Humberd closed 3 years ago

Humberd commented 3 years ago

I'm trying to procedurally generate a mesh. There are 3 vertices and 3 colors for them. The code below throws an error. However, when I don't add ARRAY_COLOR it works (but without colors). I had to create a surfaceArray helper, because due to #206 I cannot assign the value to a specific index.

fun generateSurface(){
      val triangle = variantArrayOf(Vector3(0, 0, 0), Vector3(1, 0, 0), Vector3(0, 1, 0))
      val colors = PoolColorArray().also { it.append(Color.blue); it.append(Color.red); it.append(Color.green) }

      ArrayMesh().also {
          it.addSurfaceFromArrays(
              primitive = Mesh.PRIMITIVE_TRIANGLE_FAN,
              arrays = surfaceArray(
                  ARRAY_VERTEX = triangle,
                  ARRAY_COLOR = colors
              )
          )
      }
}

fun surfaceArray(
    ARRAY_VERTEX: VariantArray<Vector3>? = null,
    ARRAY_NORMAL: VariantArray<Nothing>? = null,
    ARRAY_TANGENT: VariantArray<Nothing>? = null,
    ARRAY_COLOR: PoolColorArray? = null,
    ARRAY_TEX_UV: VariantArray<Nothing>? = null,
    ARRAY_TEX_UV2: VariantArray<Nothing>? = null,
    ARRAY_BONES: VariantArray<Nothing>? = null,
    ARRAY_WEIGHTS: VariantArray<Nothing>? = null,
    ARRAY_INDEX: VariantArray<Nothing>? = null,
): VariantArray<Any?> {
    return variantArrayOf(
        ARRAY_VERTEX, // 0 - ARRAY_VERTEX
        ARRAY_NORMAL, // 1 - ARRAY_NORMAL
        ARRAY_TANGENT, // 2 - ARRAY_TANGENT
        ARRAY_COLOR, // 3 - ARRAY_COLOR
        ARRAY_TEX_UV, // 4 - ARRAY_TEX_UV
        ARRAY_TEX_UV2, // 5 - ARRAY_TEX_UV2
        ARRAY_BONES, // 6 - ARRAY_BONES
        ARRAY_WEIGHTS, // 7 - ARRAY_WEIGHTS
        ARRAY_INDEX, // 8 - ARRAY_INDEX
    )
}
Caused by: java.lang.UnsupportedOperationException: Can't convert type class godot.core.PoolColorArray (Kotlin reflection is not available) to Variant
    at godot.core.VariantType$64.invoke(Variant.kt:447)
    at godot.core.VariantType$64.invoke(Variant.kt:431)
    at godot.core.VariantType$toGodot$1.invoke(Variant.kt:471)
    at godot.core.VariantType$toGodot$1.invoke(Variant.kt:84)
    at godot.core.TransferContext.writeArguments(TransferContext.kt:21)
    at godot.core.VariantArray.append(VariantArray.kt:168)
    at godot.core.VariantArray.addAll(VariantArray.kt:54)
    at kotlin.collections.CollectionsKt__MutableCollectionsKt.addAll(MutableCollections.kt:141)
piiertho commented 3 years ago

Thx for reporting, it is forgotten case in ANY conversion switch in Variant.kt: https://github.com/utopia-rise/godot-kotlin-jvm/blob/0e6abcb3854b52da9445d00217db22938a773860/kt/godot-runtime/src/main/kotlin/godot/core/Variant.kt#L469 All pool arrays are missing here.