RATools icon indicating copy to clipboard operation
RATools copied to clipboard

[feature request] make strings iterable and indexable

Open gdeOo opened this issue 5 years ago • 1 comments

Would be useful for strings to be iterable and indexable like arrays and dictionaries.

Sample use case:

function text_starts_with(text_addr, needle) {
    cond = always_true()
    ptr = text_addr
    for char in needle {
        cond = cond && byte(ptr) == char
        ptr = ptr + 1
    }
    return cond
}

// this...
function my_trigger() => text_starts_with(0x1234, "game over")

// ...instead of what's currently possible:
function my_trigger() => text_starts_with(0x1234, ["g", "a", "m", "e", " ", "o", "v", "e", "r"])

gdeOo avatar Mar 10 '20 18:03 gdeOo

I see two issues with this:

  1. It assumes the character mapping is ASCII. Most systems that we currently support use per-game character mappings that depend on the tile layout for the current screen. For alphabetic characters, an offset could be applied, but if the non-alphabetic characters don't exactly line up with the alphabetic characters, things will break.
  2. We generally prefer solutions that don't rely on matching characters on the screen as they almost never work with alternate versions of the game (especially regional variants where the language changes). It's much preferable to find an event flag, or even just the unique identifier of the message being shown (the unique identifier does not change between regions and serves as a lookup into the region-specific string tables).

However, on the newer systems (like PSX and DS), ASCII (and sometimes UCS-2 [unicode]) are used, and assuming there isn't any option other than matching the string, it could be handy to have behavior similar to that which was proposed. Rather than making string iterable, I recommend adding an array_from_string function that would generate an array object from a string. The string is already UCS-2 internally to the compiler, so it would be simple to generate a list of integers from the string. This could even be enhanced in the future by providing a secondary parameter that would allow a custom mapping for those systems that are still using tile-based screen text.

Jamiras avatar Mar 11 '20 01:03 Jamiras