Keyboard input script issues (Solved more or less)

Started by Guineapiggy, Mon 19/11/2007 08:36:17

Previous topic - Next topic

Guineapiggy

Okay, so I've generated a script that allows for keyboard movement of the character with an acceleration and deceleration instead of the sudden stop and it works nicely save I've yet to link it to animation. (All in good time).

I also added a function that allows the user to sprint or proceed cautiously by holding either the shift or ctrl key at the same time as walking. Now... this works to an extent, if you hold either key the character will indeed speed up or slow down but curiously... frustratingly it also seems to randomly freeze the character in their tracks and they don't move again until I reload the game. I've tried everything I can think of but I still can't find what I might be doing wrong, part of me thinks it may be a bug with the AGS 3 beta I'm currently using.

Anyways, here's the script.

Note - I'd love to do it a more simple way than the rather hacked-togehter move X method but not being able to change character walk speeds whilst in motion is a royal pain. Why is this the case?

Quote//****************************************************************************************************
// VARIABLES
//****************************************************************************************************

// keycodes as variables for future key customization functions (static variables?):
int KeyboardMovement_KeyLeft = 375; // left arrow
int KeyboardMovement_KeyRight = 377; // right arrow
int walkloop;
int walkaccel = 1;

KeyboardMovement_Modes KeyboardMovement_Mode = eKeyboardMovement_None; // stores current keyboard control mode (disabled by default)

KeyboardMovement_Directions KeyboardMovement_CurrentDirection = eKeyboardMovement_Stop; // stores current walking direction of player character

//****************************************************************************************************
// USER FUNCTIONS
//****************************************************************************************************

//====================================================================================================

static function KeyboardMovement::SetMode(KeyboardMovement_Modes mode) {
   KeyboardMovement_Mode = mode;
}

//====================================================================================================

// key customization functions here

//====================================================================================================

//****************************************************************************************************
// EVENT HANDLER FUNCTIONS
//****************************************************************************************************

//====================================================================================================

function repeatedly_execute() {

   //--------------------------------------------------
   // Pressing mode
   //--------------------------------------------------
  int WlkSpd = 2;
  if (IsKeyPressed(403)) {
  WlkSpd = 1;
  }
  else if (IsKeyPressed(405)) {
  WlkSpd = 3;
  }

  if (walkloop > 0 && walkloop <= 20){
    walkaccel = walkaccel + 1;
    if (walkaccel == (WlkSpd + 1)) {
    player.x = player.x + 1;
    walkaccel = 0;
    }
   }
  else if (walkloop >= 20) {
    walkaccel = walkaccel + 1;
    if (walkaccel == (WlkSpd + 1)) {
    player.x = player.x + 2;
    walkaccel = 0;
    }
   }
   if (walkloop < 0 && walkloop >= -20){
    walkaccel = walkaccel + 1;
    if (walkaccel == (WlkSpd + 1)) {
    player.x = player.x - 1;
    walkaccel = 0;
    }
   }
  else if (walkloop <= -20) {
    walkaccel = walkaccel + 1;
    if (walkaccel == (WlkSpd + 1)) {
    player.x = player.x - 2;
    walkaccel = 0;
    }
   }
   if ((IsGamePaused() == true) || (KeyboardMovement_Mode != eKeyboardMovement_Pressing) || (IsInterfaceEnabled() == false) || (player.on == false)) return 0;
     // if game is paused, module or mode disabled, interface disabled or player character hidden, quit function

   KeyboardMovement_Directions newdirection; // declare variable storing new direction

   // get new direction:
   if (IsKeyPressed(KeyboardMovement_KeyLeft)) newdirection = eKeyboardMovement_Left; // left arrow
   else if (IsKeyPressed(KeyboardMovement_KeyRight)) newdirection = eKeyboardMovement_Right; // right arrow
   else {
    if (walkloop > 1) {
      walkloop = walkloop - 1;
     }
    else if (walkloop <= 1 && walkloop >= -1){
      walkloop = 0;
      }
    else if (walkloop < 1) {
      walkloop = walkloop + 1;
     }
     }
      if (newdirection == eKeyboardMovement_Left) {
         
          if (walkloop > -40) { //Basic build-up of walk speed
            walkloop = walkloop - 1;
            }
          if (walkloop > 0) { //Makes turning or suddenly stopping a little sharper
            walkloop = walkloop - 1;
            }
        }
          else if (newdirection == eKeyboardMovement_Right) {
         if (walkloop < 40) { //Basic build-up of walk speed
           walkloop = walkloop +1;
          }
         if (walkloop < 0) { //Makes turning or suddenly stopping a little sharper
           walkloop = walkloop + 1;
          }
         }         
   }

