MULTIPLE keys for kb movement

Started by FortressCaulfield, Tue 08/10/2024 23:18:08

Previous topic - Next topic

FortressCaulfield

One of my playtesters reported frustration trying to use WASD. If it's not too complicated I'd like to enable that option WITHOUT having to disable arrow keys OR add a choice for it to the menu.

I'm guessing I need to put something in on_keyboard_press to tell it W is the same as up etc. but can't quite figure out what.
"I can hear you! My ears do more than excrete toxic mucus, you know!"

-Hall of Heroes Docent, Accrual Twist of Fate

Crimson Wizard

#1
You really should post a code that you are using, because it may matter whether you are reacting to keys in on_key_press or in repeatedly_* function using IsKeyPressed.

But in general, if all you want is to treat two keys as same action, then it is solved simply by a combined condition, for example:
Code: ags
if (key == eKeyW || key == eKeyUpArrow)
{
}
or
Code: ags
if (IsKeyPressed(eKeyW) || IsKeyPressed(eKeyUpArrow))
{
}

However, it is worth noting, that WASD is not present as a convenient combo on all keyboards, e.g. French AZERTY keyboards have a different layout.
https://en.wikipedia.org/wiki/AZERTY#/media/File:KB_France.svg

For that reason, it's advised to make reconfigurable key controls in your game.

This may be done e.g. by storing selected key codes in variables like LeftArrowKey, RightArrowKey, and so on, and then use these variables when testing a key in script.

FortressCaulfield

the problem with this is it seems the system sets keys for movement, with only one defined per movement, so there doesn't seem to be anything useful to put after the if statement

I suppose it really is config screen or forget it huh?
"I can hear you! My ears do more than excrete toxic mucus, you know!"

-Hall of Heroes Docent, Accrual Twist of Fate

Snarky

Quote from: FortressCaulfield on Sun 13/10/2024 13:33:09the problem with this is it seems the system sets keys for movement, with only one defined per movement

"The system" doesn't do anything. The code you have in your project does. Somewhere in your project there is a line of code that looks similar to the examples CW gave. If you are using the KeyboardMovement module (which would be useful information to share when asking the question, in that case), it's line 139-157:

Code: ags
  if (IsKeyPressed(keymap.KeyDown))
  {
    apply_direction(direction.Down);
  }
  // etc.

Yes, there is a complication here in that the module is set up to allow you to remap the keys, but not to support multiple keys. If you want to hardcode an alternative mapping where the arrow keys always act this way you can simply do the edit CW proposed:

Code: ags
  if (IsKeyPressed(eKeyDownArrow) || IsKeyPressed(keymap.KeyDown))
  {
    apply_direction(direction.Down);
  }
  // etc.

(With keymap set to WASD by default.)

On the other hand, if you want to support multiple configurable keymappings it gets a little bit more complicated: you could change keymap into an array and use a loop to check all the mappings.

eri0o

Since AGS has no Keyboard movement by default and you can implement your own, just wanted to give plug to my own module because I like it better than whatever comes with the templates, I present you my controlz script module (GitHub repo here). (note if you use it you will have to also disable whatever you are using now because otherwise it won't work well)

If you are using something else though, it would be nice if you could share your code for keyboard movement.

Crimson Wizard

@FortressCaulfield you should really mention which code you are using when asking such questions, or at least which template you started with. If I knew that you did not write any code for keyboard controls yourself, then I could at least guess that you are probably using KeyboardMovement module which comes with Sierra template, and explain how to modify it in my reply.

AGS does not have any built-in movement controls. It also has very limited built-in controls overall (like default inventory clicks handling), provided mainly for backwards compatibility, and these may be disabled in game settings.

For the most part you code all the controls yourself, use the script that comes with template, or adjust that script.

SMF spam blocked by CleanTalk