Page 1 of 1

strcpy (text string) tutorial

PostPosted: Sat Apr 23, 2011 7:50 pm
by savvy
strcpy(text, "hello world");

strcpy sets the 1st value, which can either be a text actor (actor.text or event actor as just text), or a variable (string1) to the second value.
the second value can be another string variable, an actor.text OR specified text ("hello world").
examples....

strcpy(string1,"hello"); sets string1 to hello
strcpy(text, string1); sets the event actors text to string1... which if following the above line would make it hello.

strcat(text," world");
strcat sets a second string onto the previous one, for example if text is the actor in the previous examples, which read hello... then the final result from this
function would be 'hello world'.

you could also use this function to name levels, so if you had an actor with the text of 'this level is level ' then you could use...
strcat(text, levelno); to set it as this level is level 1... levelno being a variable.


hope this way faintly helpful....

my lineto tutorial is here http://game-editor.com/forum/viewtopic.php?f=5&t=10414 and is better than this one, mroe descriptive... that will be even more helpful than this....

savvy

(points if helpful please)

Re: strcpy (text string) tutorial

PostPosted: Sat Apr 23, 2011 8:03 pm
by SuperSonic
So strcpy overwrites text and strcat adds text? :D

Re: strcpy (text string) tutorial

PostPosted: Sat Apr 23, 2011 10:18 pm
by lcl
Nice tutorial. :)

But personally I prefer using sprintf();
With it you can add free number of variables into string.
Like:
Code: Select all
sprintf(text, "Lives: %i\nHealth: %i", lives, health);

With that code the current actor would print text with the integer variables lives and health (have to be declared before). And lives and health would be on their own lines, thanks to \n (escape sequence for new line).

Let's say lives = 5 and health = 42.
The text would look like this:

Lives: 5
Health: 42

Variable inputs:
%i (used in this example) inputs integer variable
%d inputs double variable
%s inputs string variable

You can also set integer value to show the value with certain number of digits, like Health: 042.
That would be done by editing the %i to be %03i. (03 for 3 digits) :)

Note that sprintf(); can be used for actors text or any string variable, just as strcpy too.

Re: strcpy (text string) tutorial

PostPosted: Sat Apr 23, 2011 11:57 pm
by Game A Gogo
using %02 will always output a double digit number, so having: sprintf(text,"Lives: %02i\nHealth: %03i",Lives,Health); will output
Lives: 05
Health: 042

you can also just do %number without a 0 to fill with blanks (space)

and having the .number will allow you to cut double/floating variables like so: sprintf(text,"Speed: %03.2d",directional_velocity);
could print out

Speed: 025.12

if the speed is 25.125 :D

Re: strcpy (text string) tutorial

PostPosted: Sun Apr 24, 2011 12:22 pm
by lcl
That's a good addition to what I said. :)
Actually I didn't even know all that before.

Re: strcpy (text string) tutorial

PostPosted: Sun Apr 24, 2011 7:00 pm
by Game A Gogo
It's quite useful when you want a text to remain the same length with numbers!