- Home /
If Statement Acting Strange
I'm trying to make a game in which you need to enter a code to get items you want. I have all the codes in a SQL table on my localhost and I'm using WWW and PHP to reach it. I'm also using String.Compare to compare a string received from the PHP script to a word or phrase. The errorMsg variable changes the value of a GUI Label which displays when the variable currentWindow is equal to Window.Failed. Here's the function that controls that:
function RegisterItem(code : String) {
registerURL = registerURL + "code=" + WWW.EscapeURL(code) + "&version=" + game.version;
// Post the URL to the site and create a download object to get the result.
var code_post = WWW(registerURL);
currentWindow = Window.Downloading;
yield code_post; // Wait until the download is done
if(code_post.error) {
errorMsg = code_post.error;
currentWindow = Window.NoConnection;
} else {
if(String.Compare(code_post.text, "Used", true) > 0){
errorMsg = "It seems that this code has already been used...";
currentWindow = Window.Failed;
Debug.Log(code_post.text);
} else if(String.Compare(code_post.text, "No code", true) > 0){
errorMsg = "This code is not in our database.";
currentWindow = Window.Failed;
Debug.Log(code_post.text);
} else if(String.Compare(code_post.text, "Version", true) > 0){
errorMsg = "Download the latest version to register this item.";
currentWindow = Window.Failed;
Debug.Log(code_post.text);
} else if(PlayerPrefs.HasKey(code_post.text)) {
errorMsg = "It seems that you already own this item.";
currentWindow = Window.Failed;
Debug.Log(code_post.text);
} else {
game.item = code_post.text;
game.items.Push(game.item);
currentWindow = Window.CodeSuccess;
}
}
}
I have the Debug Log print out what the value of code_post.text is to see what it is. When I test run the game and use a fake code I entered into the SQL table, I see that in the Debug Log, it prints out "Version", yet the GUI Label shows the phrase I put if code_post.text is equal to "Used". It doesn't seem to be a problem with the PHP script (although it shouldn't be saying that the version is wrong, but I'm not focused on that right now). What could possibly be the problem?
Well, where do you use "error$$anonymous$$sg"? Are you sure that you access the right variable? I guess "currentWindow" is an enum which is used to display different "windows" ?
I can't see any obvious error in your code, maybe you can include the part where error$$anonymous$$sg is used?
Like I said, error$$anonymous$$sg is being used in a GUI Label. All it is is:
case Window.Failed: GUILayout.Label(error$$anonymous$$sg); if(GUILayout.Button("Okay")) { currentWindow = Window.Register; } break;
It is obviously in a switch inside the OnGUI function.
Answer by Bunny83 · Sep 10, 2012 at 10:07 PM
Well, i just realized you used String.Compare which returns "an integer that indicates their relative position in the sort order".
It should be 0 when they are equal since than they have the same "level" in the sort order.
You might want to use String.Equals instead and use the StringComparison value of CurrentCultureIgnoreCase for example.
if (code_post.text.Equals("No code", System.StringComparison.CurrentCultureIgnoreCase))
usually it's simpler to just convert the string you want to test to lower-case and do an ordinary string comparison:
var code = code_post.text.ToLower();
if (code == "no code")
//[...]
Keep in mind that your "constants" need to be lower-case
Oh...! I thought that it wasn't equal if it was 0... Thanks. :)
Your answer
Follow this Question
Related Questions
www.data and string conversion to integer 2 Answers
How to convert a string to a type WWW 0 Answers
How do I find a String in an Array? 2 Answers
Tag comparision with a string as variable doesnt work 4 Answers
Getting String From WWW 2 Answers