Make assign expressions return a value

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

I’ve been hanging around Godot Engine for a while and making my first project here right now. I’m used to coding in C# pretty much, but I got used to GDScript pretty fast. One thing to note is that sometimes I might need to write assign expressions inside of other statements to make the code pretty much more clean. Here is the example:

var type_chance := rand_range(0, 10)
if type_chance <= bomb_chance: bubble.type = 1
elif type_chance <= bomb_chance + lock_chance: bubble.type = 2
elif type_chance <= bomb_chance + lock_chance + coin_chance: bubble.type = 3
elif type_chance <= bomb_chance + lock_chance + coin_chance + gem_chance: bubble.type = 4
elif type_chance <= bomb_chance + lock_chance + coin_chance + gem_chance + blind_chance: bubble.type = 5
elif type_chance <= bomb_chance + lock_chance + coin_chance + gem_chance + blind_chance + timer_chance: bubble.type = 6
else: bubble.type = 0

Looks complicated, huh? Would like to introduce a new variable just not to overload this, but here comes the new thing:

var type_chance := rand_range(0, 10)
var type_comp := bomb_chance
if type_chance <= type_comp: bubble.type = 1
else:
    type_comp += lock_chance
    if type_chance <= type_comp: bubble.type = 2
    else:
        type_comp += coin_chance
        if type_chance <= type_comp: bubble.type = 3
        # etc...

You just can see that it will just overcomplicate the code with indentation.
In C# you can pretty much say something like:

if (someVar += otherVar <= thirdVar) DoSomething();

So I would like GDScript to have something like this too:

var type_chance := rand_range(0, 10)
var type_comp := bomb_chance
if type_chance <= type_comp: bubble.type = 1
elif type_chance <= type_comp += lock_chance: bubble.type = 2
elif type_chance <= type_comp += coin_chance: bubble.type = 3
elif type_chance <= type_comp += gem_chance: bubble.type = 4
elif type_chance <= type_comp += blind_chance: bubble.type = 5
elif type_chance <= type_comp += timer_chance: bubble.type = 6
else: bubble.type = 0

Looks neat, doesn’t it? (Besides the fact that I did some one-liners) This will just be a nice addition to GDScript, let me know what you think of it. Have a nice day!

:bust_in_silhouette: Reply From: njamster

This doesn’t seem to be a question, but a proposal. In which case, you shouldn’t post it here, but open up an issue on github. The GDscript-parser is currently rewritten for Godot 4.0, so now might be a good time to ask for any improvement really.

However, your example seems a bit constructed to me, there’s no real reason why you would need to assign the values during comparison, just do it in advance:

var bomb_chance  := 0.2
var lock_chance  := bomb_chance  + 0.1
var coin_chance  := lock_chance  + 0.1
var gem_chance   := coin_chance  + 0.1
var blind_chance := gem_chance   + 0.1
var timer_chance := blind_chance + 0.1

That alone makes your code a lot more readable:

var type_chance := rand_range(0, 1)

if type_chance <= bomb_chance:
    bubble.type = 1
elif type_chance <= lock_chance:
    bubble.type = 2
elif type_chance <= coin_chance:
    bubble.type = 3
elif type_chance <= gem_chance:
    bubble.type = 4
elif type_chance <= blind_chance:
    bubble.type = 5
elif type_chance <= timer_chance:
    bubble.type = 6

Which is not to say that I disagree with your proposal. Just want to point out, that your example might not be the best to convince other people of its usefulness. Personally I would rather prefer to be able to use comparisons in the match-statement:

match rand_range(0, 1):
    <= bomb_chance:
        bubble.type = 1
    <= lock_chance:
        bubble.type = 2
    <= coin_chance:
        bubble.type = 3
    <= gem_chance:
        bubble.type = 4
    <= blind_chance:
        bubble.type = 5
    <= timer_chance:
        bubble.type = 6
        

That has been proposed in the past, but not yet been implemented sadly.

Thanks! Didn’t even think about making it that way, because the chances were dynamically changing at some point of time (now they are not). Will definitely write an issue at Godot’s GitHub!
Also this idea with comparisons in match-statement is great, looks like when-statement in Kotlin.

altered | 2020-06-15 06:07

PS: Feature proposals should be opened on the Godot proposals repository, not on the main Godot repository as that one is intended to be used for bug reports only.

Calinou | 2020-06-29 13:31