Textbox Help

Started by Valentine, Wed 16/05/2007 12:30:20

Previous topic - Next topic

Valentine

I'm having trouble with a feature I want in my game.

At one point, the player is required to enter a name for their character. I'm assuming this would be through a Text Box, but I can't figure out how to get the data the player has typed in. I know it's the TextBox.Text property   but I don't know where to put it. I want the name the player has used to show up on the GUI. How do I code this?

Thanks!

Andail

Create the textbox and name it and set an Activate, for instance NAME.Activate.

In the global editor, write

function NAME_Activate(GUIControl *control){
  Game.GlobalStrings[1] =NAME.Text;
}

After this you call the string with for instance

cEgo.Say ("My name is %s.",  Game.GlobalStrings[1]);

Of course, what you call the GUI and the global string is up to you.

Valentine

Thanks for that. I've got it so the character can say his name, but I want the name to be displayed constantly on the GUI. How do I do this? I can put a label on the GUI, but what do I need it to say to get it to display the character name?


Andail

Create a label and the script name to (for instance) "lblshowname"

Then you just set it like

lblshowname.Text = NAME.Text;

Valentine

Wahey! This is working quite well now :D. Thanks for the help Andail!

I do have another question though. I have a GUI that pops up when a certain object is clicked. Currently I have an OK button to close the textbox GUI, but it only works if the player presses enter after typing in the box. Is there a way I can get rid of the button, and have it so the GUI sets the data to what the user has put in, and closes the GUI?

Khris

You could try this:

Add this to your global script:
Code: ags
function repeatedly_execute_always() {
  if (IsKeyPressed(13) && gTextboxGUI.Visible==true)
    gTextboxGUI.Visible=false;
}


This should close the GUI if one presses enter while it is open. Just give it a try.
As before, the text in the textbox can be accessed using textboxname.Text.

Valentine

Well its closing now, but what I type in isn't being 'saved' when I try to display it later.  :-\

Khris

It works for me.
Here's a sample game.

Interact with the bed to bring up the textbox & clear it, press enter to close it again.
The contents of the textbox is displayed in the label.

(rep_ex_always is at the very bottom of the global script.)

Valentine

Ah I got it now! Thanks for the help there. I think I had some stuff mixed up.

Now that I've got the information going into the label, how do I get my character to say his name? I was using:

cEgo.Say ("My name is %s.",  Game.GlobalStrings[1])

Khris

As I've mentioned, use "textboxname.Text" to get the contents where textboxname is the scriptname of your textbox you can set in the GUI editor.

Keep in mind that the GlobalStrings are just standard string variables. Using "Game.GlobalStrings[1]=textbox.Text;" somewhere in the code won't keep the GlobalString updated if the contents of the textbox changes, it will set GlobalString[1] to the contents of the textbox only ONCE.

So use:
Code: ags
cEgo.Say ("My name is %s.",  textboxname.Text);

Andail

Right..don't ask me why I included that globalstring there, it just seemed good at the time :)

Valentine

Thanks to both of you! I've got it working exactly how I wanted now!

One teeny-tiny extra question though, lol. I know this should be really basic, but I've been looking and trying things out for a while now with no luck. I want to deduct 100 from one of my global variables. I'm using:

SetGraphicalVariable("Money", -100);

But obviously this is *setting* it to -100. What's the code I need to use?

Thanks guys.

Akatosh

SetGraphicalVariable("Money", GetGraphicalVariable("Money")-100); should work.

Valentine

Thanks Akatosh, works perfectly.

Unfortunately I have another question (I see a pattern developing...).

Is it possible to add/subtract two variables?

Ashen

You mean something like:
Code: ags

int MyInt = 3;
SetGraphicalVariable("Money", GetGraphicalVariable("Money")-MyInt);
MyInt += AnotherInt;
// ... Etc


Yes, should be possible - have you tried it? If not, in the future TRY before posting any more questions. If so, and it didn't work, what went wrong?
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk