Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Changing the value of local variable in smart string via script

Discussion in 'Localization Tools' started by ratshayuu, Feb 25, 2023.

  1. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    Hi, I've read the following page but I couldn't understand, or I did something wrong.

    I have this smart string that counts the 'day' in-game. It should do a ++ whenever a new day begins.

    I've made a Variables Group Asset called "LocalVariable" and added an integer variable called 'day'. I may have done something wrong here because in the linked page above, in the third picture, there's a check box for "Addressable" and it doesn't have a (Variables Group Asset) after the name, unlike my group asset.

    I've added the Variables Group Asset in the Project Settings > Localization > Smart Format > Parser > Sources > Persistent Variables Source, named "global".

    Here's the script for TimeManager.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Localization.Settings;
    6. public class TimeManager : MonoBehaviour
    7. {
    8.     public int day;
    9.     public void UpdateDate()
    10.     {
    11.         var source = LocalizationSettings.StringDatabase.SmartFormatter.GetSourceExtension<PersistentVariablesSource>();
    12.         day = source["localVariable"]["day"] as IntVariable;
    13.         using (PersistentVariablesSource.UpdateScope())
    14.         {
    15.             EndOfDay();
    16.         }
    17.     }
    18.     public void EndOfDay()
    19.     {
    20.         day++;
    21.     }
    22. }
    23.  
    24.  
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,388
    Can you provide screenshots of the group and the day variable as well as the persistent source inspector?

    Your script day field should be
    Code (csharp):
    1. public IntVariable day;
     
  3. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    Sorry for the late reply, here they are:

    project settings: https://drive.google.com/file/d/1Oyx0hx7iNNR8FQDZ2a79Htr8xzvz-SWS/view?usp=share_link

    text that has the smart string: https://drive.google.com/file/d/1OlyUDPfasu5g3s86jtOIUHvPkEMoUqRU/view?usp=share_link

    Variable Group Assets: https://drive.google.com/file/d/1_ErwVRYjCcqZJJrsaZQ6rx8ba5CG_pf-/view?usp=share_link
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,388
    Oh thats a bit confusing. It looks like you have multiple variables called day in different places.
    Delete these variables
    upload_2023-2-25_18-59-6.png

    Now do this in script
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Localization.Settings;
    5.  
    6. public class TimeManager : MonoBehaviour
    7. {
    8.     public IntVariable day;
    9.     public void Start()
    10.     {
    11.         var source = LocalizationSettings.StringDatabase.SmartFormatter.GetSourceExtension<PersistentVariablesSource>();
    12.         day = source["global"]["day"] as IntVariable;
    13.         EndOfDay();
    14.     }
    15.     public void EndOfDay()
    16.     {
    17.         day.Value++;
    18.     }
    19. }
    This will use the global variable
     
  5. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    Ooh, I see. I've deleted it just now; however, it gave me this error below:
    Assets\Scripts\Master\TimeManager.cs(8,12): error CS0246: The type or namespace name 'IntVariable' could not be found (are you missing a using directive or an assembly reference?)

    I guessed not adding Localization and Localization.Tables were the problem but they weren't. Am I missing something?
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,388
    The namespace is UnityEngine.Localization.SmartFormat.PersistentVariables
     
  7. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Localization.Settings;
    5. using UnityEngine.Localization.SmartFormat.PersistentVariables;
    6.  
    7. public class TimeManager : MonoBehaviour
    8. {
    9.     public IntVariable day;
    10.  
    11.     public void Start()
    12.     {
    13.         var source = LocalizationSettings.StringDatabase.SmartFormatter.GetSourceExtension<PersistentVariablesSource>();
    14.         day = source["global"]["day"] as IntVariable;
    15.         EndOfDay();
    16.     }
    17.     public void EndOfDay()
    18.     {
    19.         day.Value++;
    20.     }
    21. }
    this is how it looks now, it gave me an error below:
    Assets\Scripts\Master\TimeManager.cs(13,92): error CS0246: The type or namespace name 'PersistentVariablesSource' could not be found (are you missing a using directive or an assembly reference?)
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,388
    Try UnityEngine.Localization.SmartFormat.Extensions

    Does your IDE not suggest a namespace? Visual studio should be able to automatically detect the missing namespaces.
     
  9. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    It almost never suggests me namespace. When I use Random.Range, it adds 'using.System' but that's about it :(

    The error mentioned before is gone now! :)
    The new error says: FormattingException: Error parsing format string: Could not evaluate the selector "day" at 1
    I checked out the old post regarding this matter here: https://forum.unity.com/threads/error-parsing-format-string.1011778/
    But I didn't catch where I need to drag my GameObject with the Localize String Event component.
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,388
    It should be {global.day}
     
  11. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    It works perfectly now!! Thank you so much for the help!! :D
     
    karl_jones likes this.