Is it expected that an inverted Array generated from get_children() will return Null?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By matterda

As described in the title. I verified that this happens in a freshly-prepared project.
The following code, in a Node2D containing a few children,

extends Node2D

func _ready():
	var a = get_children()
	print(a.invert())

will print Null instead of the inverted Array of children nodes. On the other hand, Print(a) will correctly print the Array of children. I am using Godot 3.2.3.

:bust_in_silhouette: Reply From: jgodfrey

You’re calling a function on the Array (invert), and that function simply inverts the array in-place and returns Void, so I wouldn’t expect it to print the inverted array contents. If you print the Array itself after calling invert, it’ll print what you expect. So, this…

var a = get_children()
print(a) # prints original array
a.invert()
print(a) # prints inverted array

Of course! I always forget that some commands happen in-place. Thanks.

matterda | 2020-12-07 08:34