This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Are there any variants how to create a calculator? In addition, can i calculate something in Label, like 2 + 5 and etc? Help me please. Сould you give me an example of a simple calculator.
Thankful in advance

Godot version 3.2.4
in Engine by (41 points)

1 Answer

+1 vote
Best answer

I'd guess you're after the Expression class. WIth that, you can parse and execute a string-based expression.

See the details (and an example) here:

by (22,704 points)
selected by

In addition to the built-in functionality, you can write your own functions that can be called during the expression parsing (as outlined in the docs mentioned above).

However, you really won't be able to get something like x^y to be processed as a function call due to its structure / format.

My first thought would be to allow your User to add something like that to the expression string they build using your UI. Then, before passing the finished expression to the Expression class for processing, you could preprocess it as necessary.

So, you'd scan the finished expression string for that construct and replace it with a valid call to pow.

So, if you find 34^3 in the User's expression, you'd replace it with pow(34,3) before passing it to Expression.

If you had a general preprocessing step in your process, you could perform other, similar substitutions as necessary based on your needs...

i have no idease how i can do this. May be there is a document with guide or something else? I saw example with "double", but its dont help me.
With sqrt i made this script:

func _on_sqrtbutton_pressed():
    hidenline.set_text(line.get_text() + " sqrt(")
    display.set_text(display.get_text() + "√(")

display - its label show expression and result.
hidenline - this line user dont see. It is doing calculation and etc. But all showing only "display"

Yeah, that's similar to my suggestion. The main difference is that I'm suggesting you let the user construct an expression string that looks like whatever you want. For instance using whatever mathematical symbols and syntax you want to support (sqrt, power, ...).

Then, when the user finishes constructing the expression (on a traditional calculator, that'd be when the = button is pressed), you pass the user-defined expression to a preprocessor function that substitutes your allowed syntax with the syntax required by Godot's Expression class.

So, for example, you might allow a user to enter this expression:

5 + 17^3 * √5

Since the expression class can't parse that, you'd need to preprocess it to look like:

5 + pow(17,3) * sqrt(5)

To do that, you'd have to search the User-defined expression string and replace the parts where you've allowed special syntax with the syntax the Expression class requires.

With that done, you'd then pass the update expression to Expression for processing.

So, to recap:

  • Let the User enter the expression, including any special syntax you want to support
  • When the user is finished (presses the = button?)...

    • Pass the expression to your preprocessor function
    • Update the syntax as necessary for the Expression class
    • Get the result
    • Present the result to the User

I know that I need to replace pow with ^ but I do not understand how exactly to do it. May be use "for" "in", or there are other variant?

I'd probably use the Regular Expression class as the basis for the mentioned preprocessing function. That provides very powerful pattern matching and replacement capabilities. That said, regular expressions can get quite complex.

Short of that, you could also just step through the expression string a character at a time to manually identify and replace the interesting substrings.

Neither way will be trivial. I'd definitely go the Regular Expression route.

Otherwise, the easiest way to do this would be to simply require the user to enter a syntax that's already accepted by Godot's Expression class. In that case, there'd be no preprocessing necessary.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.