//====================================================================================================

Dualnames

Well, I'm afraid character speed can only be changed when character isn't moving. This is how it works...
At least on AGS 2.72
I have a GUI that enables the player to change characters speed but when I opened the game while the character was in motion and I tried to alter the speed the game crashed.. By the way walk speed can't be smaller than 1..
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Khris

Guineapiggy is aware that the speed can't be changed while the player is moving.
It's due to the internal methods of pathfinding and making the char walk.
This can't be changed easily; according to CJ he'd have to rewrite everything.
It has been brought up and is on the list but not likely to be implemented soon.

Actually, the walk speed can be smaller than 1; afaik a walk speed setting of -2 will result in a speed of 0.5 (-3 -> 0.333, -4 -> 0.25, etc.)

Guineapiggy

Thanks for the info, Khris. I discovered it was using the walkaccel == clause, as somehow it managed to increase beyond where it should. To resolve the issue I replaced it for a >=. Fiddly variables I guess. Still, I've managed to make a fairly decent walking system so far which includes running, sliding to force a sudden turn and soon I'm hoping to code in running in to walls, jumping and falling.

m0ds

Interesting. Does this only cover left and right though? What about up & down? This could be useful code for a mini-game I'm making...only it's top down, so it'd need left & right, up & down. Still, good code. I will try it out sometime. Plus you've now pretty much solved a question I was going to have to ask ;) Awesome!

Guineapiggy

#5
I'll upload it for y'all when it's in all there for sure, though it's designed for a side-on view so it'd take some modification for top down. The script you see there is missing a header and very basic compared to what I've modified it to.

Dualnames

Quote from: KhrisMUC on Tue 20/11/2007 09:13:45
Guineapiggy is aware that the speed can't be changed while the player is moving.
It's due to the internal methods of pathfinding and making the char walk.
This can't be changed easily; according to CJ he'd have to rewrite everything.
It has been brought up and is on the list but not likely to be implemented soon.

Actually, the walk speed can be smaller than 1; afaik a walk speed setting of -2 will result in a speed of 0.5 (-3 -> 0.333, -4 -> 0.25, etc.)

That's a new thing. You just know about everything... ;D
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Ashen

It was added in 2.72, which is over a year old, so it's not that new...
And, it IS mentioned in the manual:
Quote
The "Movement speed" option allows you to control how fast the character moves when walking. Here, a larger number means he walks faster. If you find that a movement speed of 1 is still too fast, you can use negative numbers (eg. -3) which will move even more slowly. The lower you go, the slower the movement speed.

So there's no excuse for you not knowing about it... ;)

Quote
I discovered it was using the walkaccel == clause, as somehow it managed to increase beyond where it should. To resolve the issue I replaced it for a >=

Why not just use '>' and skip the +1 you've had to include. i.e.:
Code: ags

if (walkaccel > WlkSpd) 
I know what you're thinking ... Don't think that.

Dualnames

So there's no excuse for you not knowing about it... ;)

Ok, then hang me..
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Guineapiggy

Quote from: Ashen on Wed 21/11/2007 15:20:26
Why not just use '>' and skip the +1 you've had to include. i.e.:
Because the +1 delays the walking somewhat, the whole thing is a delay loop to regulate the character's speed. That said I've since replaced this system with a better one.

Ashen

But > WlkSpd is exactly the same as >= WalkSpd+1, as both will be triggered when walkaccel hits WlkSpd + 1. You wouldn't gain or lose anything which ever way you did it, I just think > WlkSpd looks neater.

Of course, if you've abandoned that method anyway, it's all academic.


And please, don't quote the entire post from immediately before yours - it makes the thread look cluttered.
I know what you're thinking ... Don't think that.

Guineapiggy

Well as we've seen from the issue that bugged this script not neccesarily, it would have produced sporadic jumps in walk speed.

SMF spam blocked by CleanTalk