If - then and Strings (SOLVED)

Started by KingMick, Fri 31/12/2004 16:33:11

Previous topic - Next topic

KingMick

I tried to make an if/then statement in a game where I wanted to see if a particular GlobalString held a particular value.  I know that it did hold this value because I entered it myself and even had the program display the GlobalString to test it.  My code was similar to:

string bufstring;
GetGlobalString(1, bufstring);
if (bufstring == "1") {
  // various commands
}

However, this if-then statement continued to return as false even though I had entered "1" in a TextBox, had the TextBox text stored to the GlobalString, and even had the game Display the contents of the GlobalString to test it (I got a "1" in the display box, so I know the storing worked).  I know that the value went correctly to bufstring too, because I had the program Display the bufstring.  Yet, the if statement continually returned as false (i.e. the commands within the statement did not happen) and I am trying to figure out why.

I already fixed this particular game by using StringToInt to convert to an integer and using the if-then on the integer instead, and that worked.  However, I am trying to figure out how to solve this problem for future projects where I will want to use an if-then on a string value that cannot be converted to an integer.

I hope that was easy to understand.  I have a difficult time describing these things sometimes.  Can anyone tell me what the problem was with my code?  I am thoroughly confused.

Radiant

Try StrComp, or StrCaseComp.


Hollister Man

I'm surprised you didn't get a 'type mismatch' error.  bufstring is a string, and == is a math operator.  You need to do something like this
Code: ags
 string bufstring;
GetGlobalString(1, bufstring);
if(StrComp(bufstring, "1") {
// various commands
}


StrComp is String Compare.  It compares a string with a string, rather than math.
That's like looking through a microscope at a bacterial culture and seeing a THOUSAND DANCING HAMSTERS!

Your whole planet is gonna blow up!  Your whole DAMN planet...

Radiant

Oh yes - and StrComp returns the number of mismatches between the strings, or the index of the first mismatch, or something like that. Anyway the point is that if the strings are identical, StrComp returns 0. So common syntax is 'if (StrComp (foo, "bar") == 0) { ... }'

TerranRich

#4
Status: Trying to come up with some ideas...

KingMick

Ah, excellent!  Thank you very much for your help!

SMF spam blocked by CleanTalk