Why it's not working?Or how do i compare two strings?

Game Editor comments and discussion.

Why it's not working?Or how do i compare two strings?

Postby Hammerit » Sat Feb 08, 2014 7:21 pm

I wanted to make hero to move in one direction until you pressing specific key.
Hero can't go right until you pressing left key, and can't go left until you pressing right key.
To done this i created global variable string Xdirection. Then i made code for keys.
Problem is when i push left, hero can't go right, but when i push right hero can go left.

For right key.
Code: Select all
if (strcmp (Xdirection,"left") != 1)
{
    hero.xvelocity = 5;
    strcpy (Xdirection,"right");
}

For left key.
Code: Select all
if (strcmp (Xdirection,"right") != 1)
{
    strcpy (Xdirection , "left");
    hero.xvelocity = -5;
}

On key up.
Code: Select all
hero.xvelocity = 0;
strcpy (Xdirection , "anyway");
Hammerit
 
Posts: 12
Joined: Sun Feb 02, 2014 9:52 pm
Score: 2 Give a positive score

Re: Why it's not working?Or how do i compare two strings?

Postby DarkParadox » Sun Feb 09, 2014 12:40 am

strcmp is an odd beast. Basically what it does is it compares the ASCII values of all the letters, and returns the difference. That means the value returned from strcmp can be greater than 1. When using the function, you should use strcmp(string1, string2) == 0, or the equivalent !strcmp(string1, string2) to indicate equality.

But in your case, none of this matters.
You shouldn't be using strings for states, it's super inefficient and so your game will run slower (four instance, checking "Xdirection" against the string "right", is 5 seperate checks, including math, making it more than 5 times the work for the computer than a normal "integer == 1" check). I'd recommend using an integer variable instead of a string, and using values that you can understand as states. Such as "-1" for left, "0" for standing still, "1" for going right, "-2" and "2" for running, etc.

If you wanted to, you could also go into global code and make some #define statements for placeholders, like...
Code: Select all
#define LEFT 1
#define RIGHT 2
#define STILL 0
#define JUMP 3

// And now in your code you can do...
states_var = LEFT;
// or
states_var = STILL;
// and so on
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: Why it's not working?Or how do i compare two strings?

Postby Hammerit » Sun Feb 09, 2014 7:36 am

DarkParadox wrote:strcmp is an odd beast. Basically what it does is it compares the ASCII values of all the letters, and returns the difference. That means the value returned from strcmp can be greater than 1. When using the function, you should use strcmp(string1, string2) == 0, or the equivalent !strcmp(string1, string2) to indicate equality.

But in your case, none of this matters.
You shouldn't be using strings for states, it's super inefficient and so your game will run slower (four instance, checking "Xdirection" against the string "right", is 5 seperate checks, including math, making it more than 5 times the work for the computer than a normal "integer == 1" check). I'd recommend using an integer variable instead of a string, and using values that you can understand as states. Such as "-1" for left, "0" for standing still, "1" for going right, "-2" and "2" for running, etc.

Yeah i know you can do this with integer and double. I was just curios why strings not working.
And strcmp can return three numbers -1(str1<str2),0(equal),1(st1>str2). It just calculates number of symbols(left = lefo = 0).
Sorry i made simple mistake. In this line it must be 0 not 1, if (strcmp (Xdirection,"left") != 0)
Code: Select all
if (strcmp (Xdirection,"left") != 0)
{
    hero.xvelocity = 5;
    strcpy (Xdirection,"right");
}
Hammerit
 
Posts: 12
Joined: Sun Feb 02, 2014 9:52 pm
Score: 2 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest