Number Parsing in GDScript
Parsing numbers from strings is a basic but common task in many programs; here’s how to do it in GDScript.
To run this script, attach it to a Node in your Godot scene and run the scene. The output will be printed to the Godot output panel.
In GDScript, number parsing is generally simpler than in some other languages:
float()
is used to parse floating-point numbers.int()
is used to parse integers. It doesn’t require specifying a base or bit size.- For hexadecimal numbers, we use the
hex_to_int()
method on strings. - There’s no separate function for parsing unsigned integers;
int()
is used for all integer types. - GDScript doesn’t have a direct equivalent to
Atoi
, but int()
serves the same purpose for base-10 integer parsing. - Unlike some languages, parsing functions in GDScript don’t return errors. Instead, they return 0 for invalid input.
Next, we’ll look at another common parsing task: URLs.