How to replicate the .extend feature in Godot

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

I’m trying to add two items to an array at the same time, so I tried to use the .extend feature like in Python only to realize that this is not supported in Godot. Any suggestions?

:bust_in_silhouette: Reply From: BE-Never Games

It’s like you said, the Array class does not offer an ‘extend’ method or something comparable (it does provide ‘.append’ though). You can however concatenate array by simply using the ‘+’ operator, just like in Python.

array1 += [2, 3]

‘array1’ will now contain the values ‘2’ and ‘3’ at its back. Now this isn’t technically the same (I believe) since GDScript will actually create a new Array from the two existing and then set array1 equal to that new one. Python’s ‘extend’ method modifies the first Array directly.
I don’t think this will matter though in most cases.

Pool arrays can be used too, if one of the available types is enough.

eons | 2018-08-12 16:15