Describe the project you are working on
Complex project using GDScript.
Describe the problem or limitation you are having in your project
This was actually suggested in the past (e.g. godotengine/godot#7222 and partially godotengine/godot#20634), but I've just encountered a case where it could be very convenient and after brief consideration it has a few advantages.
So my problem I encountered just now is:
I have a method, let's say get_value() which returns an int. I used it like
if get_value(sth):
do_something()
BUT, I now want to use the value from the method. I have to do:
var val = get_value(sth)
if val:
do_something2(val)
This code has several problems:
- it's one line longer
val is not needed in my scope, only inside the condition
- consequently, if I want to use it multiple times in the same scope, I can't copy-paste it
var val = get_value(sth)
if val:
do_something2(val)
var val = get_value(sth2) # value is re-declared. Need to drop var part
if val:
do_something3(val)
As you can see, it's pretty meh.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
The problem disappears when declaring variable inside condition is allowed. However, instead of doing if a = something: I propose something a bit different: if var a = something:
The above code changes into
if var val = get_value(sth):
do_something2(val)
if var val = get_value(sth2):
do_something3(val)
val exists only inside condition
- I can easily copy-paste-change it
- it's shorter
- the
var part ensures that you don't make the common mistake of accidental asignement inside condition
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Just allow if var a = value syntax.
EDIT: while var a = value would be useful too (see #2727 (comment))
If this enhancement will not be used often, can it be worked around with a few lines of script?
The point is to avoid these lines.
Is there a reason why this should be core and not an add-on in the asset library?
Can't be an addon.
Describe the project you are working on
Complex project using GDScript.
Describe the problem or limitation you are having in your project
This was actually suggested in the past (e.g. godotengine/godot#7222 and partially godotengine/godot#20634), but I've just encountered a case where it could be very convenient and after brief consideration it has a few advantages.
So my problem I encountered just now is:
I have a method, let's say
get_value()which returns anint. I used it likeBUT, I now want to use the value from the method. I have to do:
This code has several problems:
valis not needed in my scope, only inside the conditionAs you can see, it's pretty meh.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
The problem disappears when declaring variable inside condition is allowed. However, instead of doing
if a = something:I propose something a bit different:if var a = something:The above code changes into
valexists only inside conditionvarpart ensures that you don't make the common mistake of accidental asignement inside conditionDescribe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Just allow
if var a = valuesyntax.EDIT:
while var a = valuewould be useful too (see #2727 (comment))If this enhancement will not be used often, can it be worked around with a few lines of script?
The point is to avoid these lines.
Is there a reason why this should be core and not an add-on in the asset library?
Can't be an addon.