Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: steptoe on Thu 15/12/2011 08:50:10

Title: SOLVED: Max characters in a textbox
Post by: steptoe on Thu 15/12/2011 08:50:10
Hi

I'm trying to put a max characters allowed in a textbox by user. (Max length will be 15)

TextBox  name is: tbInput1


if(tbInput1.Text.Chars[tbInput1.Text.Length 15])


I get:

GlobalScript.asc(863): Error (line 863): Parse error in expr near 'tbInput1'


My code at the moment is:


function Button11_OnClick(GUIControl *control, MouseButton button)
{
  if (tbInput1.Text != "")

{
  player.Name = tbInput1.Text;
  gName.Visible=false;
  player.Name = tbInput1.Text;
  Leddie.Text = String.Format("%s", player.Name);
  ggameonfo.Visible=true;
}
 else
 Display("You can't be the Nameless One!");
}


Or could I use a variable of some sort?

Looking for the right solution, can you help?

cheers



Title: Re: Max characters in a textbox
Post by: Gilbert on Thu 15/12/2011 09:43:36
Just do something like:
if(tbInput1.Text.Length > 15) { //blah }

For example, if you want to actively limit the length of the text while the player is inputing something just put the code below in repeatedly execute:
if(tbInput1.Text.Length > 15) tbInput1.Text = tbInput1.Text.Substring(0, 15);
Title: Re: Max characters in a textbox
Post by: steptoe on Thu 15/12/2011 14:48:10
Hi Iceboty

works a treat many thanks.


function repeatedly_execute()
{
  //IF USER TRIES TO TYPES IN TOO MANY CHARACTER
 
  if(tbInput1.Text.Length > 15) {
 
  tbInput1.Text="Maximum 15 characters";
  Wait(100);
  tbInput1.Text="Please enter Name";
  Wait(100);
  tbInput1.Text="";
 
}


Notice I added a bit extra because can't run Say/Display etc because of Rep Exe

cheers

;)


Title: Re: Max characters in a textbox
Post by: monkey0506 on Thu 15/12/2011 14:56:03
You can run blocking commands in repeatedly_execute.
Title: Re: Max characters in a textbox
Post by: steptoe on Thu 15/12/2011 14:57:22
Hi Monkey

if I try to do a display, say or saybackground it hangs.

I know i have Wait.

cheers
Title: Re: Max characters in a textbox
Post by: monkey0506 on Thu 15/12/2011 14:59:38
Sure, if you run Say or Display unconditionally 40 times per second your game is going to hang. But if it's properly encapsulated in a condition that will limit how often the command is run then it works fine.
Title: Re: Max characters in a textbox
Post by: steptoe on Thu 15/12/2011 15:03:19
Hi Monkey

A Say for example will hang until I click the mouse then it goes otherwise it hangs and spacebar does nothing to remove it. I think its not too bad with what I have with display in textbox with no say/display. Its durable.

Can you shed further light on:

Quote
Sure, if you run Say or Display unconditionally 40 times per second your game is going to hang. But if it's properly encapsulated in a condition that will limit how often the command is run then it works fine.

cheers

Title: Re: Max characters in a textbox
Post by: Khris on Thu 15/12/2011 16:12:05
If you put the Display command inside an if block that ISN'T run 40 times per second, using Say is fine.

function repeatedly_execute()
{
  //IF USER TRIES TO TYPE IN TOO MANY CHARACTERS
  if (tbInput1.Text.Length > 15) {
    tbInput1.Text = tbInput1.Text.Substring(0, 15);
    Display("Maximum 15 characters");
  }
}


This should work fine because as soon as the Display command is run, steps are taken to make the outcome of the condition false again. Thus the next Display command isn't run until the player again types in a 16th character.
If you press space to remove the Display box, you might accidentally type a space character into the textbox immediately afterwards which of course again triggers the Display command, and so on. If that's indeed what happens, we have to make sure that the player has let go of any key before we continue.

void WaitForNoKeyPressed() {
  int i = eKeySpace;
  bool wait = true;
  while (wait) {
    wait = false;
    while (i < eKeyDelete) {
      if (IsKeyPressed(i)) wait = true;
      i++;
    }
  }
}


Put this function above repeatedly_execute, then put this line after the Display command:
    WaitForNoKeyPressed();
Title: Re: Max characters in a textbox
Post by: steptoe on Thu 15/12/2011 20:44:10
Hi Khris

cheers, those codes work a treat as well.

I suppose it just beats last coding as it keeps what you have in the textbox.

Display shows until you do a backspace.


   if (tbInput1.Text.Length > 15) {
   Display("Maximum of 15 characters only. [Use backspace to delete letters.");
   WaitForNoKeyPressed();
   tbInput1.Text = tbInput1.Text.Substring(0, 15);
}


I notice that Caps take up more space in a label which could be a problem I need to look at.

thanks for all help on this.

:=

steptoe



Title: Re: SOLVED: Max characters in a textbox
Post by: Khris on Thu 15/12/2011 22:16:15
I've tested this.
My custom Wait function doesn't really work and I'm too lazy to find out why.
The thing is, I don't need it.

Regardless of which key I press to remove the Display message, unless I deliberately hold it down, the message disappears and then the game just sits there, waiting for my input. It works just fine, far from hanging or displaying the message over and over again.
Title: Re: SOLVED: Max characters in a textbox
Post by: steptoe on Thu 15/12/2011 22:32:32
Your are absolutely right.

It works very well.

cheers Khris