set_text_key for Text Surfaces

Started by llamazing, October 21, 2018, 07:44:37 PM

Previous topic - Next topic
The quest documentation indicates that text_surface:set_text_key(key) is equivalent to text_surface:set_text(sol.language.get_string(key)), but is that really true?

If they are indeed equivalent then that would mean that the text wouldn't get updated when the language is changed and text_surface:set_text_key() would need to be called again just to update it. That doesn't seem right, could anyone confirm?

So after a bit of experimentation, I think the implementation is as I feared and that text_surfaces need to manually have the text key set again after a language change. This means that a list of all active text_surfaces has to be retained along with the set_text_key string it is supposed to have in order to be able to manually do the update after the language has changed.

How have people dealt with this problem in existing quests? Is it by only allowing the player to change the language while the game is not running? (and thus the only active text_surfaces are the ones displayed on the change language menu?).

That's right, text surfaces don't automatically update if the language is changed. So the documentation is correct about set_text_key() being just a shortcut to set_text(sol.language.get_string()).

In my games, the language can only be changed before starting the game so I only have to update manually the text surfaces of the file selection menu.
You can open a feature request to make text surfaces automatically update. That would be useful.

Here is an interim solution to fix the behavior using a lua script:
Code (lua) Select
local text_surface_keys_list = setmetatable({}, {__mode = "k"}) --list of text surfaces to update on language change
local text_surface_meta = sol.main.get_metatable("text_surface")

local old_set_text_key = text_surface_meta.set_text_key
function text_surface_meta:set_text_key(key, ...)
    text_surface_keys_list[self] = key --add text surface to list to automatically update on language change and save key
    return old_set_text_key(self, key, ...)

local old_set_text = text_surface_meta.set_text
function text_surface_meta:set_text(...)
    text_surface_keys_list[self] = nil --no longer automatically update this text surface when language changed
    return old_set_text(self, ...)

local old_set_lang = sol.language.set_language
function sol.language.set_language(...)
    old_set_lang(...) --change the language
    --update text of active text surfaces assigned a text key
    for text_surface, text_key in pairs(text_surface_keys_list) do