How can I make a scientific calculator?

Godot Version

4.2.1 without C# support


I’m new with godot engine. I’d like to make a scientific calculator just like the one included in windows system.

I had known that Expression class can be used with simple string. However, I don’t know how to make more complex calculatings…

Maybe I could write script like following idea below…
But I’m not sure that will work properly.

Making 3 variables, one for display, one for storage number temporarily and the other one for calculating.

When numbers’ and operators’ buttons are pressed, writing them into those two variables.

When special operator’s buttons are pressed, execute special operator’s calculating function with number storaged, put the Special(number) string into display variable and put the execution into calculating variable?

I’d imagine it could get quite complex. You’ll have to make some kind of parser/tokenizer system and boil what’s typed down into small chunks that you can then call functions on in a loop as you process the queue.

Start simple and get the basic gui working. Then add something new like square-root and just iterate.

Also have a look at lambda functions which you can store in a dict, for example, so if you have a key like “sqrt” then you can lookup a func directly and call it.

1 Like

Yeah that would be very complicated😂
Maybe I also need to prepare an extension designed especially for the complicated calculations like trigonometric functions and so on…

you could try using c# and then finding a library out there for dealing with math expressions.

i wouldnt question why you wanted to make scientific calculator in godot, while there are like, millions of calculator both offline and online that can do almost any equation…
if it’s for a game, then it will be interesting
because mostly game with maths gimmick only needs basic operators to function