strcpy (text string) tutorial

Learn how to make certain types of games and use gameEditor.

strcpy (text string) tutorial

Postby savvy » Sat Apr 23, 2011 7:50 pm

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)
--> For my help, i ask for a simple +1 if it helps! ^-^
--> I dont code, I type art which you dont understand.
--> I keep winning the 3D model challenge at college, teacher says: "you keep winning im not giving you prizes".
User avatar
savvy
 
Posts: 494
Joined: Wed Jun 03, 2009 11:55 am
Location: England
Score: 44 Give a positive score

Re: strcpy (text string) tutorial

Postby SuperSonic » Sat Apr 23, 2011 8:03 pm

So strcpy overwrites text and strcat adds text? :D
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: strcpy (text string) tutorial

Postby lcl » Sat Apr 23, 2011 10:18 pm

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.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: strcpy (text string) tutorial

Postby Game A Gogo » Sat Apr 23, 2011 11:57 pm

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: strcpy (text string) tutorial

Postby lcl » Sun Apr 24, 2011 12:22 pm

That's a good addition to what I said. :)
Actually I didn't even know all that before.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: strcpy (text string) tutorial

Postby Game A Gogo » Sun Apr 24, 2011 7:00 pm

It's quite useful when you want a text to remain the same length with numbers!
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest