I need a code for input box. (Solved)

Started by Meystari F, Wed 15/01/2020 23:46:54

Previous topic - Next topic

Meystari F

I am now trying to write a code for player, when he needs to pick a 6 numbers.

When that happens a  game.inputbox appear where he needs to write numbers.

The player must insert only a numbers in that text box from 1-999 in that ,not letters.

What is the code to do that,  if I want player write only a numbers and avoid letters in that game text box?

Let say if the player write letters instead for numbers, I want to display 
Code: ags
Display"Sorry, you must only write numbers";


Help!

Khris

You can't stop the player from typing other characters into the box, but you can keep asking them (i.e. use a while loop).

Code: ags
  bool done = false;
  String input;
  do {
    input = Game.InputBox("Enter a number (1-999):");
    done = true; // assume input checks out for now
    // numeric chars only
    for (int i = 0; i < input.length; i++) if (input.Chars[i] < eKey0 || input.Chars[i] > eKey9) done = false;
    // length: 1-3
    if (input.length == 0 || input.length > 3) done = false;
    if (!done) Display("Sorry, you must only enter a number from 1-999");
  } while (!done);
  int number = input.AsInt;


It's also possible to create that input box from scratch using on_key_press, a GUI and a label (or a drawn string). In that case you can choose not to react to any keys other than 0-9.

Laura Hunt

Quote from: Khris on Thu 16/01/2020 10:13:11
You can't stop the player from typing other characters into the box

Actually you can, using this code that you shared yourself a while back ;)

https://www.adventuregamestudio.co.uk/forums/index.php?topic=41543.msg549505#msg549505

I seem to recall that I tried it out and it worked, although with the caveat you mention in your post that if you type something that's not a number, it'll appear for a brief moment before getting removed.

Cassiebsg

Quote
it'll appear for a brief moment before getting removed.

Have you tried placing the code in late_repeatedly_execute_always ? Should solve that glitch.  ;)

@Fribbi
A suggestion: You could also create a GUI with the buttons, and simulate the keypad (if it's a keypad) the player needs to use. That way you avoid typed text boxes, and make your game more friendly to mobil platforms. And the player might appreciate a keypad more rather than needing to enter text in the keyboard.


There are those who believe that life here began out there...

Laura Hunt

Quote from: Cassiebsg on Thu 16/01/2020 11:21:06
Quote
it'll appear for a brief moment before getting removed.

Have you tried placing the code in late_repeatedly_execute_always ? Should solve that glitch.  ;)

I don't think I even knew that late_rep_ex existed then (laugh) Sounds like it should work though! But I doubt that I'll go back to tinkering with that game's code...


Khris

Right, I forgot about using a GUI Textbox. I was talking about  Game.InputBox  however, which cannot be controlled by code while it's open, afaik.

Crimson Wizard

Quote from: Khris on Thu 16/01/2020 13:07:53
Right, I forgot about using a GUI Textbox. I was talking about  Game.InputBox  however, which cannot be controlled by code while it's open, afaik.

Yes, Game.InputBox creates an old built-in variant that is fully controlled by the engine, and cannot be accessed by script.

All those built-in dialogs today are meant only for quick prototyping; normally it's better to create your own GUI.

Laura Hunt

Quote from: Khris on Thu 16/01/2020 13:07:53
Right, I forgot about using a GUI Textbox. I was talking about  Game.InputBox  however, which cannot be controlled by code while it's open, afaik.

Ohhh ok my bad then! I believe I've never used Game.InputBox so I was getting them mixed up, thanks for clarifying! :)

Khris

Try this:

Code: ags
int numbers[6];
int pick = 0;
 
function TNumbers_OnActivate(GUIControl *control)
{
  String input = TNumbers.Text;
  int t = input.AsInt;
  if (t > 0 && t < 1000) {
    numbers[pick] = t;
    pick++;
    if (pick == 6) {
      gPicks.Visible = false; // we're done, turn off GUI
      // do something with numbers[...]
    }
    else {
      // move to next number; update prompt and clear text field
      Picks.Text = String.Format("Pick #%d", pick + 1);
      TNumbers.Text = "";
    }
  }
  else Display("Please enter a number from 1-999");
}

Meystari F

#9
Khris you are the coolest and the smartest guy I know from AGS forums.
But that code worked perfectly.
Thank  you!

I also hope any others game designers  will find this code useful too for their own game. :)

Khris


Meystari F

#11




Hi again Khris.

In last time you helped me with the code how to pick a random 6 numbers.
Right now I need to know how to pick the right 6 numbers.

The right 6 numbers are random numbers.

What I have done so far is this.

Code: ags
function cReceptionist_UseInv()
{

if (Region.GetAtRoomXY(player.x, player.y) == region[2]) {
if (cLarry.ActiveInventory == iLottoticket){
gDisplay.SetPosition(25, 55);
gDisplay.Visible = true;
BPicture.NormalGraphic = 11;

 LTexti.Text = "\"Say,\" you ask the receptionist, \"is this lottery ticket any good?\"";
}
}
}


Code: ags
else if (cLarry.room == 17){
  int tala1=Random(999);
   int tala2=Random(999);
    int tala3=Random(999);
     int tala4=Random(999);
      int tala5=Random(999);
       int tala6=Random(999);
gDisplay.Visible = false;
DisplayAt(63, 50, 210,  "\"I don't know,\" she replies, \"I've misplaced my glasses! As best I can remember, this week's Lucky Life[Lottery Luck-O Buck-O numbers[are: %d,%d,%d,%d,%d, and %d.[What six numbers do you have?\"",tala1, tala2, tala3, tala4, tala5, tala6);
gNumberPicks.Visible = true;


Code: ags

else if (cLarry.room== 17){
  
   int tala1=Random(999);
   int tala2=Random(999);
    int tala3=Random(999);
     int tala4=Random(999);
      int tala5=Random(999);
       int tala6=Random(999);
  String input = TNumbers.Text;
  int t = input.AsInt;
  if (t > 0 && t < 1000) {
    numbers[pick] = t;
    pick++;
    if (pick == 6) {


 gNumberPicks.Visible = false;
      gLottoMachine.SetPosition(90, 75);
      gLottoMachine.Visible =false;


if (tala1 && tala2 && tala3 && tala4 && tala5 && tala6== Random(999)){

   
   Display("Right");   //If all numbers was correctly picked. 
   }
  
  

 else{

      Display("Wrong."); //If the player typed wrong number.
         
      gNumberPicks.Visible = false;
      gLottoMachine.SetPosition(90, 75);
      gLottoMachine.Visible =false;
 
 }
 

      
      
      
    }
    else {
      Picks.Text = String.Format("Pick #%d", pick + 1);
      TNumbers.Text = "";
    }
 }


  
  else Display("Please enter a number from 1-999");


}
}




The problem is  the message Yes appears  only when I pick both right number and wrong number.

Khris or anyone, please I help with this.



Khris

You need to compare the random numbers to the user's pick stored in  numbers.

Top of global script (above game_start)
Code: ags
int tala[6];


Inside game_start:
Code: ags
  for (int i = 0; i < 6; i++) tala[i] = Random(998) + 1; // 1 - 999, pick once for entire game, not new numbers each time


Inside cReceptionist_UseInv
Code: ags
  // "using lottoticket in room 17" block
  DisplayAt(63, 50, 210,  "\"I don't know,\" she replies, \"I've misplaced my glasses! As best I can remember, this week's Lucky Life[Lottery Luck-O Buck-O numbers[are: %d,%d,%d,%d,%d, and %d.[What six numbers do you have?\"", tala[0], tala[1], tala[2], tala[3], tala[4], tala[5]);

  // turn on GUI with textbox
  gNumbers.Visible = true;
  return;  // leave UseInv function


in TNumbers_OnActivate, where it says  // do something with numbers[...]
Code: ags
  bool match = true;
  for (int i = 0; i < 6; i++) if (numbers[i] != tala[i]) match = false;

  if (match) {
    // ...
  }
  else {
    // ...
  }

Meystari F

Thanks so much again for this Khris. I knew I could count on you again.

And please stay safe and healthy from that evil Corona virus. You are one of the most useful smartest man and a true legend on AGS Forums.

Amen!  I promise I will mention you in the credit list as my thanks to you in my game when I finally have completed it.

SMF spam blocked by CleanTalk