Strings and Runes in GDScript

# A GDScript string is a sequence of characters. The language
# and the standard library treat strings specially - as
# containers of text encoded in UTF-8.
# In GDScript, strings are immutable sequences of Unicode characters.

# The `const` keyword is used to declare constants.
const s = "สวัสดี"

# Since strings are sequences of characters, this
# will produce the length of the string in characters.
print("Len:", s.length())

# Indexing into a string produces the character at
# each index. This loop generates the hex values of all
# the characters in `s`.
for i in range(s.length()):
    print("%x " % s.ord_at(i))
print()

# To count how many characters are in a string, we can use
# the `length()` method. Note that in GDScript, this counts
# the number of Unicode characters, not bytes.
print("Character count:", s.length())

# A `for` loop can iterate over characters in a string directly.
for i in range(s.length()):
    var character = s[i]
    print("U+%X '%s' starts at %d" % [character.unicode_at(0), character, i])

# This demonstrates passing a character to a function.
print("\nUsing explicit indexing")
for i in range(s.length()):
    var character = s[i]
    print("U+%X '%s' starts at %d" % [character.unicode_at(0), character, i])
    examine_character(character)

func examine_character(c):
    # We can compare a character value to a character literal directly.
    if c == 't':
        print("found tee")
    elif c == 'ส':
        print("found so sua")

# Running this script would produce output similar to:
# Len: 6
# e0 b8 aa e0 b8 a7 e0 b8 b1 e0 b8 aa e0 b8 94 e0 b8 b5 
# Character count: 6
# U+E2A 'ส' starts at 0
# U+E27 'ว' starts at 1
# U+E31 'ั' starts at 2
# U+E2A 'ส' starts at 3
# U+E14 'ด' starts at 4
# U+E35 'ี' starts at 5
# 
# Using explicit indexing
# U+E2A 'ส' starts at 0
# found so sua
# U+E27 'ว' starts at 1
# U+E31 'ั' starts at 2
# U+E2A 'ส' starts at 3
# found so sua
# U+E14 'ด' starts at 4
# U+E35 'ี' starts at 5

In this GDScript version:

  1. We use const to declare a constant string, similar to the original Go code.

  2. Instead of len(), we use the length() method to get the string length.

  3. GDScript doesn’t have a direct equivalent to Go’s byte-level access, so we use ord_at() to get the Unicode code point of each character.

  4. For rune counting, we simply use length() as GDScript strings are already Unicode-aware.

  5. We use a for loop with range() to iterate over the string, which is similar to Go’s range loop.

  6. Instead of utf8.DecodeRuneInString, we directly index into the string to get characters.

  7. We use unicode_at(0) to get the Unicode code point of a character.

  8. The examine_character function works similarly to the Go version, comparing characters directly.

Note that GDScript handles Unicode strings more transparently than Go, so some of the complexity in dealing with UTF-8 encoding is abstracted away. The output will be similar, but the byte-level details are not exposed in the same way as in Go.