Author Topic: set_text_key for Text Surfaces  (Read 126 times)

llamazing

  • Full Member
  • ***
  • Posts: 139
    • View Profile
set_text_key for Text Surfaces
« on: October 21, 2018, 07:44:37 pm »
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?
http://www.solarus-games.org/doc/latest/lua_api_text_surface.html

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?

llamazing

  • Full Member
  • ***
  • Posts: 139
    • View Profile
Re: set_text_key for Text Surfaces
« Reply #1 on: October 24, 2018, 05:37:07 am »
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?).

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1144
    • View Profile
Re: set_text_key for Text Surfaces
« Reply #2 on: October 24, 2018, 02:02:04 pm »
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.

llamazing

  • Full Member
  • ***
  • Posts: 139
    • View Profile
Re: set_text_key for Text Surfaces
« Reply #3 on: October 25, 2018, 01:33:32 am »
Done!

Issue #1285 opened

llamazing

  • Full Member
  • ***
  • Posts: 139
    • View Profile
Re: set_text_key for Text Surfaces
« Reply #4 on: October 26, 2018, 02:07:47 am »
Here is an interim solution to fix the behavior using a lua script:
Code: Lua
  1. local text_surface_keys_list = setmetatable({}, {__mode = "k"}) --list of text surfaces to update on language change
  2. local text_surface_meta = sol.main.get_metatable("text_surface")
  3.  
  4. local old_set_text_key = text_surface_meta.set_text_key
  5. function text_surface_meta:set_text_key(key, ...)
  6.     text_surface_keys_list[self] = key --add text surface to list to automatically update on language change and save key
  7.     return old_set_text_key(self, key, ...)
  8. end
  9.  
  10. local old_set_text = text_surface_meta.set_text
  11. function text_surface_meta:set_text(...)
  12.     text_surface_keys_list[self] = nil --no longer automatically update this text surface when language changed
  13.     return old_set_text(self, ...)
  14. end
  15.  
  16. local old_set_lang = sol.language.set_language
  17. function sol.language.set_language(...)
  18.     old_set_lang(...) --change the language
  19.    
  20.     --update text of active text surfaces assigned a text key
  21.     for text_surface, text_key in pairs(text_surface_keys_list) do
  22.         text_surface:set_text_key(text_key)
  23.     end
  24. end