BigNumber in Godot 4.4.1
All Aboard! The Train Defense Express was written in Godot 4.4.1. In Godot, integers are signed 64-bit, so the maximum value is 9,223,372,036,854,775,807. All Aboard is something of an idle/clicker game, and it had a problem until this most recent patch (0.7.3) where numbers would overflow and go negative, despite my best attempts to protect against it. Deciding enough is enough, I started to integrate BigNumber.gd from this GitHub page here: https://github.com/rohanrhu/BigCat
It worked great, but the code wasn't as optimized as it could be. It internally uses a Variant Array to store the interval value of the BigNumber, and it doesn't use static typing at every opportunity. Static typing makes GDScript, Godot's interpreted scripting language, run faster. Also, BigNumber doesn't extend RefCounted, which, if I understand Godot correctly, means that it extends Object, and memory has to be freed manually.
I went through the whole file and tried to improve performance everywhere I could (Pastebin). In my code, I was using it to add a number over 5,000 times. The performance improvement was significant: it went from over 300ms to around 90ms.
That wasn't enough for me though, because I wanted to use the class everywhere, and performance is critical, so I needed it to run as fast as possible. I decided to port it to C++ and compile it into the engine itself as a third-party module. Claude.ai did a lot of the initial code conversion, and I added a few functions that weren't in the original class. I had to remove the stuff about prime number generation; I don't need it for my project, and it seems like a nightmare to port to C++.
Source code: Pastebin
It took forever to get past all the compile errors, but it finally works and is fully integrated into my game. Running add() from GDScript 10,000 times took around 30ms on my computer. The max in-game gold went from 9,223,372,036,854,775,807 to 9,999,999,999,999,999,999,999,999 (the decimal moved to the right 6 places). Of course, that's just my game's limit; it should be able to go on practically forever.
The only thing I really dislike about my implementation is the exposed static constants. I wanted there to be a BigNumber.ZERO, BigNumber.ONE, etc. just like the original implementation, but I'm not sure how to statically initialize Ref<BigNumber> like that, so I put them in a static BigNumber::initialize() function. It works fine for my project, but I'd want to do it differently if I were to put this on the Godot addon store.
Anyway, thanks for reading my blog. If you have any suggestions to improve the BigNumber port, feel free to comment.
Files
Get All Aboard! The Train Defense Express
All Aboard! The Train Defense Express
The best train-based auto-battler/survival/tower defense/idle/clicker game ever!
Status | In development |
Author | likadev |
Genre | Survival, Strategy |
Tags | 2D, Casual, Clicker, Godot, Idle, Indie, Pixel Art, Singleplayer, Tower Defense |
Languages | German, English, Spanish; Castilian, French, Italian, Japanese, Korean, Polish, Portuguese (Brazil), Russian, Chinese (Simplified) |
Accessibility | Interactive tutorial, One button |
Leave a comment
Log in with itch.io to leave a comment.