String/Text help

Non-platform specific questions.

String/Text help

Postby MrB » Sun Dec 05, 2010 11:13 am

OK

I have been trying to write a game where a child types a phrase into a speech bubble, and then presses a button and the speech bubble vanishes and the text they typed appears at the bottom of the page enclosed by speech marks. Anyway I am having a nightmare trying to get the text part to work, all I get is the name of the variable displayed.

Help would be appreciated.


Code: Select all
strcpy(WinnieQuote,"\" &  said Winnie.");
strcpy(OpenQuote,"\"");


strcpy(WinnieSaid, "&OpenQuote &WinnieInput.text &WinnieQuote");


CreateActor("WinnieText", "icon", "(none)", "(none)", -600, 0, false);
strcpy(WinnieText.text, "&WinnieSaid");



DestroyActor("WinnieInput");
DestroyActor("WinnieSpeech");


What I want is this for example:

Child types :- Hello

Output under is :- "Hello" said Winnie.

This must be possible.
MrB
 
Posts: 41
Joined: Sun Jun 14, 2009 8:41 am
Score: 0 Give a positive score

Re: String/Text help

Postby skydereign » Sun Dec 05, 2010 11:21 am

Well I'm not really sure why you have so many lines of code, and actors. Maybe this will help.
Code: Select all
sprintf(outputTextActor.text, "\"%s\" said Winnie", childTypeActor.text);


This sets outputTextActor's text to equal "childTypeActor.text" said Winnie. Of course, it will change childTypeActor.text to the childTypeActor's text, which I assume is how the person inputs the text.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: String/Text help

Postby MrB » Sun Dec 05, 2010 12:47 pm

You are an absolute diamond thanks that worked perfectly.

Just curious, how does that work, can you pick it apart and explain it to me?
MrB
 
Posts: 41
Joined: Sun Jun 14, 2009 8:41 am
Score: 0 Give a positive score

Re: String/Text help

Postby DarkParadox » Sun Dec 05, 2010 1:20 pm

The following was taken from my post here: http://game-editor.com/forum/viewtopic.php?f=1&t=6834
●The sprintf() Function
This function allows you to insert integers, double/real's, and strings into another string 'dynamically'.
In order to insert another variable, you need to know it's type, and use a "specifier" to match that. If you wanted to insert a integer you would type the specifier "%d" or "%i".
Code: Select all
int myvar = 54;
sprintf(text, "The variable myvar equals %d", myvar);
// After the string with specifiers, you list the variables your using in order, according to the order of the specifiers.


The specifiers that can be used are:
  • %d & %i - Integers/double variables.
  • %s - String variables.
  • %c - Single character. (usually you would assign a single character value like so:)
    Code: Select all
    char mychar = 'k';
  • %% - Puts a % into the string.

So, in order, the things that you input into this function are:
Code: Select all
sprintf(text, "my text %d", 4);

The first parameter is output, whats going to display the text, currently it outputs to the current actors text.
The second parameter holds the text, and any specifiers you place.
The third and onward (sprintf is special, there's no limit to the parameters) holds the corresponding variables to the text.
So, if your text was "His name is %s, and he's %d years old", your sprintf would look something like this:
Code: Select all
sprintf(text, "His name is %s, and he's %d years old", "Noah", 14);
// The amount of parameters is only limited by the number of specifiers you use.
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: String/Text help

Postby MrB » Sun Dec 05, 2010 9:07 pm

Thanks everyone who has helped. Another tool for my programming arsenal.

Doe this mean that the following would work, putting 2 text variables in, as there is unlimited parameters.

sprintf(text,"Hello %s you are a %s", nameVar, descriptionVar);
MrB
 
Posts: 41
Joined: Sun Jun 14, 2009 8:41 am
Score: 0 Give a positive score

Re: String/Text help

Postby DarkParadox » Sun Dec 05, 2010 9:09 pm

That would work just perfectly.
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest