- Home /
if,if,if, string cases, and if not, then something else?
I've added chat emotes, and commands to my chat, but how do I check for several cases of inputs, and if none of them just do a normal one. e.g.
If /dance Then....
If /Smile Then...
And in the end, if nothing of the ones above, then do normal text. How do I do this context of many if cases, or could I use a wild card, to check if first character of the string is a "/" .. Cutout from the code (.js):
if(inputField == "/smile")
{
inputField = "ARRRRRRRRRRRRRRRGH SMILE!";
}
else
{
// Normal chat output would go here.
}
I could make an If (.. || .. || .. || .. || .. ) then find out what one it was, else do normal. But there must be an easier way, like to check if the string starts with / or not?
Answer by AlucardJay · Jul 21, 2012 at 11:56 PM
Use a Switch-Case :
private var inputField : String = "/smile";
switch(inputField )
{
case "/smile" :
// do stuff for condition 1
outputField = "ARRRRRRRRRRRRRRRGH SMILE!";
break;
case "/frown" :
// do stuff for condition 2
outputField = "why so down?";
break;
case "/angry" :
// do stuff for condition 3
outputField = "Look Out";
break;
default :
// do stuff if none of the above
outputField = "Nice Day !";
break;
}
Note: I havn't used this with strings, so if you have any probs, create an array of strings and just have each case as a reference to that array i.e. var inputEmotes : String[3];
then case inputEmotes[0]: case inputEmotes[1]:
etc. let me know and I shall submit a tested working script.
Also working on this project, and the string code worked, including the style colouring for emotes, thank you.