3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1]

Author Topic: How to make a simple keyboard Menu in AngelScript  (Read 2346 times)

jestermon

« on: August 19, 2010, 11:46:09 AM »
I have been asked by a few users to make an example of a simple menu
that works with angelscript, that uses the keyboard to move through the
menus, and then does some action when the ENTER key is pressed.

It is currently set up for 3 menus, but can easily be changed to as many
menu sprites as you need. You will need 2 sprites for every menu option.
One for the normal menu texture, and one for highlighted texture.

Enjoy.


Code: [Select]
///=========================================================================
/// A Simple menu in AngelScript
///
/// This project requires 6 sprites. One for each of the 3 menus, and one
/// for each of the highlighted menun options. As the currentMenu variable
/// points to a specific menu, a highlighted menu sprite will be displayed
/// instead of the normal menu spite.
///
/// Use the up and down arrows to change the value of the currentMenu
/// variable. 2 Arrays are used to store the OBJ values of the sprites. One
/// array for normal menu sprites, and one array for the highlighted menu
/// sprites.
///
/// For this demo, the normal menu sprites are set to 0.6 opacity, and the
/// highlight menu sprites are left at normal opacity. This is so that you
/// can see which menu item is highlighted  when the currentMenu variable
/// focuses on a spexific sprite. In a real game, you will use different
/// textures for the normal and highligted menu sprites, so wont need to set
/// the opacity of the sprites.
///
/// Place the highlight menu sprite and the normal menu sprite for a
/// specific menu in the same location, so that when their visibility is
/// changed, they appear in the same place on the screen
///=========================================================================


///Create an array for the normal menu sprites
int [] MENU_NORMAL (3);

///Create an array for the highlighted menu sprites
int [] MENU_HIGHLIGHT (3);

///Set the maximum number of msnus
int MAX_MENUS = 3;

///Set up a variable to store the current sprite number
///AngelScript counts array entries from 0
int currentMenu =0;  //default to top menu =  (0 to 2)

///Set up some variables to be used in controlling the
///delay between jumping to the next menu when the up
///down key is held in
int menuTimer = 0;
int menuDelay = 20;  ///Numver of frames to delay keys


///
///---This function sets the OBJ values to the MENU arrays----------------
///
void setupMenus()
{
   //Store the normal menu sprite OBJ values
   MENU_NORMAL[0] = OBJ_66;
   MENU_NORMAL[1] = OBJ_0;
   MENU_NORMAL[2] = OBJ_110;

   //Store the highlight menu sprite OBJ values
   MENU_HIGHLIGHT[0] = OBJ_44;
   MENU_HIGHLIGHT[1] = OBJ_22;
   MENU_HIGHLIGHT[2] = OBJ_132;
}

///
///---Thsi function shows the currently selected menu
///
void showMenu()
{
   int i;
   for(i=0;i<MAX_MENUS;i++){
      if(i != currentMenu){
         iObjectHide(MENU_HIGHLIGHT[i]);
      }
      if(i == currentMenu){
         iObjectShow(MENU_HIGHLIGHT[i]);
      }
      if(i != currentMenu){
         iObjectShow(MENU_NORMAL[i]);
      }
      if(i == currentMenu){
         iObjectHide(MENU_NORMAL[i]);
      }
   }
}


///
///---The Main function--------------------------------------------------
///
void Main()
{
   ///The initializing block runs only when the scrips is started
   if(iInitializing()){
      ///call a function to store the menu sprite OBJ values
      setupMenus();
      ///Call a function to show the current menu
      showMenu();

   }


   ///If the menu timer has not reached the menu delay timer then
   ///increace the value if the menu timer
   if(menuTimer < menuDelay)
      menuTimer++;

   ///Only if the menu timer has run out, then check which key
   ///the user is pressing, and respond to it. If the timer is
   ///not ready, then ignore any keys that are pressed.
   if(menuTimer == menuDelay){

      ///If the user presses the UP key, then decrease the value
      if (iKeyDown(iKeyCode("DIK_UP"))){
         if(currentMenu > 0){
            currentMenu--;
            ///Call a function to show the current menu
            showMenu();
            ///Reset the menu timer, to start the delay count
            menuTimer=0;
         }
      }

      ///If the user presses the DOWN key, then increase the value
      if (iKeyDown(iKeyCode("DIK_DOWN"))){
         if(currentMenu < MAX_MENUS-1){
            currentMenu++;
            ///Call a function to show the current menu
            showMenu();
            ///Reset the menu timer, to start the delay count
            menuTimer=0;
         }
      }

      ///If the ENTER key is pressed then process the current menu
      if (iKeyDown(iKeyCode("DIK_RETURN"))){
         ///Delay with ENTER as well, so you dont start the
         ///action on every game frame
         menuTimer=0;
         switch(currentMenu){
            case 0:{
                 //Menu0 - do something
                 }
            case 1:{
                 //Menu1 - do something
                 }
            case 2:{
                 //Menu2 - do something
                 }
         }
      }
   }

}

psikotropico

« Reply #1 on: August 19, 2010, 12:23:52 PM »
nice script... viva las arrays!!! ;D

jestermon

« Reply #2 on: August 19, 2010, 10:04:41 PM »
nice script... viva las arrays!!! ;D
Thanks.. Arrays are very useful for working with multiple items, can't do without them. :)
« Reply #3 on: September 09, 2010, 09:48:18 AM »
I've used your plug-in. No doubt good staff  :D.
But there was a glitch.
I use Exitfade, for example, in case 2 in the script and Exitfade works in the 3 cases though other
staff also works in appropriate case.
That is whichever case (button) you switch you have the same result - first a staff in a current
case, then inevitable Exitcase.
So I looked in AngelScript guide by Darryl Hunt and found that after all commands in a current case I
must set 'break' command to prevent working Exitfade in all cases.
Perhaps somebody who wants to use the plug-in should consider it.

jestermon

« Reply #4 on: September 09, 2010, 11:55:52 AM »
The Switch case is just a simple example in the script. The script concentrated mainly on the objective of making a menu, and left the management of the switch case for the user to do. But you are correct; you need a break with each case statement, else all of the case's are seen as one condition. Sometimes little oversights like this can confuse new users. Thanks for pointing this out Alec.

« Reply #5 on: September 10, 2012, 04:04:29 AM »
      ///If the ENTER key is pressed then process the current menu
      if (iKeyDown(iKeyCode("DIK_RETURN"))){
         ///Delay with ENTER as well, so you dont start the
         ///action on every game frame
         menuTimer=0;
         switch(currentMenu){
            case 0:{
                 //Menu0 - do something
                 }
            case 1:{
                 //Menu1 - do something
                 }
            case 2:{
                 //Menu2 - do something
                 }
         }


I still don't get it ..what must i put to go to other project..
for example if 1 press enter in menu 1 ,it will load to other project using ExitFade..
can help me with that script
Just make me pround :D
« Reply #6 on: September 11, 2012, 06:07:00 AM »
take a look here Nokami... http://www.3drad.com/forum/index.php?topic=8842.0  this may help...

--Mike
« Reply #7 on: September 12, 2012, 11:27:26 PM »
Thanx you mike  :D
Just make me pround :D
Pages: [1]