Page 1 of 1

String/Text help

PostPosted: Sun Dec 05, 2010 11:13 am
by MrB
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.

Re: String/Text help

PostPosted: Sun Dec 05, 2010 11:21 am
by skydereign
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.

Re: String/Text help

PostPosted: Sun Dec 05, 2010 12:47 pm
by MrB
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?

Re: String/Text help

PostPosted: Sun Dec 05, 2010 1:20 pm
by DarkParadox
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.

Re: String/Text help

PostPosted: Sun Dec 05, 2010 9:07 pm
by MrB
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);

Re: String/Text help

PostPosted: Sun Dec 05, 2010 9:09 pm
by DarkParadox
That would work just perfectly.