- Home /
How can i simplify multiple if-statements?
Hey guys, i'm working on a small game currently and was working on a function to allow multiple languages. My script works very well but i'm wondering if i can simplify my script a little.
public void SetLanguage()
{
language = PlayerPrefs.GetInt("language");
for (int i = 0; i < Object.Length; i++)
{
if (language == 0)
{
Object[i].ObjectText.text = Object[i].enString;
}
else if (language == 1)
{
Object[i].ObjectText.text = Object[i].deString;
}
}
}
if i would add more languages i would have to check for "else if (language == 2) , else if language == 3), etc...) is there a way to shorten the script?
Answer by sparkzbarca · Jun 20, 2017 at 04:02 AM
yep, do a string array and do .textstring[x] have that set by possibly even a global as that might actually make sense here that's an enum
so what you get roughly is
enum Language { English, German, French}
so what that means is
Language languageChoice = 0 //this is english, 0 is english, 1 is german, 2 is french however
language langaugeChoice = Langauge.English //also works, it's just a way of saying zero but in a much more readable not going to get screwed by it later when you make changes and add a language or something kind of way.
so what you can then do with this is because you know 0 is english, 1 german, 2 french you go
string[] text;
text = new string[3];
text[0] = //english text; text[1] = //german; text[2] = //french;
then Object[i].text[Languagechoice];
so now you simply ask the player what language they want, store that result in the Language choice variable and now you only have to call text[langaugechoice] and if they change from english to german, your text will change.
Excuse me, could you explain it more detailed please? Never worked with an enum
@Darkyne$$anonymous$$B check here ;) https://unity3d.com/learn/tutorials/topics/scripting/enumerations
Enums are just like say how with phones each number has a few letters associated with it and you can tell someone to dial 1800 lawyer ins$$anonymous$$d of the numbers because it's easy to remember.
An enum is just an int, just a number but you can get that by using a word. $$anonymous$$uch like you can look up a number with a name.
Emums aren't strictly needed, you could just use an int array, however if you do that you need to always remember that 0 is English, 1 is Spanish etc.
In unity an example is tags or layers.
Tags and layers are enums. When you place something in a layer or tag its actually a number, the name is so Insta of remembering that 0 is say terrain you can ins$$anonymous$$d put layer.terrain now layer.terrain IS zero but it's nice for reading code and writing it to put layer.terrain, this is purely a human convenience thing though, it will go through the compiler and come out zero.
But when you go later and read code or someone else does reading layer.terrain tells you a lot more about what layer your using than 0 just like later reading
Array [Language.english] makes much more clear what your code does than array[0]
Even if they mean the same thing.
So like using health =100 and putting health helps ins$$anonymous$$d of 100 using enums helps.
But they are at bottom arrays of ints
Answer by thedigitalminer · Jun 22, 2017 at 01:43 PM
I really like enum's for readability. I would be inclined to do something like this, but obviously edited slightly to work with your script :)
public enum PlayerLanguage
{
English,
German,
Spanish
}
class MyClass : MonoBehaviour
{
PlayerLanguage playerLanguage;
public void SetLanguage()
{
playerLanguage = (PlayerLanguage)PlayerPrefs.GetInt("language");
for (int i = 0; i < Object.Length; i++) {
switch (PlayerLanguage) {
case PlayerLanguage.English:
Object [i].ObjectText.text = Object [i].enString;
break;
case PlayerLanguage.German:
Object [i].ObjectText.text = Object [i].deString;
break;
case PlayerLanguage.Spanish:
Object [i].ObjectText.text = Object [i].esString;
break;
default:
break;
}
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Input.GetKey(KeyCode.E) requires multiple presses. 1 Answer
If statement behaves unexpectedly 3 Answers
Simplify Code 3 Answers