Page 1 of 1

How to set text under certain conditions

PostPosted: Tue Aug 14, 2012 1:46 am
by happyjustbecause
I've been using a pretty inefficient space wasting method for text in my game, I've just been making images with text rather than having a font in my data and setting text in gameEditor. I've got a large amount of text in my pause menu, and I would like to change it from an image to in game text if you know what I mean.

But for something like my upgrades menu, I have it so that if the crosshairs or mouse hover over the "button" to purchase the upgrade, text pops up. But you can switch the menu to be hosting different upgrades. So basically I need to be able to have if statements or switch statements that set the text to be a certain text depending on what menu you are at.

Do I need to have 4 different actors for the text? Or is there some way to set text in script editor? Basically what I'm asking is can text me set under certain conditions like an if statement permits, kind of like:

Code: Select all
if(Upgrades_state==2)
{
    setText(Bomb Text)
}

Re: How to set text under certain conditions

PostPosted: Tue Aug 14, 2012 1:52 am
by SuperSonic
You could create one text actor and for it's draw event, you could put this:
Code: Select all
switch(Upgrades_state)
{
    case 2:
        setText(BombText);
        break;

    case etc:
        ....
}
:D

EDIT*
HappyJustBecause wrote:Basically what I'm asking is can text me set under certain conditions like an if statement permits, kind of like:
Yeah, you should be able to do that :)

Re: How to set text under certain conditions

PostPosted: Tue Aug 14, 2012 1:53 am
by Jagmaster
I know I'll get beat to this, haha

Sprintf and strcpy are used to write strings, like text.

http://game-editor.com/Sprintf
http://cplusplus.com/reference/clibrary/cstdio/sprintf/
http://cplusplus.com/reference/clibrary/cstring/strcpy/

:)

Edit: yep, and use the switch like SS along with those functions :lol: I get beat all the time xD

Re: How to set text under certain conditions

PostPosted: Tue Aug 14, 2012 2:58 am
by happyjustbecause
Thank you Supersonic and Jagmaster! I overlooked the links you(Jag) sent, but I don't quite know how to set the text still... I'm not sure how to actually write just text for this:

Code: Select all
sprintf(text, "A string: %s\nAn integer: %d\nA float value: %f", "Hello!", 7, 3.4);


I thought to just put:

Code: Select all
sprintf(text, "A string: %s\nAn", "Hello!");


But I'm sure that's not correct, can you(someone) please show me an example of how to input text?

Re: How to set text under certain conditions

PostPosted: Tue Aug 14, 2012 3:32 am
by Jagmaster
Sorry, I knew I should of briefed a bit more on those :? Anyway,

The difference between sprintf and strcpy is, the sprintf function can insert other variables into the string (text). Strcpy just copies one string value into annother string.
For instance:
Code: Select all
strcpy(text, "This is a whole bunch of useless text");

Text would be the variable that displays the text in the text actor, and the stuff in the quotes is the stuff we're coping into it. If you had 2 string variables that you simply wanted to copy from one to the other, you'd leave off the quotes. It is a little simpler, but not nearly as versatile as sprintf.

The sprintf has a lot of neat escape sequences that you can use to drop an actual variable into the string.
For instance:
Code: Select all
sprintf(text, "Score: %d", scorevariable);
that would display the score and the "Score:" in the same line of text. The %d is the integer escape sequence, the others are listed on the ge wiki page.

Another thing you might need would be "\n" which, when inserted, makes a new line. there are others that use the forward slash like that, but I forgot what they are, and not all of them worked in ge (I think like \t for tab or something like that).

I've had problems with text going out of the 255 character limit before, so watch out. I think I was passing the text through a string variable though, not sure it was a while ago. :lol: I'll double check to see if that's an issue.

Re: How to set text under certain conditions

PostPosted: Tue Aug 14, 2012 6:32 am
by happyjustbecause
Oh thank you very much Jagmaster! I can use both the strcpy and sprintf for useful purposes! I feel like I'm back in a tutorial for the beginning of coding, seeing something like:

"This is a whole bunch of useless text"

:D

I'll begin swapping out images of text for actual text immediately. One last question (for now), so there's a 255 character limit? Is there any way to get around this? I don't need it for the thing I currently want to do, but I might want to do something with more than that.

Re: How to set text under certain conditions

PostPosted: Tue Aug 14, 2012 6:57 am
by happyjustbecause
Also, I've been trying to do use the "\n" for a new line in the strcpy, but I can't figure out how to use it, when I try to I keep getting a close parenthesis error.

This is my attempt

strcpy(text, "Obsolete text"\n"New Line of Useless Text");

Re: How to set text under certain conditions

PostPosted: Tue Aug 14, 2012 7:15 am
by MrJolteon
happyjustbecause wrote:Also, I've been trying to do use the "\n" for a new line in the strcpy, but I can't figure out how to use it, when I try to I keep getting a close parenthesis error.

This is my attempt

strcpy(text, "Obsolete text"\n"New Line of Useless Text");

It's
Code: Select all
strcpy(text, "Obsolete text\nNew Line of Useless Text");

Re: How to set text under certain conditions

PostPosted: Tue Aug 14, 2012 4:53 pm
by happyjustbecause
Oh okay, I thought it didn't need the quotes, that they were just there so you know that it is only \n, but I didn't try it for some reason. Thank you!

Re: How to set text under certain conditions

PostPosted: Tue Aug 14, 2012 7:36 pm
by Jagmaster
Just confirming the 256 char limit, which is rather annoying if have long text. One fix is to split the text into 256 char chunks and cycle through them in a keydown event. May or may not work for what you're doing.

Oddly, you can make the the startup text be as long as you want. I don't get that. :|

Re: How to set text under certain conditions

PostPosted: Tue Aug 14, 2012 8:53 pm
by skydereign
The reason for that is that the startup text is predetermined in size. It can know how large to allocate the text (in fact it might just store startup text elsewhere). Since it already knows the size, the game won't have any problems. But, when you change it in game, it is a set size, and since you can't have an infinitely sized string in C, you run into the limit. What might be good is to have the text be dynamically allocated, and therefore we can use realloc. Though I believe the reason makslane didn't do that, is that with a set limit, the user is less likely to create weird crashes, and everyone experiences the same limit (easier to solve).