Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: KingMick on Fri 31/12/2004 16:33:11

Title: If - then and Strings (SOLVED)
Post by: KingMick on Fri 31/12/2004 16:33:11
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.
Title: Re: If - then and Strings
Post by: Radiant on Fri 31/12/2004 17:23:28
Try StrComp, or StrCaseComp.

Title: Re: If - then and Strings
Post by: Hollister Man on Fri 31/12/2004 17:24:28
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 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.
Title: Re: If - then and Strings
Post by: Radiant on Fri 31/12/2004 18:20:05
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) { ... }'
Title: Re: If - then and Strings
Post by: TerranRich on Sat 01/01/2005 18:48:19
Updated BFAQ: http://bfaq.xylot.com/#coding07
Title: Re: If - then and Strings
Post by: KingMick on Tue 04/01/2005 18:00:25
Ah, excellent!  Thank you very much for your help!