Search Unity

Resolved Getting string reference from reference table

Discussion in 'Localization Tools' started by DucaDiMonteSberna, Feb 1, 2024.

  1. DucaDiMonteSberna

    DucaDiMonteSberna

    Joined:
    Jan 18, 2018
    Posts:
    79
    I am trying to get a string reference from reference table.
    I must use the INDEX to select the proper string because I'm bound from another script.
    I just need the localizedstring to set the value of MAX in the string shown to the player. but I cannot get the string from the string table, I cannot figure out the code to do it.
    (Also why I have to set the local variables in the LocalizedString instead of the LocalizedStringEvent since they are shown there?)

    Code (csharp):
    1.  
    2.     public void FillWithMission(MissionBase m, MissionUI owner)
    3.     {
    4.         //descriptionText.text = m.GetMissionDesc();
    5.  
    6.         // get index of mission and max to reach from the mission entry
    7.         int index = Array.IndexOf(Enum.GetValues(typeof(MissionType)), m.GetMissionType());
    8.         float value = m.GetMax();
    9.  
    10.         //tries to get the correct entry from a given entry table given the index provided in the function above (string entries in the table follows the same patterns)
    11.         StringTable descriptionsTable = missionDescTable.GetTable();
    12. //THIS LINE GIVES ME THE ERROR:
    13.         LocalizedString localizedString = descriptionsTable.GetLocalizedString(index);
    14.  
    15.         descriptionEvent.StringReference.SetReference(missionDescTable.TableReference, descriptionsTable.SharedData.Entries[index].Id);
    16.  
    17.         // sets the value found in missions to the smart string
    18.         FloatVariable floatToSet = null;
    19.         if (!localizedString.TryGetValue("quantity", out var variable))
    20.         {
    21.             floatToSet = new FloatVariable();
    22.             localizedString.Add("quantity", floatToSet);
    23.         }
    24.         else
    25.         {
    26.             floatToSet = variable as FloatVariable;
    27.         }
    28.  
    29.         floatToSet.Value = value;
    30.  
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    StringTable does not have a method called GetLocalizedString, thats why you get an error.
    There's some confusion in the code around LocalizedStrings.
    I would not recommend using indexes to access the string tables, the items may not be in the order you expect. For your example, you could name the entries after the Enum names or assign the enum values as the entry ids (except for 0).

    Although this should work if the indexes are correct:

    Code (csharp):
    1. public void FillWithMission(MissionBase m, MissionUI owner)
    2. {
    3.     // get index of mission and max to reach from the mission entry
    4.     int index = Array.IndexOf(Enum.GetValues(typeof(MissionType)), m.GetMissionType());
    5.     float value = m.GetMax();
    6.  
    7.     StringTable descriptionsTable = missionDescTable.GetTable();
    8.     descriptionEvent.StringReference.SetReference(missionDescTable.TableReference, descriptionsTable.SharedData.Entries[index].Id);
    9.  
    10.     // sets the value found in missions to the smart string
    11.     FloatVariable floatToSet = null;
    12.     if (!descriptionEvent.StringReference.TryGetValue("quantity", out var variable))
    13.     {
    14.         floatToSet = new FloatVariable();
    15.         descriptionEvent.StringReference.Add("quantity", floatToSet);
    16.     }
    17.     else
    18.     {
    19.         floatToSet = variable as FloatVariable;
    20.     }
    21.  
    22.     floatToSet.Value = value;
    The variables are part of the LocalizedString, the LocalizeStringEvent is showing the LocalizedSring property drawer. If you add a LocalizedString to your class you will see the same editor.
     
    DucaDiMonteSberna likes this.
  3. DucaDiMonteSberna

    DucaDiMonteSberna

    Joined:
    Jan 18, 2018
    Posts:
    79
    I see... I just needed to grab the LocalizedString from the LocalizedStringEvent in order to set variables on it... it was not needed to "extract" it from a StringTable. Well that works thanks! I know the ID stuff is not that good but it's working, and otherwise I have to modify a lot of the code in other scripts (that are not inheriting to monobehaviour, so I an't even reference public stuff from there). Anyway It's working, thanks!
     
    karl_jones likes this.