Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Editing Local Variables in runtime

Discussion in 'Localization Tools' started by emredesu, May 19, 2022.

  1. emredesu

    emredesu

    Joined:
    Aug 12, 2021
    Posts:
    55
    How can I access and edit the "Local Variables" of a Localize String Event from a script? I want to edit the gold cost of a chest during runtime using a value from a script, but I couldn't find how to do so.

     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,275
  3. emredesu

    emredesu

    Joined:
    Aug 12, 2021
    Posts:
    55
    Thank you very much for the lightning fast answer, and I've seen this documentation on how to retrieve local variables, however it doesn't explain how to set them. How can I initialize an IVariable so I can assign it when doing
    localizedString["chestName"] = "Musical Chest"
    ? Currently I get a
     "Cannot implicitly convert type 'string' to 'UnityEngine.LocaIization.SmartFormat.PersistentVariabIes.IVariabIe'"
    error.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,275
    You need to cast it to the concrete type. One of these https://docs.unity3d.com/Packages/c...lization.SmartFormat.PersistentVariables.html

    In this case it would be a StringVariable.

    Code (csharp):
    1. (localizedString["chestName"] as StringVariable).Value = "Musical Chest"
     
    c-Row and emredesu like this.
  5. emredesu

    emredesu

    Joined:
    Aug 12, 2021
    Posts:
    55
    karl_jones likes this.
  6. Rangeroliver

    Rangeroliver

    Joined:
    Mar 13, 2017
    Posts:
    4
    I was trying to follow the steps above to acces "Local Variables", in my script I cant use "StringVariable" or "IntVariable", using UnityEngine.Localization library. I couldnt use the examples above.
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,275
    Can you provide more details? Are you getting errors? What does your code look like?
     
  8. Rangeroliver

    Rangeroliver

    Joined:
    Mar 13, 2017
    Posts:
    4

    The name "Leon" appear in the text, but i want to change to "Leonard" in Start.
    I learning, some things is hard to get.
     

    Attached Files:

    • Code.png
      Code.png
      File size:
      52.5 KB
      Views:
      112
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,275
    That should work. Looks like you are just missing a using statement, Visual Studio should be able to fix this automatically if you hover over the error. You need
    using UnityEngine.Localization.SmartFormat.PersistentVariables;
     
  10. Rangeroliver

    Rangeroliver

    Joined:
    Mar 13, 2017
    Posts:
    4
    Could not change the "Local Variables" yet, when I press play apeears "Leon" in the text, but when I press "P" nothing happens, should change to "Leonard".
    Is correct my localizedString reference?
     

    Attached Files:

  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,275
    Is the PlayerData script connected to a GameObject? The screenshot shows a LocalizedStringEvent, not a PlayerData.
    Try adding the script to the same GameObject and do something like

    Code (csharp):
    1. void Start()
    2. {
    3.     localizedString = GetComponent<LocalizedStringEvent>().StringReference;
    4. }
    Now you should reference the same localized string.
     
  12. Rangeroliver

    Rangeroliver

    Joined:
    Mar 13, 2017
    Posts:
    4

    Thank u so much! I forgot the reference =D
     
    karl_jones likes this.
  13. maikkanerva

    maikkanerva

    Joined:
    Dec 14, 2018
    Posts:
    27
    Is it possible to retrieve a list of local variables, like a List<(string, IVariable)>?
    What I'd like to do is to "clone" a localized string instace and copy the variables too.
     
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,275
    maikkanerva likes this.
  15. maikkanerva

    maikkanerva

    Joined:
    Dec 14, 2018
    Posts:
    27
    karl_jones likes this.
  16. Stephanommg

    Stephanommg

    Joined:
    Aug 18, 2014
    Posts:
    88
    Is it guaranteed that each element in Keys match the corresponding element (with the same index in the collection) in Values?

    I need to access the variable name at runtime, and, according to it, set a value.
     
  17. Stephanommg

    Stephanommg

    Joined:
    Aug 18, 2014
    Posts:
    88
    But I don't know their names at runtime, I must check them. I need to do something like:

    foreach (var l in localVariables) {
    if (l.VariableName == "playerName")
    l.Value = getPlayerName()
    else if ....
    }

    Is it really that there is no Variable data structure containing all data of a variable (i.e., name and value)? Or at least some way to correspond between them?
     
  18. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,275
    Sorry my reply was wrong, I thought you were referring to string table Values.
    For LocalizedString we have a Dictionary internally and just return its Values and Keys property values. I dont think a dictionary guarantees the values will be in the same order.

    I think you may want to use
    IEnumerator<KeyValuePair<string, IVariable>> IEnumerable<KeyValuePair<string, IVariable>>.GetEnumerator()


    Try this
    Code (csharp):
    1. var enumerator = ((IEnumerable<KeyValuePair<string, IVariable>>)localizedString).GetEnumerator();
    2. while (enumerator.MoveNext())
    3. {
    4.     var name = enumerator.Current.Key;
    5.     var variable = enumerator.Current.Value;  
    6. }
    This will give you the name and IVariable. You can make changes to the variable but if you need to change the name then you will need to use the other API as changes here wont be applied to the internal name.
     
    Last edited: Jan 26, 2024
  19. Stephanommg

    Stephanommg

    Joined:
    Aug 18, 2014
    Posts:
    88
    Thanks
     
    karl_jones likes this.