[SOLVED] Choose player character name at the start of the game

Started by Diegolomac, Thu 07/06/2018 06:19:04

Previous topic - Next topic

Diegolomac

Hey, so, I need to make the player type in a name once the game starts, and that name is going to be used by all the other characters when they talk directly to the protagonist. Now, I know the basic idea of what I need to do (start in a room with a GUI, program the "OK" button to send what was typed in the text box to a global string variable, change the room to the actual room I want the story to start in, and that variable will be used in the middle of dialogues). Now, MAKING that happen in itself is being the problem.

I tried making my starting room be room 20, which has nothing:

Code: ags

function room_FirstLoad()
{
  gNomeProt.Visible = true;
} 


And then in the global script I put this:

Code: ags

function Button1_OnClick(GUIControl *control, MouseButton button)
{
  playername = TextBox1.Text;
  player.ChangeRoom(1, 1000, 570, eDirectionLeft);
}


But the game instantly crashes when I try to run it. Everything was running smoothly before I put this in.

Any help?

Slasher

You could use something like this

Code: ags


function Button1_OnClick(GUIControl *control, MouseButton button)
{
if (TextBox1.Text != "") // 
 
 {
 player.Name = TextBox1.Text;   //name you typed in for the player
 gNomeProt.Visible=false;
 player.Say("Hello. My name is %s.",player.Name); // %s returns a string and player.Name is the string which is the name you typed in.
 player.ChangeRoom(1, 1000, 570, eDirectionLeft);
}
}


All you need to do is add the %s and player.Name to labels/speech to display the name of the character typed in.

Snarky

Does your Room 20 have a background? It needs to.

Another approach is instead of using a separate room, just make a GUI that covers the whole screen.

Khris

Usually, when a game crashes, you're presented with an error message... why not go ahead and paste that into your post, verbatim?

Diegolomac

The problem was the lack of a background :tongue:

As soon as I put one in it started working. I also implemented the code you guys suggestes, I hadn't even thought about what would happen if the player didn't type anything. Thanks.

SMF spam blocked by CleanTalk