godot icon indicating copy to clipboard operation
godot copied to clipboard

Initialize `Quaternion` variant with identity

Open detomon opened this issue 2 years ago • 0 comments

Initializes the Quaternion variant with its identity value.

There are some cases where quaternions are initialized with zeroed values, which is not valid. For example, when resizing an array of type Array[Quaternion]:

var array: Array[Quaternion] = []

array.resize(3)

# Prints `[(0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0)]`.
print(array)

This also applies to exported variables of type Array[Quaternion] and to some extend to Quaternion (when resetting in inspector):

@tool
class_name TestResource
extends Resource

# Without specified value, defaults to `Quaternion(0, 0, 0, 0)` in editor when resetting.
@export var quaternion: Quaternion

# New elements added in editor default to `Quaternion(0, 0, 0, 0)`.
# Also prints `The quaternion must be normalized.` in the console.
@export var quaternion_array: Array[Quaternion] = []

As VariantInternal::initialize is also used in the script engine, I'm not sure if this has some implications for the core functionality.

Test project: quaternion.zip

detomon avatar Nov 09 '23 11:11 detomon