Quick way to allow player to customise keys

Started by Kinoko, Tue 08/03/2005 03:50:39

Previous topic - Next topic

Kinoko

My game has some default keys that do certain actions but I'd like to create a screen where the user can define their own. I've been having a little think about it, and the only way I can think of doing it is to have a lot of 'if (keycode==blahblah) keyx=blahblah;" lines... One for each key that could be pushed. Is there another way the engine can simply recognise that a key has been pressed, and to assign whatever that key is to keyx?

The advantage of the first choice is that they can only specify keys I code for, because I have no idea what the implications of someone pressing some really odd key would be otherwise.

Gilbert

Here's an idea. Use on_key_press().
Just use a variable (say, keycustom) to keep track on whether the game is in key customizing mode, something like:

if (keycustom) {//Key customizing mode active
Ã,  if (((keycode>=25)&&(keycode<=60))||((keycode>=80)&&(keycode<=100))){
Ã,  // say, if you only accept keycodes from 25-60 and 80-100 for use, this is completely random as an example
Ã,  // codes for assigning the key follows (maybe you can also check if the key was used by another action
Ã,  Ã,  blah bla bla
Ã,  }
} else {// normal on_key_press responses follow
Ã,  Ã,  blah bla bla
}


Kinoko

That's an excellent idea, thanks once again Gilbot! ^_^

Kinoko

#3
Erm, just a question. If I do that, how can I assign whichever key was pressed to keyx?

Oh, nevermind. keycode, right? >_< Duh. It's just that I hardly ever use it...

RickJ

Ok, I think I see what you want.  Try using an array variable to hold the key translations.  An array variable contains multiple values that are accessed via an index.   So in the example below the keycodes of the actual keys are used as the index and the corresponding array values are the assigned keycodes.   

Hope this is helpful.  If there is something you don't understand just ask.

//===========================================
//  Custom Keycode Assignment 
//-----------------------------------------------------------------------------

// Define the maximum number of keycodes that can be recieved 
// from the keyboard.  Check AGS manual to be sure number is 
// large enough??
#define MAXKEYS 256

// Array that contains key translations.  Actual keycodes are used as the index
// and the assigned keycodes are the values.
int KeyTrans[MAXKEYS];

// This function makes default keycode assignments.  So initially keysare assigned to themselves by default.
function KeyTrans_Init() {
   in i;

   // Make default key assignments
   i = 0;
   while (i<MAXKEYS) {
      KeyTrans = i;
      i++;
   }   
}

// This function assigns a different keycode to a key
function KeyTrans_Assign(int key, int code) {

   // Assign a different code to the specified key
   KeyTrans[key] = code;
}

// This function decodes a key press and returns the assigned codedifferent keycode to a key
function KeyTrans_Code(int key) {

   // Decode key press and return assigned code
   return KeyTrans[key];
}

//===========================================
//  AGS Standard Events
//-----------------------------------------------------------------------------
function game_start() {

   // Make default key assignements
   KeyTrans_Init();  
}

function  on_key_press(int keycode) {
   int keyx;

   // Get key assignment
   keyx = KeyTrans_Code(keycode);
  
   // Decode keyx and do action
   if (keyx==0) {
   }
   else if (keyx==1) {
   }
   :
}

function some_gui_handler() {
   
   // Key assignment GUI handler
   // Gui allows user to specify key and code "somehow?"
   // Then the assignment is made as follows
   KeyTrans_Assign(key,code);
}


SMF spam blocked by CleanTalk