Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Valentine on Wed 16/05/2007 12:30:20

Title: Textbox Help
Post by: Valentine on Wed 16/05/2007 12:30:20
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!
Title: Re: Textbox Help
Post by: Andail on Wed 16/05/2007 13:00:15
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.
Title: Re: Textbox Help
Post by: Valentine on Wed 16/05/2007 13:16:15
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?

Title: Re: Textbox Help
Post by: Andail on Wed 16/05/2007 13:37:14
Create a label and the script name to (for instance) "lblshowname"

Then you just set it like

lblshowname.Text = NAME.Text;
Title: Re: Textbox Help
Post by: Valentine on Wed 16/05/2007 16:48:34
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?
Title: Re: Textbox Help
Post by: Khris on Wed 16/05/2007 16:55:49
You could try this:

Add this to your global script:
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.
Title: Re: Textbox Help
Post by: Valentine on Wed 16/05/2007 17:02:02
Well its closing now, but what I type in isn't being 'saved' when I try to display it later.  :-\
Title: Re: Textbox Help
Post by: Khris on Wed 16/05/2007 17:22:18
It works for me.
Here's a sample game (http://www.2dadventure.com/ags/default.zip).

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.)
Title: Re: Textbox Help
Post by: Valentine on Wed 16/05/2007 17:44:12
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])
Title: Re: Textbox Help
Post by: Khris on Wed 16/05/2007 18:38:16
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:
cEgo.Say ("My name is %s.",  textboxname.Text);
Title: Re: Textbox Help
Post by: Andail on Wed 16/05/2007 20:25:14
Right..don't ask me why I included that globalstring there, it just seemed good at the time :)
Title: Re: Textbox Help
Post by: Valentine on Thu 17/05/2007 14:46:17
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.
Title: Re: Textbox Help
Post by: Akatosh on Thu 17/05/2007 14:58:25
SetGraphicalVariable("Money", GetGraphicalVariable("Money")-100); should work.
Title: Re: Textbox Help
Post by: Valentine on Thu 17/05/2007 17:50:58
Thanks Akatosh, works perfectly.

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

Is it possible to add/subtract two variables?
Title: Re: Textbox Help
Post by: Ashen on Thu 17/05/2007 17:58:06
You mean something like:

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?