I want to assign a variable in Kotlin with when:
val clickedBlock: Block? = when (event.action) {
...
Action.RIGHT_CLICK_AIR -> {
p.getLineOfSight(null, 5).forEach { block ->
if (block.type != Material.VOID_AIR) {
block // I want to assign the variable with this
}
}
null // and not always with this
}
else -> null
}
But IntelliJ says it would always return the second null value.
How can I achieve it that the variable clickedBlock will be assigned with block (and not null) if the if statement inside of the forEach loop is true without having to introduce another variable?