Question about gdscript: Which is more efficient?

https://sopuli.xyz/post/4120489

Question about gdscript: Which is more efficient? - Sopuli

I’m just curious about which is the most efficient way of doing this (in a loop): o=[var1,var2,var3,varN][i] or match i: 0: o=var1 1: o=var2 2: o=var3 N-1: o=varN or var items = [var1,var2,var3,varN] for i in something(): o=items[i] Or is there a more efficient way of doing it?

If speed is actually important then I would consider writing it in another language looking for someone else’s implementation (personally). I try to focus on readability and doing the simplest implementation. I’ve been trying to use better function/variable names to explain what is happening at a glance instead of reading the whole thing. Have you considered using map for array iteration? I am hoping even if people don’t know how this work the intention is more readable than an equivalent for loop:

func _show_only_first_layer_dots(): var set_dots_visibility = func(layer): layer.get_node("Dots").visible = (layer == $Layers.get_child(0)) $Layers.get_children().map(set_dots_visibility)
func _show_only_first_layer_dots(): for c in $Layers.get_children(): c.get_node("Dots").visible = false $Layers.get_child(0).get_node("Dots").visible = true

Mines 10x more readable and I saved a line of code.

Simplicity is king.

If you’re working on the function then yes; everyone learns for loops fairly early on.

If you just need to know what it is intended to do then I would argue you didn’t need to read anymore than the function name but if you did then I’d argue just the name of the help function was easier to read than the whole for loop, no?

It’s a poor name choice then, because it actually says less about what it’s doing than the main function does.

Besides, what is the point of “looking further” just to stop at another function name? Wouldn’t looking further imply the need to review the implementation?

Seeing another function divides the code into another subsection. In the example it’s the only one there but if more was added then you could choose where to focus your attention on the implementation.
In practice it turns out the method to make just the first element visible was redundant anyway. It would be made visible during the setup function that all elements call.