How can I make a scientific calculator?

Godot Version

4.2.1 without C# support

Question

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

OP: I did find this - KCFresh’s GitHub, shared on reddit: an attempt to mimic the MS calculator… Godot4-Calculator/Resources/scripts/main.gd at main · kcfresh53/Godot4-Calculator · GitHub Which is the most useful thing I’ve found so far. I’m not sure I agree with having every single thing an individual function instead of processing things by type, but it is clean and comprehensive. I am not sure if it is capable of operating with more than two values (like idk what would happen if you put 3 + 2 + 1), bc I haven’t tested it. It seems this offers a way to integrate a more complex calculator directly inside Godot without being limited to 4 functions, or tortured by calling an API and dealing with that nightmare.

@zdrmlpzdrmlp
I am not sure why you posted this. It’s unhelpful at best.

Yes, there are a lot of great calculators in javascript/html/css online, but if OP specifically is using it as part of their game, they may need the functionality in the game. I find it abrasive that you are telling the OP what they need in their game, essentially scoffing at their valid question. They probably have a good reason for needing it, you could at least ask them instead of spewing sarcastic, potentially deprecating remarks.

If the game doesn’t have math “gimmick” as you suggested, but instead is actually dedicated to math(s) or uses it in a deeper way than basic operations, the calculator most certainly will need more than + - * and / . So, I don’t think you thought this through–ie are you suggesting somehow using the API for one of the online calculators (license allowing) and embedding that in Godot (which, as I understand it, will only work on compatibility/web export) is preferable? Or having something else open besides OP’s game? Or…? I’m not sure using one of those will be the answer OP needs.

I didn’t said this, you are making stuff up. Also, blaming me of killing the discussion which is not cool.
Reread what i post, please.

I indirectly points out that It’s not wrong to make scientific calculator in godot but the amount of scientific calculators out there can’t be denied which tells the solutions can either be using these calculators indirectly or actually implementing it to Gdscript or in-game engine. But you viewed it as being sarcastic and abrasive?

I did try to support if it’s for a game that he needs it, but there are no more reply by the OP after that. So i guess, it’s never be accomplished. Either the OP gave up or searched somewhere else.

And lastly, i pointed out that most games doesn’t need scientific calculator function to work, which i think should be a fact. But if the game OP be creating needed it, then it will be interesting and definitely will try to helped OP out. So the post is going somewhere else. but no more reply after that.

As matter of fact, the OP already left this forum never came back 11 hours after his last post after saying:

Didn’t even read what other posts about. which the next posts by trevor and me in the next 5 hours after he gone (last seen) from this forum.

(You didn’t make that calculator… ) but good find.

So yeah, the OP not even reading the posts after his last post. So don’t blame me for being abrasive and sarcastic in which i didn’t try to do too.

Well, this is not true, you see i did go through possible solution of using other calculators, directly or indirectly. You are just being rude for this.