-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Add fallthrough for match
cases
#160
Comments
As a temporary solution, you could do:
That will loop it (__case + 1) times and do something until it hits 0. |
I thought Honestly though, for the OP's system I would devise some in-script abstraction to perform this, because using a bunch of For instance, I would add conditions and results to some array, then walk through the list, running everything greater than the given value. If you need actions, the array can be a list of functions. |
|
Since the var value = 2
match value:
1:
print("Value is one")
2:
print("More than one")
1, 2:
print("That's all folks") It should print: # More than one
# That's all folks |
Implicit fallthrough is the source of many bugs in programming, so I would rather not have it become the default in GDScript. Having an explicit |
It is an explicit fallthrough since there are 2 caes that match the same condition. |
Referencing godotengine/godot#37684 which aimed at implementing this proposal (but got stale as there was no consensus yet on how the feature should behave and/or be named exactly). |
Describe the project you are working on:
It's an RPG where when a character levels, specific stats are increased for example speed, skills, etc. The problem that this proposal will be addressing isn't specific to ours but moreso a feature that offers a solution to simplify the amount of work required for a situation.
Describe the problem or limitation you are having in your project:
Admittedly I wouldn't call it a problem, it's more of a QoL change where a keyword is added such as
fallthrough
where in amatch
statement, if used the next block is executed without the need for an additional check or comparison.The problem we initially ran into was that if the player is say level 3, then we would like everything that happens to them on lvl 3 to happen, then 2, then 1.
However due to the way that
continue
currently works where it rechecks the value being matched, onlyOnly this prints
is put into console. We eventually found a solution (described below) however I still think the feature would be useful.Describe how this feature / enhancement will help you overcome this problem or limitation:
I am primarily making this feature proposal because having used
switch case
before in other languages, I assumed that using the keywordcontinue
would effectively act as a fallthrough. However it seems that Godot will continue to check for a match instead of just executing the next case as it would in Java for example. From my understanding that Godot attempts to be a very flexible toolkit not beholden to other languages/engines, but I think that adding fallthrough simplifies the problem solving needed for certain logic problems.Show a mock up screenshots/video or a flow diagram explaining how your proposal will work:
Not... entirely sure what to put here, but effectively what I'm proposing would be a keyword
fallthrough
or something along those lines where when used it continues and executes the next block without doing a check on the values. Adding a keyword this way would maintain backwards compatability so no previously writtenmatch
statements would need to be rewritten.Describe implementation detail for your proposal (in code), if possible:
A keyword
fallthrough
where if used in a match, the next code block is executed without the need for a comparison.Console Output:
If this enhancement will not be used often, can it be worked around with a few lines of script?:
Using the current
match
you could do thisHowever this runs into a space problem where if it goes on to say value 100 it takes up a tone of space and it still has to run a check through the list of values.
Another alternative I thought of would be to start from the lowest level and check climbing up using if statements.
This helps cut back on the number of checks being done, however still runs into a space problem especially with the amount of indentation.
This is actually the solution that we're using, however it doesn't solve a case where functionally you would like two of the options to be connected, and one to be independent.
This now solves the above problem, but requires the changing of the variable
val
. Alternatively the value could be duplicated to a temp value but that would introduce another variable iffallthrough
was available this could be simplified to:No temp variable or value changing needed. It also simplifies a lot of the through process of how to solve the issue (as the above code blocks were in fact how we iterated on the issue).
Is there a reason why this should be core and not an add-on in the asset library?:
This could be an asset, however I think there can be situations other than my use case. In general for instance:
It's mostly weird to me that seemingly I fallthrough wasn't implemented when
match
was first created, but of course that's just a personal preference. Though apparently the issue has been raised before so there is precedence.https://www.reddit.com/r/godot/comments/b8gqli/match_fallthrough_with_continue_gdscript_help/
I apologies if this is way too long and verbose, but hopefully this can get added to Godot's core
and if not someone plz make an addon I will use it ty!The text was updated successfully, but these errors were encountered: