• Godot HelpProgramming
  • Is there a more efficient way of doing different functions on several members of an array?

I have an array and want to periodically perform different functions on different members. My array is for a music player node, and I want to dynamically turn the different layers up or down. The music player itself works perfectly, but implementing the different functions has proven a bit... Cumbersome. The code I have at the moment actually does the job just fine - but, as you'll see, it is extremely redundant. Is there a more efficient way of doing this?

func _process(delta): intensity = (float(drone_nests.drones.size())/drone_nests.Max_Drones)*4 intensity = floor(intensity)+1 score_bar.text = str("SCORE: " + str(floor(score))) play_arrangement(intensity)

...

func play_arrangement(num): if num == 1 && arr != 1: arr = 1 ##--FADE-INS--## music_a._fadeIn(0) music_a._fadeIn(1) ##--FADE-OUTS--## music_a._fadeOut(2) music_a._fadeOut(3) music_a._fadeOut(4) music_a._fadeOut(5) music_a._fadeOut(6) music_a._fadeOut(7) music_a._fadeOut(8) music_a._fadeOut(9) music_a._fadeOut(10) music_a._fadeOut(11) if num == 2 && arr != 2: arr = 2 ##--FADE-INS--## music_a._fadeIn(0) music_a._fadeIn(1) music_a._fadeIn(2) music_a._fadeIn(3) music_a._fadeIn(4) ##--FADE-OUTS--## music_a._fadeOut(5) music_a._fadeOut(6) music_a._fadeOut(7) music_a._fadeOut(8) music_a._fadeOut(9) music_a._fadeOut(10) music_a._fadeOut(11) if num == 3 && arr != 3: arr = 3 ##--FADE-INS--## music_a._fadeIn(0) music_a._fadeIn(1) music_a._fadeIn(2) music_a._fadeIn(3) music_a._fadeIn(4) music_a._fadeIn(5) music_a._fadeIn(6) music_a._fadeIn(7) music_a._fadeIn(8) ##--FADE-OUTS--## music_a._fadeOut(9) music_a._fadeOut(10) music_a._fadeOut(11) if num == 4 && arr != 4: arr = 4 ##--FADE-INS--## music_a._fadeIn(0) music_a._fadeIn(1) music_a._fadeIn(2) music_a._fadeIn(3) music_a._fadeIn(4) music_a._fadeIn(5) music_a._fadeIn(6) music_a._fadeIn(7) music_a._fadeIn(8) music_a._fadeIn(9) music_a._fadeIn(10) music_a._fadeIn(11)

So, the principle is, the more drones there are in the level (up to the max_drones count), the more layers of music come in. My original plan was to have all 12 layers move independently, but as you can see, doing that would require 8 more of these monstrous code blocks... Unless there is a simpler way of doing this?

You can create a list of fade-ins and fade-outs, like this (supposing the numbers are sequences):

var fade_in_list = []
var fade_out_list = []
if num == 1:
    fade_in_list = range(2)
    fade_out_list = range(2, 11)
if num == 2:
    fade_in_list = range(5)
    fade_out_list = range(5, 11)
# and so on for each num...
# then just iterate on each list to fadeIn/Out each list element
for i in fade_out_list:
    music_a._fadeOut(i)
for i in fade_in_list:
    music_a._fadeIn(i)

See range() function for more info.

Also, if they are not sequences, you can create list indices on each condition, like:

if num == 18:  # arbitrary example
    fade_in_list = [1,5,6,7,8,9,10,12]
    fade_out_list = [2,3,4,11]

Or better (if I understood your code correctly):

func play_arrangement(num):
    for i in range(num + 1):
        music_a._fadeIn(i)
    for i in range(num + 1, 13):
        music_a._fadeOut(i)

WHOA! That last example there is probably the best of the lot, and I can't believe I didn't think of that! Thank you, man, that helps a lot. Damn, I can probably do the full 12 tracks now. Only thing is - if I'm understanding properly - I'd reverse the fadeIn and fadeOut, because it seems to me your example would fade in everything above num, and fade out everything below.

EDIT: Nope, I misunderstood. Your code works perfectly, only I had to rescale to 11, as there are only 12 tracks in my arrangement. Still, thank you so much for this insight. I thought of something along the lines thatyou posted, but couldn't get my thoughts into code. I may just add this little snippet into the music player itself as its own function- perhaps "func muteAboveLayer(layer)". Thanks again!

5 years later