Skip to content
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

Support returning functions in match patterns #2337

Open
MaaaxiKing opened this issue Feb 23, 2021 · 9 comments
Open

Support returning functions in match patterns #2337

MaaaxiKing opened this issue Feb 23, 2021 · 9 comments

Comments

@MaaaxiKing
Copy link

Describe the project you are working on

Reaction game

Describe the problem or limitation you are having in your project

I cannot check for a text with tr() in match.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

You could use any function that returns something as a pattern in a match statement.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

match $Label.text:
	tr("foo"):
		print("hi")
	tr("bar"):
		print("ha")

If this enhancement will not be used often, can it be worked around with a few lines of script?

No

Is there a reason why this should be core and not an add-on in the asset library?

Yes, it's useful for everyone and not even possible to implement with an asset afaik.

@Calinou
Copy link
Member

Calinou commented Feb 23, 2021

Try this instead:

match tr($Label.text):
	"foo":
		print("hi")
	"bar":
		print("ha")

The values of match blocks must be constant expressions. This is likely done in the interest of performance, so they only have to be evaluated once when the script is compiled.

@MaaaxiKing
Copy link
Author

I tried and it did not work which also surprised me.

@MaaaxiKing
Copy link
Author

What you wrote makes that if the Label's text is "MY TEXT.", it will check if there's a translation with the key "MY TEXT." instead of checking for the translation.

@Calinou
Copy link
Member

Calinou commented Feb 23, 2021

@MaaaxiKing Out of curiosity, what's the end goal here? What are you trying to achieve?

@MaaaxiKing
Copy link
Author

I have a tutorial and there's text to display. Everytime the user does what he has to, I call next() and there I want to check what the current text ist. My current way is that I have a variable: index. And in next() I check for this with match and at the end of the function I increase index by 1.

@Calinou
Copy link
Member

Calinou commented Feb 24, 2021

I have a tutorial and there's text to display. Everytime the user does what he has to, I call next() and there I want to check what the current text ist. My current way is that I have a variable: index. And in next() I check for this with match and at the end of the function I increase index by 1.

I think you should have a separate variable that stores the untranslated text key at all times and use that for the match comparison instead. I wouldn't recommend relying on translated text for logic that can impact your game's behavior.

@DaGamingWolf
Copy link

i fully support this. it seems incredibly weird not to be able to use anything in a match statement that isn't a constant. it has hugely limited my code. i keep running into this over and over again. can't use variables as a pattern? this is silly. you should easily be able to compare a string with a bunch of different node names that vary because you have no idea what those names will be. if if statements can do it, there's no reason a match statement shouldn't also be able to.

@DaGamingWolf
Copy link

DaGamingWolf commented Jul 16, 2022

maybe this is the reason variables can no longer be used in match statements in 4.0. setters and getters return the value, and have been linked with declaration of variables. the loss of the ability to use variables in match statements is way to huge a loss imo not to reconfigure match to allow returning values for comparison.

@dalexeev
Copy link
Member

dalexeev commented Apr 29, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants