Our first example demonstrates base64 encoding and decoding. GDScript doesn’t have a built-in base64 module, so we’ll use a custom implementation.
This script defines functions for both standard and URL-compatible base64 encoding and decoding. Here’s what each part does:
We define a constant BASE64_CHARS containing the characters used in base64 encoding.
In the _ready() function, we demonstrate the usage of our base64 functions:
We encode and then decode a string using standard base64.
We do the same using URL-compatible base64.
The base64_encode() function takes a string input and returns its base64 encoded form.
The base64_decode() function takes a base64 encoded string and returns the original string.
The base64_url_encode() function is similar to base64_encode(), but replaces characters to make the output URL-safe.
The base64_url_decode() function reverses the URL-safe encoding.
To run this script, you would need to attach it to a Node in your Godot scene. The output would be printed to the Godot console.
Note that this implementation is not as efficient as built-in methods in other languages, but it demonstrates the concept of base64 encoding and decoding in GDScript.