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.
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.
E.G
var variable = localizedString["challengeGoldCost"] as IntVariable;
Debug.Log("The value is " + variable.Value);
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.
You need to cast it to the concrete type. One of these https://docs.unity3d.com/Packages/com.unity.localization@1.3/api/UnityEngine.Localization.SmartFormat.PersistentVariables.html
In this case it would be a StringVariable.
(localizedString["chestName"] as StringVariable).Value = "Musical Chest"
Awesome, thank you.
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.
Can you provide more details? Are you getting errors? What does your code look like?
The name “Leon” appear in the text, but i want to change to “Leonard” in Start.
I learning, some things is hard to get.
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;
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?
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
void Start()
{
localizedString = GetComponent<LocalizedStringEvent>().StringReference;
}
Now you should reference the same localized string.
Thank u so much! I forgot the reference =D
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.
Ah great, somehow I missed this while going through the documentation.
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.
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?
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
var enumerator = ((IEnumerable<KeyValuePair<string, IVariable>>)localizedString).GetEnumerator();
while (enumerator.MoveNext())
{
var name = enumerator.Current.Key;
var variable = enumerator.Current.Value;
}
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.
Thanks