Is there a Node.append_children(array) method?

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

Hello!

I’m making a multiplayer 2D platformer (which I’m very proud of, by the way xD), and I recently added a multi-target follow camera from this absolutely amazing tutorial:

But then I wanted to add a system for joining the game manually, and then I realised if I wanted to do that, I’d have to make an array containing all the current players and then append all of those to the “targets” list in the multi-target camera’s script.

I’ve tried quite a few times, but each time I get an error basically saying that it hasn’t appended the children, just the parent node.

I don’t really know how to do this, so any help would be great. :slight_smile:

:bust_in_silhouette: Reply From: Inces

every array has append_array method, that is used percisely for combining multielement arrays with each other. Just use this :wink:

Before version 3.5 this method didn’t exist, but You could still do it manually :

for child in x.get_children() :
       targetsarray.append(child)

Thank you!

Yes, that’s pretty much what I did.
(I answered my own question, but it must not have sent. I have bad internet here xD).

Here was my solution:

for _i in $Players.get_children ():
	Players.append(_i)
	camera.targets.append(_i)

Which worked perfectly.
Thank you for replying though! :smiley:

Redical | 2023-01-09 08:33

No problem :wink:
Just to make sure You understood fully my answer :
You can also do it in one line of code, one level of optimization better :

Players.append_array($Players.get_children())
camera.targets = Players.duplicate()

Inces | 2023-01-09 16:28

Oh, cool!
Thanks!
I might actually do that, as now that I’m making my first game I’m discovering how important it is to actually optimize code, not just to make it run better, but also so that you can see what it does more easily in the future.
Thanks for the help! :smiley:

Redical | 2023-01-09 22:03

Hehe let me tell You, very often optimizing the code will make it less readable !. And I believe readable code is almost always a better choice, unless your project actually framedrops :wink:

Inces | 2023-01-10 13:59

Ah, okay!
Good to know.
I suppose it’s a bit on what you prefer as well;
whether you prefer extremely optimized code as opposed to very readable code.

I’m not really experienced enough to have an opinion on that yet, but I definitely think I agree with you on liking when you can see exactly what the code does.

Thanks! :smiley:

Redical | 2023-01-11 09:00