Page 1 of 1

Sprintf problem

PostPosted: Sat Oct 09, 2010 4:26 pm
by jimmynewguy
I stumbled into this problem, might be my lack of using strings before. I have this code:
Code: Select all
//animname is declared as: char * animname = "0"; GE crashes if the = "0" isn't there fyi
sprintf(animname, "a%s", getAnimName(animindex));
sprintf(debug.text, "%s", animname);
ChangeAnimation("Event Actor", animname, FORWARD);

With the animations being 1,2,3,4 (up, left, down, right) with attacking animations a1,a2,a3,a4. So I want it to change to the attacking animation according to which animation the player is in. I know I could use ifs or switches but I want to try shorter codes to get things going "simpler" if you catch my drift. Anyways, the debug shows that the animname is always just a instead of a1 or a anything else. If I set the dubug text to sprintf(debug.text, "a%s", getAnimName(animindex)); it shows up right strangely enough.

Thanks in advance! :)

Re: Sprintf problem

PostPosted: Sat Oct 09, 2010 4:37 pm
by Bee-Ant
If you use pointer, don't forget to allocate the memory first...
Code: Select all
char *animname;
animname=malloc(32);

But I suggest you to use array, it's way safer I think...
Code: Select all
char animname[32];

Re: Sprintf problem

PostPosted: Sat Oct 09, 2010 4:50 pm
by jimmynewguy
see? knew it was me not knowing something :P

Thanks Bee

Re: Sprintf problem

PostPosted: Sat Oct 09, 2010 4:57 pm
by Bee-Ant
NP :)

But I wonder...do you really named your animation in numbers 1, 2, 3, and 4 ???
I wonder if you can use number as first letter in GE...