Array of nodes / Assign value to an array in C style

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

I want to use an array to my store my songs, so I need to store AudioPlayerStream nodes into the array.

I created my array like this

MySongs = []

Then the first thing that came to my mind in order to add an element to the array was this:

MySongs[1] = $MySong 

But It doesn’t work, I had to make it this way instead:

MySongs.append($MySong)

Also, something like the following also doesn’t work

MySongs = [$MySong_1 , $MySong_2 , $MySong_3]

I’m missing something here, can someone show me all the possible ways to make an assignment of nodes to an array?

Thanks.

:bust_in_silhouette: Reply From: kidscancode

When you do

MySongs = []

You’ve created an empty array. It has no elements, so you can’t put something in Mysongs[1] because there are no “slots”.

If you had this:

MySongs = [1, 2, 3]
MySongs[1] = 4

you’d be fine. The array would be [1, 4, 3].

append() is indeed the way to add another item to the end of the existing array. See here for more examples.

MySongs = [$MySong_1 , $MySong_2 , $MySong_3]

What didn’t work about this?

Thank you! I now understand!

TGMG | 2020-06-18 03:03