-
-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to define variables inside conditions #2727
Comments
I think this makes sense, but not 100% sure about the syntax yet, so let me brainstorm... If we take for i in 100:
do_something(i) Using if var val = get_value(sth):
do_something2(val) That said, perhaps other parts could allow for var i in 100:
do_something(i)
while var state = get_state(sth):
do_something(state) I mean, I guess in any case this would touch expressions parsing in general, so should be applicable to other control flow operators, not just conditionals. |
my two cents:
|
Not really, because you are assigning the variable. |
Sorry, what I meant is that it would break expectation if you are going to use |
I like this idea, but people prefer to have less keywords in GDScript...
Probably not needed, it's something which does not cater to common use case as described in this proposal. |
Ok, I found use-case for
So with this we could do
instead of
|
|
Looks like implementing this proposal could indeed improve the linked list usage via script: #1522. |
I know, it's complicated, but I do like the |
I like go’s syntax: if foo := bar(); foo == “blah” {
// foo is “blah. Do something
} obviously this syntax isn’t very gdscript-like and could be massaged to fit conventions, but yea, i like that you can define a variable and have an explicit condition in a single line. It’s more verbose than the proposed solution but its far more flexible. Obviously, you can use any condition after the |
In C# syntax it would be something like if(thing is Type thingAsType) in gd could be something like if thing is Type var thing_as_type: |
Although I do like C#'s equivalent to Java's Pattern Matching for |
you mean technically difficult because of how gdscript works? |
yeah, basically (because of the colon operator for specifying the type) |
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:
val
is 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
val
exists only inside conditionvar
part 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 = 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.
The text was updated successfully, but these errors were encountered: