godot icon indicating copy to clipboard operation
godot copied to clipboard

GDScript 2.0: Packed arrays are not considered constant expressions

Open dalexeev opened this issue 1 year ago • 4 comments

Godot version

v4.0.beta3.official [01ae26d31]

System information

Kubuntu 22.10

Issue description

Attempting to use a packed array in a constant definition causes "Initializer for a constant must be a constant expression" error.

Steps to reproduce

# 3.x:
const A = PoolIntArray() # OK.
const B = PoolIntArray([1, 2, 3]) # OK.

# 4.0-dev:
const A = PackedInt32Array() # Error: Initializer for a constant must be a constant expression.
const B = PackedInt32Array([1, 2, 3]) # Error: Initializer for a constant must be a constant expression.

Minimal reproduction project

No response


See also:

  • godotengine/godot-proposals#5653

dalexeev avatar Oct 25 '22 16:10 dalexeev

related to https://github.com/godotengine/godot/issues/66949 introduced in https://github.com/godotengine/godot/commit/a604e72dc974723439e9df5edc67502b82beba4b

rune-scape avatar Dec 11 '22 15:12 rune-scape

Also, we should not forget about readonly Packed*Arrays (make_read_only()), for consistency with Array and Dictionary.

dalexeev avatar Mar 08 '23 13:03 dalexeev

Also, we should not forget about readonly Packed*Arrays (make_read_only()), for consistency with Array and Dictionary.

This might be tricky because Packed*Array are just typedefs for Vector<>. Adding read-only functionality would affect all vectors used in core.

They could be made proper classes inheriting from Vector<> and adding features, but that's a big change and should be discussed on its own merit.

vnen avatar Mar 09 '23 13:03 vnen

This might be tricky because Packed*Array are just typedefs for Vector<>. Adding read-only functionality would affect all vectors used in core.

Yes, I was worried about it, and that's why I tried to draw attention to it yesterday. :)

dalexeev avatar Mar 09 '23 13:03 dalexeev

I suggest deferring this to 4.2 or 4.x as 4.1 is in a feature freeze. Since this is more a lack of a new feature than a bug. Packed arrays have changed significantly in 4.0, they are now passed by reference rather than by value. And since they don't have a make_read_only() method (unlike Array and Dictionary), we can't make them constant as it worked before on the GDScript side. Core changes are required, and now is not the time to experiment with such important engine structure as Vector<T>. Please add the topic:core label.

dalexeev avatar Jun 14 '23 03:06 dalexeev