ITomi wrote:I tried to use the old
getclone() to ask attributes (e.g.: X and Y coordinates) of particular actors, but I couldn't do it. For example this code is not work:
- Code: Select all
int i;
char a_ghost;
i=2;
a_ghost=strcat("ghosts.",i);
sprintf(text,"X place:%d",getclone(a_ghost)->x);
I can realize it only with
getclone2()?
This code's problem is not getclone(). It's that there's a couple of errors in it.
First thing you should notice is that char a_ghost; doesn't define a string pointer (a pointer to an array of chars), but instead a variable that can hold a single character, like 'e' for example. To define a string pointer you have to do this:
- Code: Select all
char *a_ghost; // This would define a pointer to a character or an array of characters
Second thing is that you're using strcat() incorrectly. First off, you can't give strcat() a string literal as the first argument. strcat() will append the second string to the first one, but if the first one is a string literal, it's just as many characters long as the string literal is, meaning that there's no space to add any other characters to. strcat() also returns a pointer to a string (an array of chars), so the variable that you want to assign the returned value to has to also be a char pointer (char *), not a single char.
One last problem is that an actor's x coordinate is not an integer, but a double value, so %d won't work in sprintf. You either have to use %f or to cast the value to integer type first.
Here's the two alternative ways to fix your code:
- Code: Select all
sprintf(text, "X place: %f", getclone("ghosts.2")->x); // Now sprintf is looking for a real variable
sprintf(text, "X place: %d", (int)getclone("ghosts.2")->x); // The (int) part casts the value to integer type so that sprint can deal with it correctly
But if you want to pass the cloneindex number by a variable, I'd suggest you to use sprintf for that:
- Code: Select all
char temp[100]; // An array of chars, 100 chars long
int i = 2;
sprintf(temp, "ghosts.%d", i);
sprintf(text, "X place: %d", (int)getclone(temp)->x);
I hope this cleared some things up for you!