I have a question regarding UI text and PlayerPrefs. The player gets to choose a character at my start scene. Then in the different scenes I have UI text that needs to automatically change words depending on the choosen characters sex.
Eg: ”Shadow took his spear and went into the dungeon. His arms where tired”
Lets say the character choosen is the girl instead. Then I need to change his to her. Almost like a ”search and replace” function which is called automatically depending on players choice.
I have many story UI text objects in different scenes and I an looking for the best way to make this word exchange automatic.
I have managed via PlayerPrefs to get the character to change depending on the choice made on the startscene. Can I set his/her etc as a PlayerPrefs string the same way? If so, how do I best go about calling that script on my UI text elements?
This is a sort of localisation issue.
If you’re not using any localisation tools and making something on your own then I would recommend making your own localisation design, it is not that hard and will make it a lot simpler along the way.
These are 3 ways you could do it.
Have a definition of all the texts in the game, can be in an external file that you read into the game, or could be in the code itself (if not too big). Have one text for each gender. Whenever needed in the game, make a call for the string in the definition, based on the gender, you can use a dictionary.
Have one definition for each text in one gender, similar to the first way and use a method to replace gender works every time you call the strings.
Or just make a method that converts gender generically for any texts you input in and just call it whenever you need to change it.
Pick one depending on how big your project is and how much accuracy you want the texts to have, from top to bottom.
Also consider that you have a limited space on Playerprefs, as it stays in the OS registry (usually as far as I know) so it might not be enough to store all your strings there.
Following on from FernandoHC’s point, if you wanted to try your own code, you could try something along these lines ( note: this is untested and only for illustration ) :
A formatter:
public class CFormatter
{
static public void Populate(Dictionary<string, string> tokens)
{
// Merge tokens into m_tokens.
}
static public string Format( string sourceText )
{
StringBuilder sb = new StringBuilder(sourceText);
foreach (var token in m_tokens)
{
if(sourceText.Contains(token.Key))
sb.Replace(token.Key, token.Value);
}
return sb.ToString();
}
static Dictionary<string, string> m_tokens;
}
And to use it:
public class CUseFormatter
{
public void TestFormatter()
{
var source = new Dictionary<string, string> {
{ "[[GENDER_PRONOUN]]", "her" }
, { "[[PLAYER_NAME]]", "Hermione" } };
CFormatter.Populate( source );
var formatted = CFormatter.Format("[[PLAYER_NAME]] went to [[GENDER_PRONOUN]] home.");
}
}
Option 2 would be my choice, mixing with replaceable tags instead of actual words.
You can use something like what @Doub_B posted.
Regarding a tool, I have only used private tools provided by the publishers so I don’t really know any openly commercial one. I’m sure there are some, I just have never used others.
Having about over 10k texts I would strongly recommend file management, segmented by each level, loading only parts of it into memory, depending on the area of the game, if on mobile.
Yes you can, the source of the text is not an issue. It just needs to get from wherever it is stored into the formatting dictionary (if that is the method you are referring to).