Searching and Comparing Strings

Game Editor comments and discussion.

Searching and Comparing Strings

Postby MiG3 » Sat Oct 08, 2005 4:06 pm

Hi again...

I have here some code I tried to translate from another language to this one. It has errors, I don't know what they are or why. I'm hoping you could not only fix the errors, but tell me why they were errors. In detail.

This should take what the player types in, compare it to an array containing other strings, and show the strings that contain the player's string at the beginning. It would show these in the Actor's text.

Keyboard_string is a string added to everytime the player types a key.
Verbs is the array of possible strings.

[i]
i=0;
j=0;

if (keyboard_string!="")
{

while (verbs[i]!="")
{
char * strncpy(char *temp_string, const char *verbs[i], size_t strlen(const char *keyboard_string));

if (char *strncmp(char *keyboard_string, const char *temp_string, size_t strlen(const char *keyboard_string))==1)

{
if (j<=6)
{
strcpy(text,verbs[i]);
strcpy(text,"#"); // I don't know how to put a return symbol, this is what I used before.
j+=1;
}
}
i+=1;
}
}



MiG3
MiG3
 
Posts: 12
Joined: Sat Oct 01, 2005 8:34 pm
Score: 0 Give a positive score

Postby makslane » Sun Oct 09, 2005 10:00 pm

You can use the C strstr function to search strings, like this:

if(strstr("your text", keyboard_string) != 0)
{
//text found
}

There is no strstr function on Game Editor, but you can put the code in the Global code and use:

Code: Select all
char *strstr(char *string, char *substring)
{
    char *a, *b;
   
    b = substring;

    if (*b == 0) return string;
   
    for ( ; *string != 0; string += 1)
    {
      if (*string != *b) continue;
      
      a = string;
      while (1)
      {
         if (*b == 0) return string;
         if (*a++ != *b++) break;
      }

      b = substring;
    }

    return NULL;
}
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby MiG3 » Mon Oct 10, 2005 2:14 pm

Okay, this is my first use of the global code editor but I think I might be getting it. Here's my current problem:

In the global code editor I have your code (I assume there were no changes neccesary):



char *strstr(char *string, char *substring)
{
char *a, *b;

b = substring;

if (*b == 0) return string;

for ( ; *string != 0; string += 1)
{
if (*string != *b) continue;

a = string;
while (1)
{
if (*b == 0) return string;
if (*a++ != *b++) break;
}

b = substring;
}

return NULL;
}


In the draw_event I have the function that calls it:


i=0;
while (verbs[i]!="");
{
int aa=strstr(verbs[i],keyboard_string);

if (aa=="")
{
strcpy(text,verbs[i]);
strcpy(text,"/");
}

i+=1;}


Here is my verb array:

verbs[0]="weld";
verbs[1]="wield";
verbs[2]=""; //signals end of verbs

Anyway. Problem is the game freezes. I'm not sure why. You can still hit escape to exit, though. I'm guessing that's not a step-by-step event. But why doesn't this code work?

Thanks,
MiG3
MiG3
 
Posts: 12
Joined: Sat Oct 01, 2005 8:34 pm
Score: 0 Give a positive score

Postby makslane » Mon Oct 10, 2005 3:16 pm

To copare strings you must use the strcmp function:

if(strcmp(verbs[i], "") == 0)
{
//Same strings
}
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby MiG3 » Mon Oct 10, 2005 7:07 pm

Still not working correctly, so:

Is this correct? It's the draw event code.


i=0;

if (strcmp(keyboard_string,"")==0)
{


while (strcmp(verbs[i], "") == 0 );
{
int aa=strstr(verbs[i],keyboard_string);

if (strcmp(aa,"")==0 )
{
strcpy(text,verbs[i]);

}

i+=1;}

}


and is this, the global code, correct?

char *strstr(char *string, char *substring)
{
char *a, *b;

b = substring;

if (*b == 0) return string;

for ( ; *string != 0; string += 1)
{
if (*string != *b) continue;

a = string;
while (1)
{
if (*b == 0) return string;
if (*a++ != *b++) break;
}

b = substring;
}

return NULL;
}


Dratted C. How I hate it. Thanks for helping though.

MiG3
MiG3
 
Posts: 12
Joined: Sat Oct 01, 2005 8:34 pm
Score: 0 Give a positive score

Postby makslane » Mon Oct 10, 2005 8:21 pm

The strstr code is ok, but I need know the other variables.
Can you send me the ged file?
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby MiG3 » Tue Oct 11, 2005 9:59 pm

I sent the ged.

MiG3
MiG3
 
Posts: 12
Joined: Sat Oct 01, 2005 8:34 pm
Score: 0 Give a positive score

Postby makslane » Wed Oct 12, 2005 1:10 pm

What's the subject?
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby MiG3 » Wed Oct 12, 2005 1:50 pm

It's "Here's the Gmd" :P Sorry I'm used to Game Maker's files. Btw, what does the "d" stand for in gmd and ged?

MiG3
MiG3
 
Posts: 12
Joined: Sat Oct 01, 2005 8:34 pm
Score: 0 Give a positive score

Postby makslane » Wed Oct 12, 2005 2:23 pm

Sorry, I have deleted your email...

If you need execute the code when keyboard_string is not empty use:

if (strcmp(keyboard_string,"") !=0 )
{
...
}

Or

if (strlen(keyboard_string) > 0 ) //test the lenght of string
{
...
}
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Joshua Worth » Thu Oct 13, 2005 3:31 am

MiG3 wrote:Btw, what does the "d" stand for in gmd and ged?
The "d" is following the "e" for "ed" as in "editor"
Stay sweet
User avatar
Joshua Worth
 
Posts: 515
Joined: Tue Jul 19, 2005 12:00 am
Location: Stralia
Score: 5 Give a positive score

Postby willg101 » Thu Oct 13, 2005 7:16 pm

Oh..
I though GED stood for Game Editor Data.
lol oops! :oops:
http://www.wish-games.com
Old betas now available!!
User avatar
willg101
 
Posts: 473
Joined: Thu Dec 16, 2004 12:08 am
Location: West Michigan
Score: 5 Give a positive score

Postby Joshua Worth » Fri Oct 14, 2005 12:08 am

willg101 wrote:Oh..
I though GED stood for Game Editor Data.
lol oops! :oops:
Oh, sorry, maby it does. Try asking makslane.
Stay sweet
User avatar
Joshua Worth
 
Posts: 515
Joined: Tue Jul 19, 2005 12:00 am
Location: Stralia
Score: 5 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest