*Please read the last post for my new query.*
Hullo.
How would I go about adding a function into my game that can allow the player their own name? When they choose to start a new game, a text box appears and the player can give themselves a name, and that name is used throughout the game, such as displayed on the status bar or mentioned when they talk to people. I've got a feeling that % can help. Am I right?
Thanks :)
Create a global string, then call
player_name = InputBox("Type your name:");
You can now do stuff like:
Display("%s, you have to go to the dragon cave.", player_name);
Kool thanks. I haven't tried it yet but I'll do it now.
Not much I can add to Khris's post, but check out the manual entries on InputBox, String formatting, and Text Boxes.
~Trent
I've added a global variable (my first :)) and the name is 'player_name', and the type is String. What do I need to do about the initial value? I had a look in the manual like Trent suggested but it has funny examples which I don't understand.
Sorry for not getting it. I would use the excuse that I'm only a beginner but I've had AGS for ages so...
In this case, it won't matter. You'll probably be setting it (with code similar to that above) in the first room, right?
~Trent
Well, the text box is on a 'New Game' GUI but yes it's in the first room.
It's asking me for a string buffer. What's a string buffer? :-\
Did you create a global String variable as Khris suggested?
String player_string;
(Note the capital S in the keyword 'String'.)
Quote from: Atelier on Mon 18/05/2009 20:36:53
It's asking me for a string buffer. What's a string buffer? :-\
Where, and when?
Just do this which is much easier:
When you need to enter the name:
player.Name = Game.InputBox("!Enter your name:");
Then when you have to show the name in speech or a message:
Display("Hello %s", player.Name);
cNpc.Say("Hello %s", player.Name);
If you need to show it on a label:
lblName.Text = String.Format("Player's name: %s", player.Name);
It works! Thanks for all your help guys. (or girls!) :)
Just to finish, how would I create a custom GUI to enter it into?
Like I said above, read on textboxes in the manual, and also custom guis.
Or you could just use the Game.InputBox for now, which creates it for you.
~Trent
PS-Creator, I don't think he wants to cancel this choice ;)
No on second thoughts I'll leave it. The grey's quite nasty but I'll leave it until later. Thanks for all your help on this - my games will go from rubbish to average now! :)
I think now's the time to get my own GUI for the input.
The line in my global script is this:
player.Name = Game.InputBox ("!Type your name. You can't change this later!")
This works fine, but I want to change the cutom box that comes up. I've already got a TextGUI ready for use (GUI id 11), but how would I use this? Do I need to change "Game.InputBox" bit?
Also, will the text box and buttons appear that are on the custom input box? Thanks.
You need to create those yourself.
Create a GUI (setting "pause game when shown"), add a textbox, then put this in the Activate event of said text box:
gNamegui.Visible = false; // turn GUI back off
player.Name = Namebox.Text;
Namebox.Text = ""; // optional, clear text box afterwards
Note that (as always) this code won't work as is. You have to use the names you gave your GUI and textbox.
Cool thanks! I've got it to work now.
Honestly if there wasn't a help forum I'd probably give up. (Eventually :))
I'd also suggest adding a button next to your text box, for the less intuitive players (since txtBox_OnActivate is triggered by the Enter key IIRC).
You can either duplicate the code, or add this line to the button's OnClick
Namebox_OnActivate(control);
which calls the Box's OnActivate function. Just make sure the OnClick function is below the OnActivate function in the script.
~Trent