Skip to content

Instantly share code, notes, and snippets.

@caligari87
Last active July 23, 2022 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caligari87/ca20c1547c76fb6f1999a2bfa952463f to your computer and use it in GitHub Desktop.
Save caligari87/ca20c1547c76fb6f1999a2bfa952463f to your computer and use it in GitHub Desktop.
Small GZDoom zscript function for checking input buttons
// ----------------------------------------
// the default pointer is "self" for use in a custom player class.
// For an inventory item it would be owner, etc.
// if (KeyCheck(BT_ATTACK, KPT_JUSTPRESSED)) { console.printf("just pressed Attack!"); }
// ----------------------------------------
bool KeyCheck(int toCheck, int checkType = KPT_PRESSING, PlayerPawn pointer = null) {
if (pointer == null) { pointer = PlayerPawn(self); } // default fallback
if (!pointer) { return; } // nullcheck just to be safe
int b = pointer.player.cmd.buttons; int ob = pointer.player.oldbuttons;
switch (checkType) {
case KPT_PRESSING: return (b&toCheck);break;
case KPT_JUSTPRESSED: return ((b&toCheck) && !(ob&toCheck));break;
case KPT_JUSTRELEASED: return (!(b&toCheck) && (ob&toCheck));break;
case KPT_HELD: return ((b&toCheck) && (ob&toCheck));break;
}return false;
}
enum KeyPressTypes {
KPT_PRESSING,
KPT_JUSTPRESSED,
KPT_JUSTRELEASED,
KPT_HELD,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment