0 votes

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?

asked Aug 12, 2018 in Engine by aagoldberg (46 points)

1 Answer

+1 vote

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.

answered Aug 12, 2018 by BE-Never Games (297 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.