Skip to content

gameKeyEvents

Quick overview

Author: mud-freak
Platform: G1, G2NotR
Category: Engine, Keys

gameKeyEvents.d is a script, which handles key events with the oCGame::HandleEvent hook. Better alternative for FrameFunction with MEM_KeyState with which you don't have to check whether any menu is opened or player is in dialogue or can move etc.

Author's description

I looked up the address within oCGame::HandleEvent. I made it into a universally usable script for Gothic 1 and Gothic 2.

One could argue now that this is not much different from a FrameFunction with MEM_KeyState. The difference is that this approach saves the extra work of checking if any menu is open, whether the player is in a dialog, whether the player may move, etc. Also this function is "event driven", meaning it is really only called when a key is pressed/held instead of every frame in vain. So it's arguably more performant.

Dependencies

Initialization

Call Game_KeyEventInit() in the Init_Global() or other initialization function.

Game_KeyEventInit();

Implementation

gameKeyEvents.d on WoG forum

Usage

To add a key pressing detection edit the main function Game_KeyEvent.

1
2
3
4
5
6
7
func int Game_KeyEvent(var int key, var int pressed) {
    if (key == KEY_LBRACKET) && (pressed) {
        // Here enter your code.
        return TRUE;
    };
    return FALSE;
};
  • To change detected key rename KEY_LBRACKET to your own key e.g. taken from Ikarus constants.
  • To detect pressing a key leave (pressed) unchanged but if you want to detect holding change it to (!pressed). That's because

    pressed is FALSE: key is held, pressed is TRUE: key press onset

  • To run code when a key is pressed, paste it or call a function where the comment is.
  • To detect more than one key add else if.